From 87f8dd23b910dda7256ab9530e89d655c0aaadff 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 +- .../tycho/core/DependencyResolver.java | 4 +- .../core/maven/TychoWorkspaceReader.java | 38 +++--- .../core/osgitools/AbstractTychoProject.java | 4 +- .../core/osgitools/DefaultReactorProject.java | 3 + .../core/osgitools/MavenBundleResolver.java | 3 +- .../core/osgitools/OsgiBundleProject.java | 4 +- .../DefaultTargetPlatformService.java | 61 +++------ .../core/resolver/P2ResolverFactory.java | 4 - .../tycho/core/utils/TychoProjectUtils.java | 33 ----- .../framework/EclipseApplicationFactory.java | 7 +- .../DefaultTargetPlatformFactory.java | 124 ++++++++++++++++++ .../p2resolver/P2DependencyResolver.java | 31 ++--- .../p2resolver/P2ResolverFactoryImpl.java | 43 +----- .../tycho/p2resolver/P2ResolverImpl.java | 10 +- .../PreliminaryTargetPlatformImpl.java | 8 +- .../PublisherServiceFactoryImpl.java | 43 ++---- .../p2tools/RepositoryReferenceTool.java | 5 +- .../CustomEEResolutionHandlerTest.java | 6 +- ...FeatureRootfileArtifactRepositoryTest.java | 3 +- .../tycho/test/util/TestResolverFactory.java | 6 - .../tycho/eclipserun/EclipseRunMojo.java | 10 +- .../extras/eclipserun/EclipseRunMojoTest.java | 3 +- .../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 +++--- 32 files changed, 309 insertions(+), 288 deletions(-) create mode 100644 tycho-core/src/main/java/org/eclipse/tycho/p2resolver/DefaultTargetPlatformFactory.java 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/DependencyResolver.java b/tycho-core/src/main/java/org/eclipse/tycho/core/DependencyResolver.java index 93aa52c8f0..8a27115d4f 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/DependencyResolver.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/DependencyResolver.java @@ -33,9 +33,11 @@ public interface DependencyResolver { PomDependencyCollector resolvePomDependencies(MavenSession session, MavenProject project); - public TargetPlatform computePreliminaryTargetPlatform(MavenSession session, MavenProject project, + public TargetPlatform computePreliminaryTargetPlatform(ReactorProject project, List reactorProjects); + public TargetPlatform computePreliminaryTargetPlatform(MavenProject project, List reactorProjects); + /** * @param targetPlatform * The candidate artifacts which may be used to resolve dependencies. If 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/AbstractTychoProject.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractTychoProject.java index f0e060e379..23997dac09 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractTychoProject.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractTychoProject.java @@ -81,8 +81,8 @@ public DependencyArtifacts getDependencyArtifacts(ReactorProject reactorProject) MavenSession mavenSession = getMavenSession(reactorProject); MavenProject mavenProject = getMavenProject(reactorProject); List reactorProjects = DefaultReactorProject.adapt(mavenSession); - TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenSession, - mavenProject, reactorProjects); + TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenProject, + reactorProjects); TargetPlatformConfiguration configuration = projectManager.getTargetPlatformConfiguration(mavenProject); DependencyResolverConfiguration resolverConfiguration = configuration.getDependencyResolverConfiguration(); DependencyArtifacts dependencyArtifacts = dependencyResolver.resolveDependencies(mavenSession, mavenProject, diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultReactorProject.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultReactorProject.java index 14c7d0ac83..ce4c200273 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultReactorProject.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultReactorProject.java @@ -104,6 +104,9 @@ protected static ReactorProject getCachedValue(MavenProject project) { } public static List adapt(MavenSession session) { + if (session == null) { + return List.of(); + } ArrayList result = new ArrayList<>(); for (MavenProject project : session.getProjects()) { ReactorProject reactorProject = adapt(project, session); 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/osgitools/OsgiBundleProject.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java index d60817829d..73b4eb723b 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java @@ -712,8 +712,8 @@ public Collection getAdditionalRequirements() { return resolverConfiguration.getAdditionalRequirements(); } }; - TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenSession, - mavenProject, reactorProjects); + TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenProject, + reactorProjects); return dependencyResolver.resolveDependencies(mavenSession, mavenProject, preliminaryTargetPlatform, reactorProjects, testResolverConfiguration, configuration.getEnvironments()); }); 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..2bc9c666ff 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 @@ -55,10 +55,12 @@ public class DefaultTargetPlatformService implements TargetPlatformService { private DependencyResolver dependencyResolver; @Requirement - ReactorRepositoryManager repositoryManager; + private ReactorRepositoryManager repositoryManager; + + @Requirement + private P2ResolverFactory p2ResolverFactory; @Requirement - P2ResolverFactory p2ResolverFactory; private TargetPlatformFactory tpFactory; @Override @@ -76,11 +78,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 +89,7 @@ public Optional getTargetPlatform(ReactorProject project) throws TargetPlatform finalTargetPlatform = computeFinalTargetPlatform(project, upstreamProjects, pomDependenciesCollector); return Optional.ofNullable(finalTargetPlatform); - } + }); } /** @@ -106,31 +104,16 @@ public Optional getTargetPlatform(ReactorProject project) throws */ private TargetPlatform computeFinalTargetPlatform(ReactorProject project, List upstreamProjects, PomDependencyCollector pomDependencyCollector) { - synchronized (project) { - PreliminaryTargetPlatformImpl preliminaryTargetPlatform = getRegisteredPreliminaryTargetPlatform(project); - if (preliminaryTargetPlatform == null) { - MavenSession session = project.adapt(MavenSession.class); - if (session == null) { - session = legacySupport.getSession(); - if (session == null) { - return null; - } - } - MavenProject mavenProject = project.adapt(MavenProject.class); - if (mavenProject == null) { - return null; - } - preliminaryTargetPlatform = (PreliminaryTargetPlatformImpl) dependencyResolver - .computePreliminaryTargetPlatform(session, mavenProject, DefaultReactorProject.adapt(session)); - - } - List upstreamProjectResults = getBuildResults(upstreamProjects); - TargetPlatform result = getTpFactory().createTargetPlatformWithUpdatedReactorContent( - preliminaryTargetPlatform, upstreamProjectResults, pomDependencyCollector); - - project.setContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY, result); - return result; + MavenSession session = project.adapt(MavenSession.class); + if (session == null) { + session = legacySupport.getSession(); } + PreliminaryTargetPlatformImpl preliminaryTargetPlatform = (PreliminaryTargetPlatformImpl) dependencyResolver + .computePreliminaryTargetPlatform(project, DefaultReactorProject.adapt(session)); + List upstreamProjectResults = getBuildResults(upstreamProjects); + TargetPlatform result = tpFactory.createTargetPlatformWithUpdatedReactorContent(preliminaryTargetPlatform, + upstreamProjectResults, pomDependencyCollector); + return result; } private List getBuildResults(List projects) { @@ -141,20 +124,6 @@ private List getBuildResults(List getReferencedTychoProjects(ReactorProject reactorProject) throws DependencyResolutionException { List result = new ArrayList<>(); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/P2ResolverFactory.java b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/P2ResolverFactory.java index 5043476319..541dbf214d 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/P2ResolverFactory.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/P2ResolverFactory.java @@ -21,7 +21,6 @@ import org.eclipse.tycho.ReactorProject; import org.eclipse.tycho.TargetEnvironment; import org.eclipse.tycho.p2.target.facade.PomDependencyCollector; -import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory; public interface P2ResolverFactory { @@ -31,9 +30,6 @@ public interface P2ResolverFactory { // TODO move to a PomDependencyCollectorFactory interface? public PomDependencyCollector newPomDependencyCollector(ReactorProject project); - // TODO directly register as service - public TargetPlatformFactory getTargetPlatformFactory(); - /** * Create a resolver for the given environments * 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/osgi/framework/EclipseApplicationFactory.java b/tycho-core/src/main/java/org/eclipse/tycho/osgi/framework/EclipseApplicationFactory.java index d6155e4407..1cc18105ed 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/osgi/framework/EclipseApplicationFactory.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/osgi/framework/EclipseApplicationFactory.java @@ -36,6 +36,7 @@ import org.eclipse.tycho.core.resolver.P2ResolverFactory; import org.eclipse.tycho.core.resolver.shared.IncludeSourceMode; import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub; +import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory; /** * Component that resolves all the bundles that make up an Eclipse Application to run from a given @@ -51,6 +52,9 @@ public class EclipseApplicationFactory { @Requirement private P2ResolverFactory resolverFactory; + @Requirement + private TargetPlatformFactory platformFactory; + @Requirement private Logger logger; @@ -88,8 +92,7 @@ public TargetPlatform createTargetPlatform(Collection l ExecutionEnvironmentConfiguration eeConfiguration = new ExecutionEnvironmentConfigurationImpl(logger, false, toolchainManager, mavenSession); eeConfiguration.setProfileConfiguration("JavaSE-" + javaVersion, "tycho-eclipse-application-resolver"); - TargetPlatform targetPlatform = resolverFactory.getTargetPlatformFactory().createTargetPlatform(tpConfiguration, - eeConfiguration, null); + TargetPlatform targetPlatform = platformFactory.createTargetPlatform(tpConfiguration, eeConfiguration, null); return targetPlatform; } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/DefaultTargetPlatformFactory.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/DefaultTargetPlatformFactory.java new file mode 100644 index 0000000000..665cbc3fea --- /dev/null +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/DefaultTargetPlatformFactory.java @@ -0,0 +1,124 @@ +/******************************************************************************* + * Copyright (c) 2008, 2023 Sonatype Inc. 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: + * Sonatype Inc. - initial API and implementation + * Christoph Läubrich - Bug 567098 - pomDependencies=consider should wrap non-osgi jars + * Issue #443 - Use regular Maven coordinates -when possible- for dependencies + * Issue #822 - If multiple fragments match a bundle all items are added to the classpath while only the one with the highest version should match + * Extracted into DefaultTargetPlatformFactory + *******************************************************************************/ +package org.eclipse.tycho.p2resolver; + +import java.io.File; +import java.util.List; + +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.eclipse.equinox.p2.core.IProvisioningAgent; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; +import org.eclipse.tycho.ExecutionEnvironmentConfiguration; +import org.eclipse.tycho.IRepositoryIdManager; +import org.eclipse.tycho.ReactorProject; +import org.eclipse.tycho.TargetPlatform; +import org.eclipse.tycho.core.TychoProjectManager; +import org.eclipse.tycho.core.osgitools.MavenBundleResolver; +import org.eclipse.tycho.core.shared.MavenContext; +import org.eclipse.tycho.p2.repository.LocalArtifactRepository; +import org.eclipse.tycho.p2.repository.LocalMetadataRepository; +import org.eclipse.tycho.p2.repository.LocalRepositoryP2Indices; +import org.eclipse.tycho.p2.repository.LocalRepositoryReader; +import org.eclipse.tycho.p2.repository.RepositoryReader; +import org.eclipse.tycho.p2.target.facade.PomDependencyCollector; +import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub; +import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory; + +@Component(role = TargetPlatformFactory.class) +public class DefaultTargetPlatformFactory implements TargetPlatformFactory { + + // TODO cache these instances in an p2 agent, and not here + private LocalMetadataRepository localMetadataRepository; + private LocalArtifactRepository localArtifactRepository; + + @Requirement + private IProvisioningAgent agent; + + @Requirement + private MavenContext mavenContext; + + @Requirement + private LocalRepositoryP2Indices localRepoIndices; + + @Requirement + private IRepositoryIdManager repositoryIdManager; + + @Requirement + private MavenBundleResolver bundleResolver; + + @Requirement + private TychoProjectManager projectManager; + + @Requirement + private TargetDefinitionResolverService targetDefinitionResolverService; + private TargetPlatformFactoryImpl impl; + + @Override + public TargetPlatform createTargetPlatform(TargetPlatformConfigurationStub tpConfiguration, + ExecutionEnvironmentConfiguration eeConfiguration, List reactorProjects, + ReactorProject project) { + // TODO Auto-generated method stub + return getImpl().createTargetPlatform(tpConfiguration, eeConfiguration, reactorProjects, project); + } + + @Override + public TargetPlatform createTargetPlatformWithUpdatedReactorContent(TargetPlatform baseTargetPlatform, + List upstreamProjectResults, PomDependencyCollector pomDependencies) { + return getImpl().createTargetPlatformWithUpdatedReactorContent(baseTargetPlatform, upstreamProjectResults, + pomDependencies); + } + + private TargetPlatformFactory getImpl() { + if (impl == null) { + // TODO should be plexus-components! + LocalMetadataRepository localMetadataRepo = getLocalMetadataRepository(mavenContext, localRepoIndices); + LocalArtifactRepository localArtifactRepo = getLocalArtifactRepository(mavenContext, localRepoIndices); + //TODO merge the impl here... + impl = new TargetPlatformFactoryImpl(mavenContext, agent, localArtifactRepo, localMetadataRepo, + targetDefinitionResolverService, repositoryIdManager, projectManager, bundleResolver); + } + return impl; + } + + private synchronized LocalMetadataRepository getLocalMetadataRepository(MavenContext context, + LocalRepositoryP2Indices localRepoIndices) { + if (localMetadataRepository == null) { + File localMavenRepoRoot = context.getLocalRepositoryRoot(); + RepositoryReader contentLocator = new LocalRepositoryReader(context); + localMetadataRepository = new LocalMetadataRepository(getAgent(), localMavenRepoRoot.toURI(), + localRepoIndices.getMetadataIndex(), contentLocator); + + } + return localMetadataRepository; + } + + private synchronized LocalArtifactRepository getLocalArtifactRepository(MavenContext mavenContext, + LocalRepositoryP2Indices localRepoIndices) { + if (localArtifactRepository == null) { + RepositoryReader contentLocator = new LocalRepositoryReader(mavenContext); + localArtifactRepository = new LocalArtifactRepository(getAgent(), localRepoIndices, contentLocator); + } + return localArtifactRepository; + } + + private IProvisioningAgent getAgent() { + //force triggering service loads... just in case not initialized yet ... + agent.getService(IArtifactRepositoryManager.class); + return agent; + } +} 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..3bc498e27a 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; @@ -140,6 +140,7 @@ public class P2DependencyResolver implements DependencyResolver, Initializable { @Requirement private MavenDependenciesResolver dependenciesResolver; + @Requirement private TargetPlatformFactory tpFactory; @Requirement @@ -195,9 +196,14 @@ protected Collection getDependencyMetadata(final MavenSessi } @Override - public TargetPlatform computePreliminaryTargetPlatform(MavenSession session, MavenProject project, + public TargetPlatform computePreliminaryTargetPlatform(MavenProject project, List reactorProjects) { + return computePreliminaryTargetPlatform(DefaultReactorProject.adapt(project), reactorProjects); + } + + @Override + public TargetPlatform computePreliminaryTargetPlatform(ReactorProject reactorProject, List reactorProjects) { - ReactorProject reactorProject = DefaultReactorProject.adapt(project); + MavenProject project = reactorProject.adapt(MavenProject.class); return reactorProject.computeContextValue(TargetPlatform.PRELIMINARY_TARGET_PLATFORM_KEY, () -> { logger.debug("Computing preliminary target platform for " + project); TargetPlatformConfiguration configuration = projectManager.getTargetPlatformConfiguration(project); @@ -218,19 +224,12 @@ public TargetPlatform computePreliminaryTargetPlatform(MavenSession session, Mav tpConfiguration .setIgnoreLocalArtifacts(configuration.getIgnoreLocalArtifacts() == LocalArtifactHandling.ignore); tpConfiguration.setReferencedRepositoryMode(configuration.getReferencedRepositoryMode()); - TargetPlatform result = getTpFactory().createTargetPlatform(tpConfiguration, ee, reactorProjects, + TargetPlatform result = tpFactory.createTargetPlatform(tpConfiguration, ee, reactorProjects, reactorProject); return result; }); } - private synchronized TargetPlatformFactory getTpFactory() { - if (tpFactory == null) { - tpFactory = resolverFactory.getTargetPlatformFactory(); - } - return tpFactory; - } - private ReactorProject getThisReactorProject(MavenSession session, MavenProject project, TargetPlatformConfiguration configuration) { // 'this' project should obey optionalDependencies configuration @@ -306,17 +305,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/P2ResolverFactoryImpl.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2ResolverFactoryImpl.java index 48ad6bfd0d..ae112056b5 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2ResolverFactoryImpl.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2ResolverFactoryImpl.java @@ -15,7 +15,6 @@ *******************************************************************************/ package org.eclipse.tycho.p2resolver; -import java.io.File; import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; import java.util.Collection; @@ -53,20 +52,13 @@ import org.eclipse.tycho.core.resolver.P2ResolverFactory; import org.eclipse.tycho.core.shared.MavenContext; 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.LocalRepositoryP2Indices; -import org.eclipse.tycho.p2.repository.LocalRepositoryReader; -import org.eclipse.tycho.p2.repository.RepositoryReader; import org.eclipse.tycho.p2.target.facade.PomDependencyCollector; +import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory; @Component(role = P2ResolverFactory.class) public class P2ResolverFactoryImpl implements P2ResolverFactory { - // TODO cache these instances in an p2 agent, and not here - private LocalMetadataRepository localMetadataRepository; - private LocalArtifactRepository localArtifactRepository; - @Requirement IProvisioningAgent agent; @@ -93,26 +85,8 @@ public class P2ResolverFactoryImpl implements P2ResolverFactory { @Requirement private MavenBundleResolver bundleResolver; - private synchronized LocalMetadataRepository getLocalMetadataRepository(MavenContext context, - LocalRepositoryP2Indices localRepoIndices) { - if (localMetadataRepository == null) { - File localMavenRepoRoot = context.getLocalRepositoryRoot(); - RepositoryReader contentLocator = new LocalRepositoryReader(context); - localMetadataRepository = new LocalMetadataRepository(getAgent(), localMavenRepoRoot.toURI(), - localRepoIndices.getMetadataIndex(), contentLocator); - - } - return localMetadataRepository; - } - - private synchronized LocalArtifactRepository getLocalArtifactRepository(MavenContext mavenContext, - LocalRepositoryP2Indices localRepoIndices) { - if (localArtifactRepository == null) { - RepositoryReader contentLocator = new LocalRepositoryReader(mavenContext); - localArtifactRepository = new LocalArtifactRepository(getAgent(), localRepoIndices, contentLocator); - } - return localArtifactRepository; - } + @Requirement + private TargetPlatformFactory targetPlatformFactory; private IProvisioningAgent getAgent() { //force triggering service loads... just in case not initialized yet ... @@ -129,18 +103,9 @@ public PomDependencyCollector newPomDependencyCollector(ReactorProject project) return new PomDependencyCollectorImpl(logger, project, getAgent()); } - @Override - public TargetPlatformFactoryImpl getTargetPlatformFactory() { - // TODO should be plexus-components! - LocalMetadataRepository localMetadataRepo = getLocalMetadataRepository(mavenContext, localRepoIndices); - LocalArtifactRepository localArtifactRepo = getLocalArtifactRepository(mavenContext, localRepoIndices); - return new TargetPlatformFactoryImpl(mavenContext, agent, localArtifactRepo, localMetadataRepo, - targetDefinitionResolverService, repositoryIdManager, projectManager, bundleResolver); - } - @Override public P2Resolver createResolver(Collection environments) { - return new P2ResolverImpl(getTargetPlatformFactory(), this, mavenContext.getLogger(), environments); + return new P2ResolverImpl(targetPlatformFactory, this, mavenContext.getLogger(), environments); } public Set calculateDependencyFragments(ResolutionData data, diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2ResolverImpl.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2ResolverImpl.java index 3a5cb2b1ff..9e4337a1c3 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2ResolverImpl.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2ResolverImpl.java @@ -75,6 +75,7 @@ import org.eclipse.tycho.p2.repository.QueryableCollection; import org.eclipse.tycho.p2.resolver.ResolverException; import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub; +import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory; import org.eclipse.tycho.targetplatform.P2TargetPlatform; public class P2ResolverImpl implements P2Resolver { @@ -88,13 +89,13 @@ public class P2ResolverImpl implements P2Resolver { private final List additionalRequirements = new ArrayList<>(); - private TargetPlatformFactoryImpl targetPlatformFactory; + private TargetPlatformFactory targetPlatformFactory; private PomDependencies pomDependencies = PomDependencies.ignore; private P2ResolverFactoryImpl p2ResolverFactoryImpl; - public P2ResolverImpl(TargetPlatformFactoryImpl targetPlatformFactory, P2ResolverFactoryImpl p2ResolverFactoryImpl, + public P2ResolverImpl(TargetPlatformFactory targetPlatformFactory, P2ResolverFactoryImpl p2ResolverFactoryImpl, MavenLogger logger, Collection environments) { this.targetPlatformFactory = targetPlatformFactory; this.p2ResolverFactoryImpl = p2ResolverFactoryImpl; @@ -162,7 +163,8 @@ public Map resolveArtifactDependencies(Ta @Override public P2ResolutionResult resolveMetadata(TargetPlatformConfigurationStub tpConfiguration, ExecutionEnvironmentConfiguration eeConfig) { - P2TargetPlatform contextImpl = targetPlatformFactory.createTargetPlatform(tpConfiguration, eeConfig, null); + P2TargetPlatform contextImpl = (P2TargetPlatform) targetPlatformFactory.createTargetPlatform(tpConfiguration, + eeConfig, null); ResolutionDataImpl data = new ResolutionDataImpl(contextImpl.getEEResolutionHints()); data.setAvailableIUs(contextImpl.getInstallableUnits()); @@ -189,7 +191,7 @@ public P2ResolutionResult resolveMetadata(TargetPlatformConfigurationStub tpConf @Override public P2ResolutionResult getTargetPlatformAsResolutionResult(TargetPlatformConfigurationStub tpConfiguration, String eeName) { - P2TargetPlatform targetPlatform = targetPlatformFactory.createTargetPlatform(tpConfiguration, + P2TargetPlatform targetPlatform = (P2TargetPlatform) targetPlatformFactory.createTargetPlatform(tpConfiguration, new ExecutionEnvironmentConfigurationStub(eeName), null); MetadataOnlyP2ResolutionResult result = new MetadataOnlyP2ResolutionResult(); 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/p2resolver/PublisherServiceFactoryImpl.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/PublisherServiceFactoryImpl.java index 75b7cc1be4..0f9ec06724 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/PublisherServiceFactoryImpl.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/PublisherServiceFactoryImpl.java @@ -19,7 +19,7 @@ import org.eclipse.tycho.Interpolator; import org.eclipse.tycho.ReactorProject; import org.eclipse.tycho.TargetEnvironment; -import org.eclipse.tycho.TargetPlatform; +import org.eclipse.tycho.TargetPlatformService; import org.eclipse.tycho.core.shared.MavenContext; import org.eclipse.tycho.p2.repository.PublishingRepository; import org.eclipse.tycho.p2.tools.publisher.PublishProductToolImpl; @@ -39,9 +39,14 @@ public class PublisherServiceFactoryImpl implements PublisherServiceFactory { @Requirement private ReactorRepositoryManager reactorRepoManager; + @Requirement + private TargetPlatformService targetPlatformService; + @Override public PublisherService createPublisher(ReactorProject project, List environments) { - P2TargetPlatform targetPlatform = (P2TargetPlatform) getFinalTargetPlatform(project); + P2TargetPlatform targetPlatform = targetPlatformService.getTargetPlatform(project) + .filter(P2TargetPlatform.class::isInstance).map(P2TargetPlatform.class::cast) + .orElseThrow(() -> new IllegalStateException("Target platform is missing")); PublisherActionRunner publisherRunner = getPublisherRunnerForProject(targetPlatform, environments); PublishingRepository publishingRepository = reactorRepoManager.getPublishingRepository(project); @@ -51,7 +56,9 @@ public PublisherService createPublisher(ReactorProject project, List environments, String buildQualifier, Interpolator interpolator) { - P2TargetPlatform targetPlatform = (P2TargetPlatform) getFinalTargetPlatform(project); + P2TargetPlatform targetPlatform = targetPlatformService.getTargetPlatform(project) + .filter(P2TargetPlatform.class::isInstance).map(P2TargetPlatform.class::cast) + .orElseThrow(() -> new IllegalStateException("Target platform is missing")); PublisherActionRunner publisherRunner = getPublisherRunnerForProject(targetPlatform, environments); PublishingRepository publishingRepository = reactorRepoManager.getPublishingRepository(project); @@ -59,40 +66,10 @@ public PublishProductTool createProductPublisher(ReactorProject project, List environments) { - checkCollaborators(); - return new PublisherActionRunner(targetPlatform.getMetadataRepository(), environments, mavenContext.getLogger()); } - // setters for DS - - public void setMavenContext(MavenContext mavenContext) { - this.mavenContext = mavenContext; - } - - public void setReactorRepositoryManager(ReactorRepositoryManager reactorRepoManager) { - this.reactorRepoManager = reactorRepoManager; - } - - private void checkCollaborators() { - if (mavenContext == null || reactorRepoManager == null) { - throw new IllegalStateException(); // shoudn't happen; see OSGI-INF/publisherfactory.xml - } - } - } 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-core/src/test/java/org/eclipse/tycho/p2resolver/CustomEEResolutionHandlerTest.java b/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/CustomEEResolutionHandlerTest.java index 9eb6ab96f8..edbbbc37fe 100644 --- a/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/CustomEEResolutionHandlerTest.java +++ b/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/CustomEEResolutionHandlerTest.java @@ -24,17 +24,14 @@ import java.util.Collections; import java.util.List; -import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.tycho.ExecutionEnvironment; import org.eclipse.tycho.ExecutionEnvironmentConfiguration; import org.eclipse.tycho.SystemCapability; import org.eclipse.tycho.SystemCapability.Type; -import org.eclipse.tycho.core.resolver.MavenTargetLocationFactory; import org.eclipse.tycho.core.test.utils.ResourceUtil; import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub; import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory; import org.eclipse.tycho.test.util.LogVerifier; -import org.eclipse.tycho.test.util.TestResolverFactory; import org.eclipse.tycho.testing.TychoPlexusTestCase; import org.junit.Before; import org.junit.Rule; @@ -50,8 +47,7 @@ public class CustomEEResolutionHandlerTest extends TychoPlexusTestCase { @Before public void setUpContext() throws Exception { - tpFactory = new TestResolverFactory(logVerifier.getMavenLogger(), logVerifier.getLogger(), - lookup(IProvisioningAgent.class), lookup(MavenTargetLocationFactory.class)).getTargetPlatformFactory(); + tpFactory = lookup(TargetPlatformFactory.class); tpConfig = new TargetPlatformConfigurationStub(); } diff --git a/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/FeatureRootfileArtifactRepositoryTest.java b/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/FeatureRootfileArtifactRepositoryTest.java index 167717a83b..4389ce1917 100644 --- a/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/FeatureRootfileArtifactRepositoryTest.java +++ b/tycho-core/src/test/java/org/eclipse/tycho/p2resolver/FeatureRootfileArtifactRepositoryTest.java @@ -29,12 +29,13 @@ import org.eclipse.tycho.p2.metadata.IP2Artifact; import org.eclipse.tycho.p2.publisher.rootfiles.FeatureRootAdvice; import org.eclipse.tycho.p2maven.advices.MavenPropertiesAdvice; +import org.eclipse.tycho.testing.TychoPlexusTestCase; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -public class FeatureRootfileArtifactRepositoryTest { +public class FeatureRootfileArtifactRepositoryTest extends TychoPlexusTestCase { @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); diff --git a/tycho-core/src/test/java/org/eclipse/tycho/test/util/TestResolverFactory.java b/tycho-core/src/test/java/org/eclipse/tycho/test/util/TestResolverFactory.java index 3b797f536a..ce54c8a915 100644 --- a/tycho-core/src/test/java/org/eclipse/tycho/test/util/TestResolverFactory.java +++ b/tycho-core/src/test/java/org/eclipse/tycho/test/util/TestResolverFactory.java @@ -33,7 +33,6 @@ import org.eclipse.tycho.p2.repository.LocalMetadataRepository; import org.eclipse.tycho.p2.repository.LocalRepositoryP2Indices; import org.eclipse.tycho.p2.repository.LocalRepositoryReader; -import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory; import org.eclipse.tycho.p2resolver.DefaultTargetDefinitionVariableResolver; import org.eclipse.tycho.p2resolver.LocalRepositoryP2IndicesImpl; import org.eclipse.tycho.p2resolver.P2ResolverImpl; @@ -102,11 +101,6 @@ public PomDependencyCollectorImpl newPomDependencyCollector() { return newPomDependencyCollector(new ReactorProjectStub(new File("."), "test")); } - @Override - public TargetPlatformFactory getTargetPlatformFactory() { - return getTargetPlatformFactoryImpl(); - } - public TargetPlatformFactoryImpl getTargetPlatformFactoryImpl() { return new TargetPlatformFactoryImpl(mavenContext, agent, localArtifactRepo, localMetadataRepo, targetDefinitionResolverService, idManager, null, null); diff --git a/tycho-eclipse-plugin/src/main/java/org/eclipse/tycho/eclipserun/EclipseRunMojo.java b/tycho-eclipse-plugin/src/main/java/org/eclipse/tycho/eclipserun/EclipseRunMojo.java index 323a857298..c694169d93 100644 --- a/tycho-eclipse-plugin/src/main/java/org/eclipse/tycho/eclipserun/EclipseRunMojo.java +++ b/tycho-eclipse-plugin/src/main/java/org/eclipse/tycho/eclipserun/EclipseRunMojo.java @@ -57,6 +57,7 @@ import org.eclipse.tycho.core.resolver.P2Resolver; import org.eclipse.tycho.core.resolver.P2ResolverFactory; import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub; +import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory; /** * Launch an eclipse process with arbitrary commandline arguments. The eclipse @@ -265,6 +266,9 @@ public class EclipseRunMojo extends AbstractMojo { @Component private ToolchainManager toolchainManager; + @Component + private TargetPlatformFactory platformFactory; + public EclipseRunMojo() { // default constructor } @@ -278,7 +282,7 @@ public EclipseRunMojo(File work, boolean clearWorkspaceBeforeLaunch, MavenProjec List applicationArgs, int forkedProcessTimeoutInSeconds, Map environmentVariables, EquinoxInstallationFactory installationFactory, EquinoxLauncher launcher, ToolchainProvider toolchainProvider, P2ResolverFactory resolverFactory, Logger logger, - ToolchainManager toolchainManager) { + ToolchainManager toolchainManager, TargetPlatformFactory platformFactory) { this.work = work; this.clearWorkspaceBeforeLaunch = clearWorkspaceBeforeLaunch; this.project = project; @@ -298,6 +302,7 @@ public EclipseRunMojo(File work, boolean clearWorkspaceBeforeLaunch, MavenProjec this.resolverFactory = resolverFactory; this.logger = logger; this.toolchainManager = toolchainManager; + this.platformFactory = platformFactory; } @Override @@ -340,8 +345,7 @@ private EquinoxInstallation createEclipseInstallation() throws MojoFailureExcept ExecutionEnvironmentConfiguration eeConfiguration = new ExecutionEnvironmentConfigurationImpl(logger, false, toolchainManager, session); eeConfiguration.setProfileConfiguration(executionEnvironment, "tycho-eclipserun-plugin "); - TargetPlatform targetPlatform = resolverFactory.getTargetPlatformFactory().createTargetPlatform(tpConfiguration, - eeConfiguration, null); + TargetPlatform targetPlatform = platformFactory.createTargetPlatform(tpConfiguration, eeConfiguration, null); P2Resolver resolver = resolverFactory .createResolver(Collections.singletonList(TargetEnvironment.getRunningEnvironment())); for (Dependency dependency : dependencies) { diff --git a/tycho-eclipse-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java b/tycho-eclipse-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java index 8460d243a1..7a96921a98 100644 --- a/tycho-eclipse-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java +++ b/tycho-eclipse-plugin/src/test/java/org/eclipse/tycho/extras/eclipserun/EclipseRunMojoTest.java @@ -142,10 +142,11 @@ public void testExecutionEnvironmentIsRespectedDuringDependencyResolution() thro P2Resolver mockP2Resolver = mock(P2Resolver.class); P2ResolverFactory mockP2ResolverFactory = mock(P2ResolverFactory.class); - when(mockP2ResolverFactory.getTargetPlatformFactory()).thenReturn(mockTargetPlatformFactory); +// when(mockP2ResolverFactory.getTargetPlatformFactory()).thenReturn(mockTargetPlatformFactory); when(mockP2ResolverFactory.createResolver(any())).thenReturn(mockP2Resolver); setVariableValueToObject(runMojo, "resolverFactory", mockP2ResolverFactory); + setVariableValueToObject(runMojo, "platformFactory", mockTargetPlatformFactory); setVariableValueToObject(runMojo, "executionEnvironment", "custom-ee"); 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"); + } + }