From 09975c2dec6c232187124bc6181291e07b8601ca 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 | 19 ++++++ .../tycho/apitools/ApiAnalysisMojo.java | 2 +- .../ApiAnnotationsClasspathContributor.java | 60 +++++++++++++++++++ ...ractSpecificationClasspathContributor.java | 22 ++++--- .../api-tools/annotations/bundle2/.classpath | 7 +++ .../api-tools/annotations/bundle2/.project | 34 +++++++++++ .../annotations/bundle2/META-INF/MANIFEST.MF | 8 +++ .../annotations/bundle2/build.properties | 4 ++ .../api-tools/annotations/bundle2/pom.xml | 23 +++++++ .../bundle2/src/bundle/ApiInterface2.java | 10 ++++ 10 files changed, 180 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/bundle2/.classpath create mode 100644 tycho-its/projects/api-tools/annotations/bundle2/.project create mode 100644 tycho-its/projects/api-tools/annotations/bundle2/META-INF/MANIFEST.MF create mode 100644 tycho-its/projects/api-tools/annotations/bundle2/build.properties create mode 100644 tycho-its/projects/api-tools/annotations/bundle2/pom.xml create mode 100644 tycho-its/projects/api-tools/annotations/bundle2/src/bundle/ApiInterface2.java diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index bd78fb19ce..0d2cb2c38e 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -6,6 +6,25 @@ If you are reading this in the browser, then you can quickly jump to specific ve ## 5.0.0 (under development) +### support for PDE Api Tools Annotations + +Tycho now supports PDE Api Tools Annotations to be added to the project automatically. + +To enable this add + +```xml + + org.eclipse.tycho + tycho-apitools-plugin + ${tycho-version} + +``` + +to your project and make sure it has the `org.eclipse.pde.api.tools.apiAnalysisNature` nature enabled in the `.project` file, for details how to use these see: + +- https://help.eclipse.org/latest/topic/org.eclipse.pde.doc.user/reference/api-tooling/api_javadoc_tags.htm +- https://help.eclipse.org/latest/topic/org.eclipse.pde.doc.user/reference/api/org/eclipse/pde/api/tools/annotations/package-summary.html + ### new tycho-repository-plugin Tycho now contains a new `tycho-repository-plugin` that can be used to package OSGi repositories. 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/bundle2/.classpath b/tycho-its/projects/api-tools/annotations/bundle2/.classpath new file mode 100644 index 0000000000..8af7df3db4 --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/bundle2/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tycho-its/projects/api-tools/annotations/bundle2/.project b/tycho-its/projects/api-tools/annotations/bundle2/.project new file mode 100644 index 0000000000..df2962ed9f --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/bundle2/.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/bundle2/META-INF/MANIFEST.MF b/tycho-its/projects/api-tools/annotations/bundle2/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..79329a422c --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/bundle2/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/bundle2/build.properties b/tycho-its/projects/api-tools/annotations/bundle2/build.properties new file mode 100644 index 0000000000..12b49e3215 --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/bundle2/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = .,\ + META-INF/ diff --git a/tycho-its/projects/api-tools/annotations/bundle2/pom.xml b/tycho-its/projects/api-tools/annotations/bundle2/pom.xml new file mode 100644 index 0000000000..455407d576 --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/bundle2/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/bundle2/src/bundle/ApiInterface2.java b/tycho-its/projects/api-tools/annotations/bundle2/src/bundle/ApiInterface2.java new file mode 100644 index 0000000000..83e6639045 --- /dev/null +++ b/tycho-its/projects/api-tools/annotations/bundle2/src/bundle/ApiInterface2.java @@ -0,0 +1,10 @@ +package bundle; + +import org.eclipse.pde.api.tools.annotations.NoExtend; + +@NoExtend +public interface ApiInterface2 { + + void sayHello(); + +}