From 40ca2698002b999519614a46f1953b05a1fd634a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sat, 9 Dec 2023 13:45:53 +0100 Subject: [PATCH] Cleanup TychoProjectUtils and use ProjectManager instead --- .../org/eclipse/tycho/TychoConstants.java | 2 + .../tycho/compiler/ValidateClassPathMojo.java | 4 +- .../core/maven/TychoWorkspaceReader.java | 38 ++++++++---------- .../core/osgitools/MavenBundleResolver.java | 3 +- .../DefaultTargetPlatformService.java | 8 +--- .../tycho/core/utils/TychoProjectUtils.java | 33 --------------- .../p2resolver/P2DependencyResolver.java | 12 +----- .../PreliminaryTargetPlatformImpl.java | 8 +++- .../p2tools/RepositoryReferenceTool.java | 5 +-- .../plugins/p2/DependenciesTreeMojo.java | 17 ++++---- .../p2/repository/AssembleRepositoryMojo.java | 15 +++++-- .../tycho/packaging/PackageIUMojo.java | 8 ++-- .../tycho/source/SourceFeatureMojo.java | 10 +++-- .../surefire/AbstractEclipseTestMojo.java | 4 +- .../eclipse/tycho/surefire/BndTestMojo.java | 40 +++++++++++-------- .../bnd/TargetPlatformRepository.java | 6 +-- .../TargetPlatformArtifactResolver.java | 38 ++++++++++-------- 17 files changed, 116 insertions(+), 135 deletions(-) diff --git a/tycho-api/src/main/java/org/eclipse/tycho/TychoConstants.java b/tycho-api/src/main/java/org/eclipse/tycho/TychoConstants.java index 5b5ead3643..36fa3ffd72 100644 --- a/tycho-api/src/main/java/org/eclipse/tycho/TychoConstants.java +++ b/tycho-api/src/main/java/org/eclipse/tycho/TychoConstants.java @@ -20,6 +20,8 @@ public interface TychoConstants { public static final String ECLIPSE_LATEST = "https://download.eclipse.org/releases/2023-09/"; + public static final String TYCHO_NOT_CONFIGURED = "Tycho build extension not configured for "; + static final String ANY_QUALIFIER = "qualifier"; static final boolean USE_SMART_BUILDER = Boolean diff --git a/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/ValidateClassPathMojo.java b/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/ValidateClassPathMojo.java index 9d69937397..10046f3cf5 100644 --- a/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/ValidateClassPathMojo.java +++ b/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/ValidateClassPathMojo.java @@ -24,10 +24,10 @@ import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; import org.eclipse.tycho.ReactorProject; +import org.eclipse.tycho.TychoConstants; import org.eclipse.tycho.core.TychoProject; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; import org.eclipse.tycho.core.osgitools.OsgiBundleProject; -import org.eclipse.tycho.core.utils.TychoProjectUtils; /** * This mojo could be added to a build if validation of the classpath is desired before the @@ -48,7 +48,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { TychoProject projectType = projectTypes.get(project.getPackaging()); if (projectType instanceof OsgiBundleProject bundleProject) { ReactorProject reactorProject = DefaultReactorProject.adapt(project); - if (TychoProjectUtils.getOptionalDependencyArtifacts(reactorProject).isPresent()) { + if (reactorProject.getContextValue(TychoConstants.CTX_DEPENDENCY_ARTIFACTS) != null) { bundleProject.getClasspath(reactorProject); } else { getLog().info("Skipped classpath validation as project is currently not resolved"); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoWorkspaceReader.java b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoWorkspaceReader.java index 393506a889..1609c837f7 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoWorkspaceReader.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoWorkspaceReader.java @@ -15,7 +15,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; -import java.util.Optional; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Model; @@ -31,9 +30,13 @@ import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.WorkspaceReader; import org.eclipse.aether.repository.WorkspaceRepository; -import org.eclipse.tycho.*; +import org.eclipse.tycho.ArtifactDescriptor; +import org.eclipse.tycho.ArtifactKey; +import org.eclipse.tycho.DependencyArtifacts; +import org.eclipse.tycho.ReactorProject; +import org.eclipse.tycho.TychoConstants; +import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; -import org.eclipse.tycho.core.utils.TychoProjectUtils; @Component(role = WorkspaceReader.class, hint = "TychoWorkspaceReader") public class TychoWorkspaceReader implements MavenWorkspaceReader { @@ -48,6 +51,9 @@ public class TychoWorkspaceReader implements MavenWorkspaceReader { @Requirement private ModelWriter modelWriter; + @Requirement + private TychoProjectManager projectManager; + public TychoWorkspaceReader() { repository = new WorkspaceRepository("tycho", null); } @@ -137,13 +143,12 @@ private File findP2Artifact(final Artifact artifact) { if (session != null) { final MavenProject currentProject = session.getCurrentProject(); final ReactorProject reactorProject = DefaultReactorProject.adapt(currentProject); - final Optional dependencyMetadata = - TychoProjectUtils.getOptionalDependencyArtifacts(reactorProject); - - if (dependencyMetadata.isPresent()) { + final DependencyArtifacts dependencyMetadata = (DependencyArtifacts) reactorProject + .getContextValue(TychoConstants.CTX_DEPENDENCY_ARTIFACTS); + if (dependencyMetadata != null) { logger.debug("Attempting to resolve " + artifact + " for project " + currentProject); - for (final ArtifactDescriptor descriptor : dependencyMetadata.get().getArtifacts()) { + for (final ArtifactDescriptor descriptor : dependencyMetadata.getArtifacts()) { if (isArtifactMatch(descriptor.getKey(), artifact)) { return descriptor.getLocation(true); } @@ -157,9 +162,8 @@ private File findP2Artifact(final Artifact artifact) { private boolean isArtifactMatch(final ArtifactKey artifactKey, final Artifact artifact) { final String groupId = artifact.getGroupId(); final String type = groupId.substring(TychoConstants.P2_GROUPID_PREFIX.length()).replace('.', '-'); - return artifactKey.getType().equals(type) && - artifactKey.getId().equals(artifact.getArtifactId()) && - artifactKey.getVersion().equals(artifact.getVersion()); + return artifactKey.getType().equals(type) && artifactKey.getId().equals(artifact.getArtifactId()) + && artifactKey.getVersion().equals(artifact.getVersion()); } private File findMavenArtifact(@SuppressWarnings("unused") final Artifact artifact) { @@ -176,16 +180,8 @@ private File getFileForArtifact(final Artifact artifact) { final RepositorySystemSession repositorySession = legacySupport.getRepositorySession(); final LocalRepository localRepository = repositorySession.getLocalRepository(); final File basedir = localRepository.getBasedir(); - final String repositoryPath = "p2/osgi/bundle/" + - artifact.getArtifactId() + - "/" + - artifact.getVersion() + - "/" + - artifact.getArtifactId() + - "-" + - artifact.getVersion() + - "." + - artifact.getExtension(); + final String repositoryPath = "p2/osgi/bundle/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" + + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + artifact.getExtension(); return new File(basedir, repositoryPath); } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/MavenBundleResolver.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/MavenBundleResolver.java index 827a87385b..2c3a4085f0 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/MavenBundleResolver.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/MavenBundleResolver.java @@ -35,7 +35,6 @@ import org.eclipse.tycho.TargetPlatform; import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.maven.MavenDependenciesResolver; -import org.eclipse.tycho.core.utils.TychoProjectUtils; /** * {@link MavenBundleResolver} helps in gathering bundles that are living in P2 and maven world and @@ -71,7 +70,7 @@ public Optional resolveMavenBundle(MavenProject project, Ma if (project == null) { return Optional.empty(); } - TargetPlatform tp = TychoProjectUtils.getTargetPlatformIfAvailable(DefaultReactorProject.adapt(project)); + TargetPlatform tp = projectManager.getTargetPlatform(project).orElse(null); String type = mavenArtifactKey.getType(); String resolvedType = PublisherHelper.CAPABILITY_NS_JAVA_PACKAGE.equals(type) ? ArtifactType.TYPE_ECLIPSE_PLUGIN : type; diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformService.java b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformService.java index ebed7133f5..56fb76dd2a 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformService.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformService.java @@ -76,11 +76,7 @@ public Optional getTargetPlatform() throws DependencyResolutionE @Override public Optional getTargetPlatform(ReactorProject project) throws DependencyResolutionException { - synchronized (project) { - Object contextValue = project.getContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY); - if (contextValue instanceof TargetPlatform) { - return Optional.of((TargetPlatform) contextValue); - } + return project.computeContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY, () -> { MavenSession session = legacySupport.getSession(); if (repositoryManager == null || session == null) { return Optional.empty(); @@ -91,7 +87,7 @@ public Optional getTargetPlatform(ReactorProject project) throws TargetPlatform finalTargetPlatform = computeFinalTargetPlatform(project, upstreamProjects, pomDependenciesCollector); return Optional.ofNullable(finalTargetPlatform); - } + }); } /** diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/utils/TychoProjectUtils.java b/tycho-core/src/main/java/org/eclipse/tycho/core/utils/TychoProjectUtils.java index aa8b355fc9..1eeb314e64 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/utils/TychoProjectUtils.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/utils/TychoProjectUtils.java @@ -14,54 +14,21 @@ import java.util.ArrayList; import java.util.List; -import java.util.Optional; import java.util.Properties; import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; -import org.eclipse.tycho.DependencyArtifacts; import org.eclipse.tycho.PlatformPropertiesUtils; import org.eclipse.tycho.ReactorProject; -import org.eclipse.tycho.TargetPlatform; import org.eclipse.tycho.TychoConstants; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; import org.eclipse.tycho.core.resolver.shared.DependencySeed; public class TychoProjectUtils { - private static final String TYCHO_NOT_CONFIGURED = "Tycho build extension not configured for "; - public static final String TYCHO_ENV_OSGI_WS = "tycho.env.osgi.ws"; public static final String TYCHO_ENV_OSGI_OS = "tycho.env.osgi.os"; public static final String TYCHO_ENV_OSGI_ARCH = "tycho.env.osgi.arch"; - public static Optional getOptionalDependencyArtifacts(ReactorProject project) { - DependencyArtifacts resolvedDependencies = (DependencyArtifacts) project - .getContextValue(TychoConstants.CTX_DEPENDENCY_ARTIFACTS); - return Optional.ofNullable(resolvedDependencies); - } - - /** - * Returns the final target platform of the given project. - */ - public static TargetPlatform getTargetPlatform(ReactorProject project) { - TargetPlatform targetPlatform = getTargetPlatformIfAvailable(project); - if (targetPlatform == null) { - throw new IllegalStateException(TYCHO_NOT_CONFIGURED + project.toString()); - } - return targetPlatform; - } - - /** - * Returns the final target platform of the given project, or null if the target - * platform is not available. - * - * Projects with -Dtycho.targetPlatform use the legacy LocalDependencyResolver, which doesn't - * provide the {@link TargetPlatform} interface. - */ - public static TargetPlatform getTargetPlatformIfAvailable(ReactorProject project) { - return (TargetPlatform) project.getContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY); - } - /** * Returns the (editable) list of {@link DependencySeed}s for the given project. */ diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java index 145f765ca5..1d3bcbef14 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java @@ -28,6 +28,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Set; import java.util.TreeMap; import java.util.function.Function; @@ -83,7 +84,6 @@ import org.eclipse.tycho.core.resolver.P2Resolver; import org.eclipse.tycho.core.resolver.P2ResolverFactory; import org.eclipse.tycho.core.resolver.shared.PomDependencies; -import org.eclipse.tycho.core.utils.TychoProjectUtils; import org.eclipse.tycho.helper.PluginRealmHelper; import org.eclipse.tycho.p2.metadata.DependencyMetadataGenerator; import org.eclipse.tycho.p2.metadata.PublisherOptions; @@ -306,17 +306,9 @@ private void addEntireP2RepositoryToTargetPlatform(ArtifactRepository repository public DependencyArtifacts resolveDependencies(final MavenSession session, final MavenProject project, TargetPlatform targetPlatform, List reactorProjects, DependencyResolverConfiguration resolverConfiguration, List environments) { - ReactorProject reactorProject = DefaultReactorProject.adapt(project); - if (targetPlatform == null) { - targetPlatform = TychoProjectUtils.getTargetPlatform(reactorProject); - } - - // TODO 364134 For compatibility reasons, target-platform-configuration includes settings for the dependency resolution - // --> split this information logically, e.g. through two distinct interfaces + Objects.requireNonNull(targetPlatform); TargetPlatformConfiguration configuration = projectManager.getTargetPlatformConfiguration(project); - P2Resolver osgiResolverImpl = resolverFactory.createResolver(environments); - return doResolveDependencies(session, project, reactorProjects, resolverConfiguration, targetPlatform, osgiResolverImpl, configuration); } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/PreliminaryTargetPlatformImpl.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/PreliminaryTargetPlatformImpl.java index 9a1a94a2e9..089b5f6627 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/PreliminaryTargetPlatformImpl.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/PreliminaryTargetPlatformImpl.java @@ -14,6 +14,7 @@ *******************************************************************************/ package org.eclipse.tycho.p2resolver; +import java.net.URI; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -32,6 +33,7 @@ import org.eclipse.tycho.core.shared.MavenLogger; import org.eclipse.tycho.p2.repository.LocalArtifactRepository; import org.eclipse.tycho.p2.repository.LocalMetadataRepository; +import org.eclipse.tycho.p2.repository.ProviderOnlyArtifactRepository; public class PreliminaryTargetPlatformImpl extends TargetPlatformBaseImpl { @@ -53,6 +55,8 @@ public class PreliminaryTargetPlatformImpl extends TargetPlatformBaseImpl { private final boolean includeLocalRepo; + private IArtifactRepository artifactRepository; + public PreliminaryTargetPlatformImpl(Map reactorProjectIUs, Collection externalIUs, ExecutionEnvironmentResolutionHints executionEnvironment, TargetPlatformFilterEvaluator filter, LocalMetadataRepository localMetadataRepository, @@ -65,6 +69,7 @@ public PreliminaryTargetPlatformImpl(Map collectAllInstallableUnits( @@ -128,8 +133,7 @@ public IMetadataRepository getMetadataRepository() { @Override public IArtifactRepository getArtifactRepository() { - // the preliminary TP shall not be used to create build results, so this method is not needed - throw new UnsupportedOperationException(); + return artifactRepository; } } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2tools/RepositoryReferenceTool.java b/tycho-core/src/main/java/org/eclipse/tycho/p2tools/RepositoryReferenceTool.java index cbabf25713..e12095a942 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2tools/RepositoryReferenceTool.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2tools/RepositoryReferenceTool.java @@ -35,7 +35,6 @@ import org.eclipse.tycho.core.TargetPlatformConfiguration; import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; -import org.eclipse.tycho.core.utils.TychoProjectUtils; import org.eclipse.tycho.p2.repository.RepositoryBlackboardKey; import org.eclipse.tycho.p2.tools.RepositoryReferences; @@ -114,8 +113,8 @@ private void addTargetPlatformRepository(RepositoryReferences sources, MavenSess repositoryLocation.mkdirs(); try (FileOutputStream stream = new FileOutputStream(new File(repositoryLocation, "content.xml"))) { - ReactorProject reactorProject = DefaultReactorProject.adapt(project); - TargetPlatform targetPlatform = TychoProjectUtils.getTargetPlatform(reactorProject); + TargetPlatform targetPlatform = projectManager.getTargetPlatform(project) + .orElseThrow(() -> new MojoFailureException(TychoConstants.TYCHO_NOT_CONFIGURED + project)); TargetPlatformConfiguration configuration = projectManager.getTargetPlatformConfiguration(project); diff --git a/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/DependenciesTreeMojo.java b/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/DependenciesTreeMojo.java index f327dbc19b..d526a4c69a 100644 --- a/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/DependenciesTreeMojo.java +++ b/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/DependenciesTreeMojo.java @@ -37,10 +37,10 @@ import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.equinox.p2.metadata.IRequirement; import org.eclipse.tycho.ArtifactDescriptor; -import org.eclipse.tycho.DependencyArtifacts; import org.eclipse.tycho.ReactorProject; +import org.eclipse.tycho.core.TychoProject; +import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; -import org.eclipse.tycho.core.utils.TychoProjectUtils; import org.eclipse.tycho.p2maven.InstallableUnitGenerator; /** @@ -60,20 +60,24 @@ public class DependenciesTreeMojo extends AbstractMojo { @Component private LegacySupport legacySupport; + @Component + private TychoProjectManager projectManager; + @Override public void execute() throws MojoExecutionException, MojoFailureException { //TODO maybe we can compute a org.apache.maven.shared.dependency.graph.DependencyNode and reuse org.apache.maven.plugins.dependency.tree.TreeMojo wich has a getSerializingDependencyNodeVisitor - ReactorProject reactorProject = DefaultReactorProject.adapt(project); - Optional optional = TychoProjectUtils.getOptionalDependencyArtifacts(reactorProject); - if (optional.isEmpty()) { + Optional tychoProject = projectManager.getTychoProject(project); + if (tychoProject.isEmpty()) { return; } + Set written = new HashSet(); written.add(project.getId()); getLog().info(project.getId()); - List artifacts = optional.get().getArtifacts(); + List artifacts = tychoProject.get() + .getDependencyArtifacts(DefaultReactorProject.adapt(project)).getArtifacts(); Map> projectMap = artifacts.stream() .filter(a -> a.getMavenProject() != null).flatMap(a -> { return a.getInstallableUnits().stream().map(iu -> new SimpleEntry<>(iu, a.getMavenProject())); @@ -81,7 +85,6 @@ public void execute() throws MojoExecutionException, MojoFailureException { .collect(Collectors.groupingBy(Entry::getKey, Collectors.mapping(Entry::getValue, Collectors.toSet()))); Set units = artifacts.stream().flatMap(d -> d.getInstallableUnits().stream()) .collect(Collectors.toCollection(HashSet::new)); - reactorProject.getDependencyMetadata(); List initial; try { initial = new ArrayList( diff --git a/tycho-p2-repository-plugin/src/main/java/org/eclipse/tycho/plugins/p2/repository/AssembleRepositoryMojo.java b/tycho-p2-repository-plugin/src/main/java/org/eclipse/tycho/plugins/p2/repository/AssembleRepositoryMojo.java index a08709e06b..ba2d7f2008 100644 --- a/tycho-p2-repository-plugin/src/main/java/org/eclipse/tycho/plugins/p2/repository/AssembleRepositoryMojo.java +++ b/tycho-p2-repository-plugin/src/main/java/org/eclipse/tycho/plugins/p2/repository/AssembleRepositoryMojo.java @@ -28,6 +28,7 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.MatchPattern; import org.eclipse.tycho.FileLockService; @@ -35,6 +36,7 @@ import org.eclipse.tycho.ReactorProject; import org.eclipse.tycho.TychoConstants; import org.eclipse.tycho.core.TychoProject; +import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.osgitools.EclipseRepositoryProject; import org.eclipse.tycho.core.resolver.shared.DependencySeed; import org.eclipse.tycho.core.utils.TychoProjectUtils; @@ -317,6 +319,9 @@ public static class RepositoryReferenceFilter { @Component private FileLockService fileLockService; + @Component + private TychoProjectManager projectManager; + @Override public void execute() throws MojoExecutionException, MojoFailureException { File destination = getAssemblyRepositoryLocation(); @@ -332,9 +337,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { } reactorProject.setContextValue(TychoConstants.CTX_METADATA_ARTIFACT_LOCATION, categoriesDirectory); - RepositoryReferences sources = repositoryReferenceTool.getVisibleRepositories(getProject(), getSession(), + MavenProject project = getProject(); + RepositoryReferences sources = repositoryReferenceTool.getVisibleRepositories(project, getSession(), RepositoryReferenceTool.REPOSITORIES_INCLUDE_CURRENT_MODULE); - sources.setTargetPlatform(TychoProjectUtils.getTargetPlatform(getReactorProject())); + sources.setTargetPlatform(projectManager.getTargetPlatform(project) + .orElseThrow(() -> new MojoFailureException(TychoConstants.TYCHO_NOT_CONFIGURED + project))); List repositoryReferences = getCategories(categoriesDirectory).stream()// .map(Category::getRepositoryReferences)// @@ -344,14 +351,14 @@ public void execute() throws MojoExecutionException, MojoFailureException { Predicate autoReferencesFilter = buildRepositoryReferenceLocationFilter(); List autoRepositoryRefeferences = new ArrayList<>(); if (addPomRepositoryReferences) { - getProject().getRepositories().stream() // + project.getRepositories().stream() // .filter(pomRepo -> "p2".equals(pomRepo.getLayout())) .filter(pomRepo -> autoReferencesFilter.test(pomRepo.getUrl())) .map(pomRepo -> new RepositoryReference(pomRepo.getName(), pomRepo.getUrl(), true)) .forEach(autoRepositoryRefeferences::add); } if (addIUTargetRepositoryReferences) { - projectManager.getTargetPlatformConfiguration(getProject()).getTargets().stream() + projectManager.getTargetPlatformConfiguration(project).getTargets().stream() .flatMap(tpFile -> tpFile.getLocations().stream()) .filter(InstallableUnitLocation.class::isInstance).map(InstallableUnitLocation.class::cast) .flatMap(iu -> iu.getRepositories().stream()) diff --git a/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackageIUMojo.java b/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackageIUMojo.java index 99f95196e8..a9a2d5c160 100644 --- a/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackageIUMojo.java +++ b/tycho-packaging-plugin/src/main/java/org/eclipse/tycho/packaging/PackageIUMojo.java @@ -27,8 +27,8 @@ import org.codehaus.plexus.archiver.util.DefaultFileSet; import org.codehaus.plexus.archiver.zip.ZipArchiver; import org.eclipse.tycho.TargetPlatform; +import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; -import org.eclipse.tycho.core.utils.TychoProjectUtils; import org.eclipse.tycho.model.IU; /** @@ -56,6 +56,9 @@ public class PackageIUMojo extends AbstractTychoPackagingMojo { @Component(role = Archiver.class, hint = "zip") private ZipArchiver zipArchiver; + @Component + private TychoProjectManager projectManager; + @Override public void execute() throws MojoExecutionException, MojoFailureException { synchronized (LOCK) { @@ -101,8 +104,7 @@ private void expandVersions(IU iu) throws MojoFailureException { iuTransformer.replaceQualifierInCapabilities(iu.getProvidedCapabilites(), DefaultReactorProject.adapt(project).getBuildQualifier()); - TargetPlatform targetPlatform = TychoProjectUtils - .getTargetPlatformIfAvailable(DefaultReactorProject.adapt(project)); + TargetPlatform targetPlatform = projectManager.getTargetPlatform(project).orElse(null); if (targetPlatform == null) { getLog().warn( "Skipping version reference expansion in p2iu project using the deprecated -Dtycho.targetPlatform configuration"); diff --git a/tycho-source-plugin/src/main/java/org/eclipse/tycho/source/SourceFeatureMojo.java b/tycho-source-plugin/src/main/java/org/eclipse/tycho/source/SourceFeatureMojo.java index 88be9dd4d8..35803c454f 100644 --- a/tycho-source-plugin/src/main/java/org/eclipse/tycho/source/SourceFeatureMojo.java +++ b/tycho-source-plugin/src/main/java/org/eclipse/tycho/source/SourceFeatureMojo.java @@ -54,12 +54,13 @@ import org.eclipse.tycho.PackagingType; import org.eclipse.tycho.TargetEnvironment; import org.eclipse.tycho.TargetPlatform; +import org.eclipse.tycho.TychoConstants; +import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; import org.eclipse.tycho.core.resolver.P2ResolutionResult; import org.eclipse.tycho.core.resolver.P2ResolutionResult.Entry; import org.eclipse.tycho.core.resolver.P2Resolver; import org.eclipse.tycho.core.resolver.P2ResolverFactory; -import org.eclipse.tycho.core.utils.TychoProjectUtils; import org.eclipse.tycho.model.Feature; import org.eclipse.tycho.model.FeatureRef; import org.eclipse.tycho.model.PluginRef; @@ -224,6 +225,9 @@ public enum MissingSourcesAction { @Component private Logger logger; + @Component + private TychoProjectManager projectManager; + @Override public void execute() throws MojoExecutionException, MojoFailureException { if (!PackagingType.TYPE_ECLIPSE_FEATURE.equals(project.getPackaging()) || skip) { @@ -321,8 +325,8 @@ private File generateSourceFeatureXml(Properties mergedSourceFeatureProps, Prope final Feature sourceFeature = createSourceFeatureSkeleton(feature, mergedSourceFeatureProps, sourceTemplateProps); - fillReferences(sourceFeature, feature, - TychoProjectUtils.getTargetPlatform(DefaultReactorProject.adapt(project))); + fillReferences(sourceFeature, feature, projectManager.getTargetPlatform(project) + .orElseThrow(() -> new MojoExecutionException(TychoConstants.TYCHO_NOT_CONFIGURED + project))); Feature.write(sourceFeature, sourceFeatureXml, " "); return sourceFeatureXml; diff --git a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/AbstractEclipseTestMojo.java b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/AbstractEclipseTestMojo.java index 93274ac480..ca3159fd6d 100644 --- a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/AbstractEclipseTestMojo.java +++ b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/AbstractEclipseTestMojo.java @@ -797,7 +797,9 @@ public Collection getAdditionalRequirements() { } }; - DependencyArtifacts testRuntimeArtifacts = dependencyResolver.resolveDependencies(session, project, null, + DependencyArtifacts testRuntimeArtifacts = dependencyResolver.resolveDependencies(session, project, + projectManager.getTargetPlatform(project) + .orElseThrow(() -> new MojoExecutionException(TychoConstants.TYCHO_NOT_CONFIGURED + project)), getReactorProjects(), resolverConfiguration, getTestTargetEnvironments()); if (testRuntimeArtifacts == null) { throw new MojoExecutionException( diff --git a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/BndTestMojo.java b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/BndTestMojo.java index 1de099f5bf..54aa31856c 100644 --- a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/BndTestMojo.java +++ b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/BndTestMojo.java @@ -56,7 +56,6 @@ import org.eclipse.tycho.core.osgitools.OsgiManifest; import org.eclipse.tycho.core.osgitools.OsgiManifestParserException; import org.eclipse.tycho.core.resolver.shared.PomDependencies; -import org.eclipse.tycho.core.utils.TychoProjectUtils; import org.eclipse.tycho.model.classpath.JUnitBundle; import org.eclipse.tycho.model.classpath.JUnitClasspathContainerEntry; import org.eclipse.tycho.surefire.bnd.ArtifactKeyRepository; @@ -244,7 +243,10 @@ protected void runTests(ScanResult scanResult) throws MojoExecutionException, Mo for (RepositoryPlugin rp : workspace.getRepositories()) { workspace.removeBasicPlugin(rp); } - workspace.addBasicPlugin(new TargetPlatformRepository(getReactorProject())); + projectManager.getTargetPlatform(project).ifPresent(tp -> { + workspace.addBasicPlugin(new TargetPlatformRepository(getReactorProject(), tp)); + }); + if (scanResult instanceof BundleScanResult bundleScanResult) { workspace.addBasicPlugin(new ArtifactKeyRepository(bundleScanResult.pomBundles, "pom-dependencies", project.getFile())); @@ -487,24 +489,28 @@ protected BundleScanResult scanForTests() { } } } - TargetPlatform targetPlatform = TychoProjectUtils.getTargetPlatform(getReactorProject()); if (bundles != null) { - for (String bundle : bundles) { - try { - ArtifactKey key = targetPlatform.resolveArtifact(ArtifactType.TYPE_ECLIPSE_PLUGIN, bundle, - Version.emptyVersion.toString()); - File file = targetPlatform.getArtifactLocation(key); - if (file != null) { - OsgiManifest manifest = bundleReader.loadManifest(file); - String header = manifest.getValue(TychoConstants.HEADER_TESTCASES); - for (String test : getDeclaredTests(header, key)) { - bundleTestCases.add(test); + TargetPlatform targetPlatform = projectManager.getTargetPlatform(project).orElse(null); + if (targetPlatform == null) { + getLog().warn("No target platform, can't resolve additionally specified bundles!"); + } else { + for (String bundle : bundles) { + try { + ArtifactKey key = targetPlatform.resolveArtifact(ArtifactType.TYPE_ECLIPSE_PLUGIN, bundle, + Version.emptyVersion.toString()); + File file = targetPlatform.getArtifactLocation(key); + if (file != null) { + OsgiManifest manifest = bundleReader.loadManifest(file); + String header = manifest.getValue(TychoConstants.HEADER_TESTCASES); + for (String test : getDeclaredTests(header, key)) { + bundleTestCases.add(test); + } } + } catch (DependencyResolutionException | IllegalArtifactReferenceException + | OsgiManifestParserException e) { + //nothing we can use... + getLog().debug("Bundle " + bundle + " was not found in target platform: " + e); } - } catch (DependencyResolutionException | IllegalArtifactReferenceException - | OsgiManifestParserException e) { - //nothing we can use... - getLog().debug("Bundle " + bundle + " was not found in target platform: " + e); } } } diff --git a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/bnd/TargetPlatformRepository.java b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/bnd/TargetPlatformRepository.java index 3cbbf6150e..880ebfd452 100644 --- a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/bnd/TargetPlatformRepository.java +++ b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/bnd/TargetPlatformRepository.java @@ -31,7 +31,6 @@ import org.eclipse.tycho.ArtifactType; import org.eclipse.tycho.ReactorProject; import org.eclipse.tycho.TargetPlatform; -import org.eclipse.tycho.core.utils.TychoProjectUtils; import org.eclipse.tycho.osgi.resource.InstallableUnitResource; import org.osgi.resource.Capability; import org.osgi.resource.Requirement; @@ -46,9 +45,9 @@ public class TargetPlatformRepository extends ResourcesRepository implements Rep private ReactorProject reactorProject; private TargetPlatform targetPlatform; - public TargetPlatformRepository(ReactorProject reactorProject) { + public TargetPlatformRepository(ReactorProject reactorProject, TargetPlatform targetPlatform) { this.reactorProject = reactorProject; - this.targetPlatform = TychoProjectUtils.getTargetPlatform(reactorProject); + this.targetPlatform = targetPlatform; IArtifactRepository artifactRepository = targetPlatform.getArtifactRepository(); Set allUnits = targetPlatform.getMetadataRepository().query(QueryUtil.ALL_UNITS, null) .toSet(); @@ -112,7 +111,6 @@ public PutResult put(InputStream stream, PutOptions options) throws Exception { @Override public SortedSet versions(String bsn) throws Exception { SortedSet set = new TreeSet<>(); - TargetPlatform targetPlatform = TychoProjectUtils.getTargetPlatformIfAvailable(reactorProject); for (IInstallableUnit iu : targetPlatform.getMetadataRepository().query(QueryUtil.createIUQuery(bsn), null)) { for (IArtifactKey artifactKey : iu.getArtifacts()) { if (BundlesAction.OSGI_BUNDLE_CLASSIFIER.equals(artifactKey.getClassifier())) { diff --git a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolver.java b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolver.java index 7261dac1ce..12afcbf124 100644 --- a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolver.java +++ b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetPlatformArtifactResolver.java @@ -92,23 +92,7 @@ public Optional getReactorTargetFile(String groupId, String artifactId, St if (groupId.equals(project.getGroupId()) && artifactId.equals(project.getArtifactId()) && version.equals(project.getVersion())) { if (classifier == null || classifier.isBlank()) { - File[] targetFiles = TargetDefinitionFile - .listTargetFiles(project.getBasedir()); - if (targetFiles == null || targetFiles.length == 0) { - throw new TargetResolveException( - "No target definition file(s) found in project '" + project.getName() + "'."); - } - if (targetFiles.length == 1) { - return Optional.of(targetFiles[0]); - } - for (File targetFile : targetFiles) { - String baseName = FilenameUtils.getBaseName(targetFile.getName()); - if (baseName.equalsIgnoreCase(project.getArtifactId())) { - return Optional.of(targetFile); - } - } - throw new TargetResolveException("One target file must be named '" + project.getArtifactId() - + TargetDefinitionFile.FILE_EXTENSION + "' when multiple targets are present"); + return Optional.of(getMainTargetFile(project)); } else { File target = new File(project.getBasedir(), classifier + TargetDefinitionFile.FILE_EXTENSION); @@ -124,4 +108,24 @@ public Optional getReactorTargetFile(String groupId, String artifactId, St return Optional.empty(); } + public static File getMainTargetFile(MavenProject project) throws TargetResolveException { + File[] targetFiles = TargetDefinitionFile + .listTargetFiles(project.getBasedir()); + if (targetFiles == null || targetFiles.length == 0) { + throw new TargetResolveException( + "No target definition file(s) found in project '" + project.getName() + "'."); + } + if (targetFiles.length == 1) { + return targetFiles[0]; + } + for (File targetFile : targetFiles) { + String baseName = FilenameUtils.getBaseName(targetFile.getName()); + if (baseName.equalsIgnoreCase(project.getArtifactId())) { + return targetFile; + } + } + throw new TargetResolveException("One target file must be named '" + project.getArtifactId() + + TargetDefinitionFile.FILE_EXTENSION + "' when multiple targets are present"); + } + }