From f7736f188e21e45458670f978e9bc6f9fc18ecd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sun, 12 Nov 2023 17:48:19 +0100 Subject: [PATCH] Add support for API Tools Annotations to Tycho This adds a similar feature like PDE will offer soon see https://github.com/eclipse-pde/eclipse.pde/pull/912 --- RELEASE_NOTES.md | 10 ++++ .../tycho/apitools/ApiAnalysisMojo.java | 2 +- .../ApiAnnotationsClasspathContributor.java | 60 +++++++++++++++++++ ...ractSpecificationClasspathContributor.java | 22 ++++--- .../projects/api-tools/annotations/.classpath | 7 +++ .../projects/api-tools/annotations/.project | 34 +++++++++++ .../annotations/META-INF/MANIFEST.MF | 8 +++ .../api-tools/annotations/build.properties | 4 ++ .../projects/api-tools/annotations/pom.xml | 23 +++++++ .../annotations/src/bundle/ApiInterface2.java | 10 ++++ .../tycho/test/apitools/ApiToolsTest.java | 7 +++ 11 files changed, 178 insertions(+), 9 deletions(-) create mode 100644 tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnnotationsClasspathContributor.java create mode 100644 tycho-its/projects/api-tools/annotations/.classpath create mode 100644 tycho-its/projects/api-tools/annotations/.project create mode 100644 tycho-its/projects/api-tools/annotations/META-INF/MANIFEST.MF create mode 100644 tycho-its/projects/api-tools/annotations/build.properties create mode 100644 tycho-its/projects/api-tools/annotations/pom.xml create mode 100644 tycho-its/projects/api-tools/annotations/src/bundle/ApiInterface2.java diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 028bb972cb..af2cc85180 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -2,6 +2,16 @@ This page describes the noteworthy improvements provided by each release of Eclipse Tycho. +## 4.0.5 + +### backports: +- support for PDE Api Tools Annotations +- api tools fixes + +## 4.0.4 + +Various bugfixes and backports + ## 4.0.3 ### new option to filter added repository-references when assembling a p2-repository diff --git a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java index 8b0cc866f4..3e83767c99 100644 --- a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java +++ b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java @@ -138,7 +138,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { } Optional eclipseProject = projectManager.getEclipseProject(project); if (eclipseProject.isEmpty() - || !eclipseProject.get().hasNature("org.eclipse.pde.api.tools.apiAnalysisNature")) { + || !eclipseProject.get().hasNature(ApiPlugin.NATURE_ID)) { return; } diff --git a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnnotationsClasspathContributor.java b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnnotationsClasspathContributor.java new file mode 100644 index 0000000000..e1560017db --- /dev/null +++ b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnnotationsClasspathContributor.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2023 Christoph Läubrich and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich - initial API and implementation + *******************************************************************************/ +package org.eclipse.tycho.apitools; + +import java.util.Optional; + +import javax.inject.Inject; + +import org.apache.maven.SessionScoped; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.annotations.Component; +import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; +import org.eclipse.tycho.classpath.ClasspathContributor; +import org.eclipse.tycho.core.TychoProjectManager; +import org.eclipse.tycho.core.osgitools.AbstractSpecificationClasspathContributor; +import org.eclipse.tycho.model.project.EclipseProject; +import org.osgi.framework.VersionRange; + +@Component(role = ClasspathContributor.class, hint = "apitools-annotations") +@SessionScoped +public class ApiAnnotationsClasspathContributor extends AbstractSpecificationClasspathContributor { + + private static final String PACKAGE_NAME = "org.eclipse.pde.api.tools.annotations"; + private static final String GROUP_ID = "org.eclipse.pde"; + private static final String ARTIFACT_ID = "org.eclipse.pde.api.tools.annotations"; + private static final VersionRange VERSION = new VersionRange("[1,2)"); + private TychoProjectManager projectManager; + + @Inject + public ApiAnnotationsClasspathContributor(MavenSession session, TychoProjectManager projectManager) { + super(session, PACKAGE_NAME, GROUP_ID, ARTIFACT_ID); + this.projectManager = projectManager; + } + + @Override + protected VersionRange getSpecificationVersion(MavenProject project) { + return VERSION; + } + + @Override + protected boolean isValidProject(MavenProject project) { + Optional eclipseProject = projectManager.getEclipseProject(project); + if (eclipseProject.isPresent()) { + return eclipseProject.get().hasNature(ApiPlugin.NATURE_ID); + } + return false; + } + +} diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractSpecificationClasspathContributor.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractSpecificationClasspathContributor.java index e89bcce6da..0dd99077d8 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractSpecificationClasspathContributor.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractSpecificationClasspathContributor.java @@ -66,18 +66,24 @@ protected AbstractSpecificationClasspathContributor(MavenSession session, String @Override public final List getAdditionalClasspathEntries(MavenProject project, String scope) { - VersionRange specificationVersion = getSpecificationVersion(project); - Optional mavenBundle = findBundle(project, specificationVersion); - if (mavenBundle.isPresent()) { - ResolvedArtifactKey resolved = mavenBundle.get(); - logger.debug("Resolved " + packageName + " to " + resolved.getId() + " " + resolved.getVersion() + " @ " - + resolved.getLocation()); - return List.of(new DefaultClasspathEntry(resolved, List.of(accessRule))); + if (isValidProject(project)) { + VersionRange specificationVersion = getSpecificationVersion(project); + Optional mavenBundle = findBundle(project, specificationVersion); + if (mavenBundle.isPresent()) { + ResolvedArtifactKey resolved = mavenBundle.get(); + logger.debug("Resolved " + packageName + " to " + resolved.getId() + " " + resolved.getVersion() + " @ " + + resolved.getLocation()); + return List.of(new DefaultClasspathEntry(resolved, List.of(accessRule))); + } + logger.warn("Cannot resolve specification package " + packageName + ", classpath might be incomplete"); } - logger.warn("Cannot resolve specification package " + packageName + ", classpath might be incomplete"); return Collections.emptyList(); } + protected boolean isValidProject(MavenProject project) { + return true; + } + protected Optional findBundle(MavenProject project, VersionRange specificationVersion) { Optional mavenBundle = mavenBundleResolver.resolveMavenBundle(project, session, MavenArtifactKey.of(PublisherHelper.CAPABILITY_NS_JAVA_PACKAGE, packageName, diff --git a/tycho-its/projects/api-tools/annotations/.classpath b/tycho-its/projects/api-tools/annotations/.classpath new file mode 100644 index 0000000000..8af7df3db4 --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tycho-its/projects/api-tools/annotations/.project b/tycho-its/projects/api-tools/annotations/.project new file mode 100644 index 0000000000..df2962ed9f --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/.project @@ -0,0 +1,34 @@ + + + api-bundle-2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + org.eclipse.pde.api.tools.apiAnalysisBuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.pde.PluginNature + org.eclipse.pde.api.tools.apiAnalysisNature + + diff --git a/tycho-its/projects/api-tools/annotations/META-INF/MANIFEST.MF b/tycho-its/projects/api-tools/annotations/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..79329a422c --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/META-INF/MANIFEST.MF @@ -0,0 +1,8 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Plugin with API +Bundle-SymbolicName: api-bundle-2 +Bundle-Version: 0.0.1.qualifier +Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=11))" +Export-Package: bundle +Automatic-Module-Name: bundle diff --git a/tycho-its/projects/api-tools/annotations/build.properties b/tycho-its/projects/api-tools/annotations/build.properties new file mode 100644 index 0000000000..12b49e3215 --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = .,\ + META-INF/ diff --git a/tycho-its/projects/api-tools/annotations/pom.xml b/tycho-its/projects/api-tools/annotations/pom.xml new file mode 100644 index 0000000000..455407d576 --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + org.eclipse.tycho.tycho-its + api-bundle-2 + 0.0.1-SNAPSHOT + eclipse-plugin + + + + org.eclipse.tycho + tycho-maven-plugin + ${tycho-version} + true + + + org.eclipse.tycho + tycho-apitools-plugin + ${tycho-version} + + + + diff --git a/tycho-its/projects/api-tools/annotations/src/bundle/ApiInterface2.java b/tycho-its/projects/api-tools/annotations/src/bundle/ApiInterface2.java new file mode 100644 index 0000000000..83e6639045 --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/src/bundle/ApiInterface2.java @@ -0,0 +1,10 @@ +package bundle; + +import org.eclipse.pde.api.tools.annotations.NoExtend; + +@NoExtend +public interface ApiInterface2 { + + void sayHello(); + +} diff --git a/tycho-its/src/test/java/org/eclipse/tycho/test/apitools/ApiToolsTest.java b/tycho-its/src/test/java/org/eclipse/tycho/test/apitools/ApiToolsTest.java index 9546933741..3b8f936e33 100644 --- a/tycho-its/src/test/java/org/eclipse/tycho/test/apitools/ApiToolsTest.java +++ b/tycho-its/src/test/java/org/eclipse/tycho/test/apitools/ApiToolsTest.java @@ -142,4 +142,11 @@ public void testSingleJar() throws Exception { verifier.executeGoals(List.of("clean", "verify")); verifier.verifyErrorFreeLog(); } + + @Test + public void testAnnotations() throws Exception { + Verifier verifier = getVerifier("api-tools/annotations", true, true); + verifier.executeGoals(List.of("clean", "verify")); + verifier.verifyErrorFreeLog(); + } }