From a9f59ade1f144ee7705446031de9bf5889eef118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sun, 10 Dec 2023 17:41:00 +0100 Subject: [PATCH] [backport] Refactor some usages of TychoProjectUtils --- .../org/eclipse/tycho/TychoConstants.java | 2 + .../tycho/compiler/ValidateClassPathMojo.java | 4 +- .../core/maven/TychoWorkspaceReader.java | 21 ++- .../core/osgitools/DefaultReactorProject.java | 3 + .../tycho/p2resolver/P2ResolverImpl.java | 10 +- .../PreliminaryTargetPlatformImpl.java | 8 +- .../p2tools/RepositoryReferenceTool.java | 5 +- .../plugins/p2/DependenciesTreeMojo.java | 17 +- .../p2/repository/AssembleRepositoryMojo.java | 163 +++++++++--------- .../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 ++-- 15 files changed, 188 insertions(+), 151 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 bda95f4351..e6aed98a95 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-12/"; + 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..17defb08c6 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); } 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/p2resolver/P2ResolverImpl.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2ResolverImpl.java index 3c95a4380a..fa0715103a 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.publisher.AuthoredIUAction; 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/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 c30aa20485..1bfd23cb81 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,8 +28,10 @@ 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; import org.eclipse.tycho.PackagingType; import org.eclipse.tycho.ReactorProject; import org.eclipse.tycho.TychoConstants; @@ -83,12 +85,11 @@ public static class RepositoryReferenceFilter { * relevant content, which is not provided by any other referenced repositories. If this is * set to {@code false} all automatically added references are added as they are available. */ - public boolean addOnlyProviding = false; + public boolean addOnlyProviding = true; /** The list of location patterns that exclude matching repository references. */ public List exclude = List.of(); } - private static final Object LOCK = new Object(); /** *

* By default, this goal creates a p2 repository. Set this to false if only a p2 @@ -243,8 +244,8 @@ public static class RepositoryReferenceFilter { * added to the assembled repository. An arbitrary number of patterns can be specified. *

*

- * If the sub-property {@code addOnlyProviding} is set to {@code true}, references to - * repositories that don't provide any relevant unit are excluded from being added to the + * If the sub-property {@code addOnlyProviding} is set to {@code true} (the default), references + * to repositories that don't provide any relevant unit are excluded from being added to the * assembled repository. *

*

@@ -314,91 +315,93 @@ public static class RepositoryReferenceFilter { @Component(role = TychoProject.class, hint = PackagingType.TYPE_ECLIPSE_REPOSITORY) private EclipseRepositoryProject eclipseRepositoryProject; + @Component + private FileLockService fileLockService; + @Override public void execute() throws MojoExecutionException, MojoFailureException { - synchronized (LOCK) { - try { - File destination = getAssemblyRepositoryLocation(); - destination.mkdirs(); - copyResources(destination); - - final ReactorProject reactorProject = getReactorProject(); - Collection projectSeeds = TychoProjectUtils.getDependencySeeds(reactorProject); - if (projectSeeds.isEmpty()) { - getLog().warn("No content specified for p2 repository"); - return; - } + File destination = getAssemblyRepositoryLocation(); + try (var locking = fileLockService.lockVirtually(destination)) { + destination.mkdirs(); + copyResources(destination); + + final ReactorProject reactorProject = getReactorProject(); + Collection projectSeeds = TychoProjectUtils.getDependencySeeds(reactorProject); + if (projectSeeds.isEmpty()) { + getLog().warn("No content specified for p2 repository"); + return; + } - reactorProject.setContextValue(TychoConstants.CTX_METADATA_ARTIFACT_LOCATION, categoriesDirectory); - RepositoryReferences sources = repositoryReferenceTool.getVisibleRepositories(getProject(), - getSession(), RepositoryReferenceTool.REPOSITORIES_INCLUDE_CURRENT_MODULE); - sources.setTargetPlatform(TychoProjectUtils.getTargetPlatform(getReactorProject())); - - List repositoryReferences = getCategories(categoriesDirectory).stream()// - .map(Category::getRepositoryReferences)// - .flatMap(List::stream)// - .map(ref -> new RepositoryReference(ref.getName(), ref.getLocation(), ref.isEnabled()))// - .toList(); - Predicate autoReferencesFilter = buildRepositoryReferenceLocationFilter(); - List autoRepositoryRefeferences = new ArrayList<>(); - if (addPomRepositoryReferences) { - getProject().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() - .flatMap(tpFile -> tpFile.getLocations().stream()) - .filter(InstallableUnitLocation.class::isInstance).map(InstallableUnitLocation.class::cast) - .flatMap(iu -> iu.getRepositories().stream()) - .map(iuRepo -> varResolver.resolve(iuRepo.getLocation())).filter(autoReferencesFilter) - .map(location -> new RepositoryReference(null, location, true)) - .forEach(autoRepositoryRefeferences::add); + reactorProject.setContextValue(TychoConstants.CTX_METADATA_ARTIFACT_LOCATION, categoriesDirectory); + MavenProject project = getProject(); + RepositoryReferences sources = repositoryReferenceTool.getVisibleRepositories(project, getSession(), + RepositoryReferenceTool.REPOSITORIES_INCLUDE_CURRENT_MODULE); + sources.setTargetPlatform(projectManager.getTargetPlatform(project) + .orElseThrow(() -> new MojoFailureException(TychoConstants.TYCHO_NOT_CONFIGURED + project))); + + List repositoryReferences = getCategories(categoriesDirectory).stream()// + .map(Category::getRepositoryReferences)// + .flatMap(List::stream)// + .map(ref -> new RepositoryReference(ref.getName(), ref.getLocation(), ref.isEnabled()))// + .toList(); + Predicate autoReferencesFilter = buildRepositoryReferenceLocationFilter(); + List autoRepositoryRefeferences = new ArrayList<>(); + if (addPomRepositoryReferences) { + 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(project).getTargets().stream() + .flatMap(tpFile -> tpFile.getLocations().stream()) + .filter(InstallableUnitLocation.class::isInstance).map(InstallableUnitLocation.class::cast) + .flatMap(iu -> iu.getRepositories().stream()) + .map(iuRepo -> varResolver.resolve(iuRepo.getLocation())).filter(autoReferencesFilter) + .map(location -> new RepositoryReference(null, location, true)) + .forEach(autoRepositoryRefeferences::add); + } + DestinationRepositoryDescriptor destinationRepoDescriptor = new DestinationRepositoryDescriptor(destination, + repositoryName, compress, xzCompress, keepNonXzIndexFiles, !createArtifactRepository, true, + extraArtifactRepositoryProperties, repositoryReferences, autoRepositoryRefeferences); + mirrorApp.mirrorReactor(sources, destinationRepoDescriptor, projectSeeds, getBuildContext(), + includeAllDependencies, includeAllSources, includeRequiredPlugins, includeRequiredFeatures, + filterProvided, repositoryReferenceFilter.addOnlyProviding, profileProperties); + if (generateOSGiRepository) { + XMLResourceGenerator resourceGenerator = new XMLResourceGenerator(); + resourceGenerator.name(repositoryName); + resourceGenerator.base(destination.toURI()); + File plugins = new File(destination, "plugins"); + if (plugins.isDirectory()) { + File[] files = plugins.listFiles(path -> path.getName().endsWith(".jar") && path.isFile()); + try { + resourceGenerator.repository(new FileSetRepository("plugins", Arrays.asList(files))); + } catch (Exception e) { + throw new MojoExecutionException("Could not read p2 repository plugins", e); + } } - DestinationRepositoryDescriptor destinationRepoDescriptor = new DestinationRepositoryDescriptor( - destination, repositoryName, compress, xzCompress, keepNonXzIndexFiles, - !createArtifactRepository, true, extraArtifactRepositoryProperties, repositoryReferences, - autoRepositoryRefeferences); - mirrorApp.mirrorReactor(sources, destinationRepoDescriptor, projectSeeds, getBuildContext(), - includeAllDependencies, includeAllSources, includeRequiredPlugins, includeRequiredFeatures, - filterProvided, repositoryReferenceFilter.addOnlyProviding, profileProperties); - if (generateOSGiRepository) { - XMLResourceGenerator resourceGenerator = new XMLResourceGenerator(); - resourceGenerator.name(repositoryName); - resourceGenerator.base(destination.toURI()); - File plugins = new File(destination, "plugins"); - if (plugins.isDirectory()) { - File[] files = plugins.listFiles(path -> path.getName().endsWith(".jar") && path.isFile()); + File features = new File(destination, "features"); + if (features.isDirectory()) { + File[] files = features.listFiles(path -> path.getName().endsWith(".jar") && path.isFile()); + for (File featureFile : files) { try { - resourceGenerator.repository(new FileSetRepository("plugins", Arrays.asList(files))); - } catch (Exception e) { - throw new MojoExecutionException("Could not read p2 repository plugins", e); - } - } - File features = new File(destination, "features"); - if (features.isDirectory()) { - File[] files = features.listFiles(path -> path.getName().endsWith(".jar") && path.isFile()); - for (File featureFile : files) { - try { - Feature feature = Feature.readJar(featureFile); - feature.toResource().forEach(resourceGenerator::resource); - } catch (IOException e) { - throw new MojoExecutionException("Could not read feature " + featureFile, e); - } + Feature feature = Feature.readJar(featureFile); + feature.toResource().forEach(resourceGenerator::resource); + } catch (IOException e) { + throw new MojoExecutionException("Could not read feature " + featureFile, e); } } - try { - String filename = compress ? repositoryFileName + ".gz" : repositoryFileName; - resourceGenerator.save(new File(destination, filename)); - } catch (IOException e) { - throw new MojoExecutionException("Could not write OSGi Repository!", e); - } } - } catch (FacadeException e) { - throw new MojoExecutionException("Could not assemble p2 repository", e); + try { + String filename = compress ? repositoryFileName + ".gz" : repositoryFileName; + resourceGenerator.save(new File(destination, filename)); + } catch (IOException e) { + throw new MojoExecutionException("Could not write OSGi Repository!", e); + } } + } catch (IOException | FacadeException e) { + throw new MojoExecutionException("Could not assemble p2 repository", e); } } 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 a8de90ce17..cf4e170897 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 1e1e141243..45c3f81f72 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"); + } + }