From d4211135482326b713b2198d3ef8758a5d258590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Fri, 2 Feb 2024 12:45:55 +0100 Subject: [PATCH] Move code from mojo to generator and generate missing p2 data if needed --- .../tycho/p2/metadata/P2Generator.java | 14 +++ .../module/ModuleArtifactRepository.java | 2 +- .../module/ModuleMetadataRepository.java | 4 +- .../tycho/p2resolver/P2GeneratorImpl.java | 90 +++++++++++++++++++ .../ReactorRepositoryManagerImpl.java | 28 ++++++ .../p2tools/RepositoryReferenceTool.java | 7 ++ .../facade/ReactorRepositoryManager.java | 4 +- .../tycho/plugins/p2/P2MetadataMojo.java | 83 ++--------------- 8 files changed, 150 insertions(+), 82 deletions(-) diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2/metadata/P2Generator.java b/tycho-core/src/main/java/org/eclipse/tycho/p2/metadata/P2Generator.java index 7b3d3b97ba..c8cdafdb87 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2/metadata/P2Generator.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2/metadata/P2Generator.java @@ -17,6 +17,7 @@ import java.util.List; import java.util.Map; +import org.apache.maven.project.MavenProject; import org.eclipse.tycho.IArtifactFacade; public interface P2Generator { @@ -32,4 +33,17 @@ public Map generateMetadata(List artifacts File targetDir) throws IOException; void persistMetadata(Map metadata, File unitsXml, File artifactsXml) throws IOException; + + FileInfo persistMetadata(Map metadata, MavenProject project) throws IOException; + + void writeArtifactLocations(MavenProject project) throws IOException; + + Map generateMetadata(MavenProject project, boolean generateDownloadStatsProperty, + boolean generateChecksums) throws IOException; + + record FileInfo(File metadata, File artifacts) { + + } + + void generateMetaData(MavenProject mavenProject) throws IOException; } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2/repository/module/ModuleArtifactRepository.java b/tycho-core/src/main/java/org/eclipse/tycho/p2/repository/module/ModuleArtifactRepository.java index cefcb70d1a..3dd2e6fa25 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2/repository/module/ModuleArtifactRepository.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2/repository/module/ModuleArtifactRepository.java @@ -82,7 +82,7 @@ public class ModuleArtifactRepository extends ArtifactRepositoryBaseImpl generateMetadata(MavenProject project, boolean generateDownloadStatsProperty, + boolean generateChecksums) throws IOException { + File targetDir = new File(project.getBuild().getDirectory()); + ArtifactFacade projectDefaultArtifact = new ArtifactFacade(project.getArtifact()); + List artifacts = new ArrayList<>(); + + artifacts.add(projectDefaultArtifact); + + for (Artifact attachedArtifact : project.getAttachedArtifacts()) { + if (attachedArtifact.getFile() != null && (attachedArtifact.getFile().getName().endsWith(".jar") + || (attachedArtifact.getFile().getName().endsWith(".zip") + && project.getPackaging().equals(ArtifactType.TYPE_INSTALLABLE_UNIT)))) { + artifacts.add(new ArtifactFacade(attachedArtifact)); + } + } + + PublisherOptions options = new PublisherOptions(); + options.setGenerateDownloadStats(generateDownloadStatsProperty); + options.setGenerateChecksums(generateChecksums); + Map generatedMetadata = generateMetadata(artifacts, options, targetDir); + return generatedMetadata; + } + + @Override + public FileInfo persistMetadata(Map metadata, MavenProject project) throws IOException { + File targetDir = new File(project.getBuild().getDirectory()); + File contentsXml = new File(targetDir, TychoConstants.FILE_NAME_P2_METADATA); + File artifactsXml = new File(targetDir, TychoConstants.FILE_NAME_P2_ARTIFACTS); + persistMetadata(metadata, contentsXml, artifactsXml); + return new FileInfo(contentsXml, artifactsXml); + } + + @Override + public void writeArtifactLocations(MavenProject project) throws IOException { + File localArtifactsFile = new File(project.getBuild().getDirectory(), TychoConstants.FILE_NAME_LOCAL_ARTIFACTS); + writeArtifactLocations(localArtifactsFile, getAllProjectArtifacts(project)); + } + + static void writeArtifactLocations(File outputFile, Map artifactLocations) throws IOException { + Properties outputProperties = new Properties(); + + for (Entry entry : artifactLocations.entrySet()) { + if (entry.getKey() == null) { + outputProperties.put(TychoConstants.KEY_ARTIFACT_MAIN, entry.getValue().getAbsolutePath()); + } else { + outputProperties.put(TychoConstants.KEY_ARTIFACT_ATTACHED + entry.getKey(), + entry.getValue().getAbsolutePath()); + } + } + + writeProperties(outputProperties, outputFile); + } + + private static void writeProperties(Properties properties, File outputFile) throws IOException { + try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { + properties.store(outputStream, null); + } + } + + /** + * Returns a map from classifiers to artifact files of the given project. The classifier + * null is mapped to the project's main artifact. + */ + private static Map getAllProjectArtifacts(MavenProject project) { + Map artifacts = new HashMap<>(); + Artifact mainArtifact = project.getArtifact(); + if (mainArtifact != null) { + artifacts.put(null, mainArtifact.getFile()); + } + for (Artifact attachedArtifact : project.getAttachedArtifacts()) { + artifacts.put(attachedArtifact.getClassifier(), attachedArtifact.getFile()); + } + return artifacts; + } + + @Override + public void generateMetaData(MavenProject mavenProject) throws IOException { + //TODO we probably should get the active execution here and derive the data from the config of the p2 plugin that applies here + Map generatedMetadata = generateMetadata(mavenProject, false, false); + persistMetadata(generatedMetadata, mavenProject); + writeArtifactLocations(mavenProject); + } + } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/ReactorRepositoryManagerImpl.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/ReactorRepositoryManagerImpl.java index 37f45c2873..b34d7c9338 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/ReactorRepositoryManagerImpl.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/ReactorRepositoryManagerImpl.java @@ -13,11 +13,18 @@ *******************************************************************************/ package org.eclipse.tycho.p2resolver; +import java.io.File; + +import org.apache.maven.project.MavenProject; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.eclipse.equinox.p2.core.IProvisioningAgent; +import org.eclipse.tycho.ReactorProject; import org.eclipse.tycho.ReactorProjectIdentities; +import org.eclipse.tycho.p2.metadata.P2Generator; import org.eclipse.tycho.p2.repository.PublishingRepository; +import org.eclipse.tycho.p2.repository.module.ModuleArtifactRepository; +import org.eclipse.tycho.p2.repository.module.ModuleMetadataRepository; import org.eclipse.tycho.p2.repository.module.PublishingRepositoryImpl; import org.eclipse.tycho.repository.registry.facade.ReactorRepositoryManager; @@ -27,9 +34,30 @@ public class ReactorRepositoryManagerImpl implements ReactorRepositoryManager { @Requirement IProvisioningAgent agent; + @Requirement + P2Generator p2generator; + @Override public PublishingRepository getPublishingRepository(ReactorProjectIdentities project) { return new PublishingRepositoryImpl(agent, project); } + @Override + public PublishingRepository getPublishingRepository(ReactorProject project) { + + File targetDir = project.getBuildDirectory().getLocation(); + if (!ModuleMetadataRepository.canAttemptRead(targetDir) + || !ModuleArtifactRepository.canAttemptRead(targetDir)) { + //no metadata there so just generate it... + try { + agent.getService(Object.class); //needed to make checksum computation work see https://github.com/eclipse-equinox/p2/issues/214 + p2generator.generateMetaData(project.adapt(MavenProject.class)); + } catch (Exception e) { + // can't do anything then... + } + } + + return getPublishingRepository(project.getIdentities()); + } + } 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 ab4e28bc05..4a49f823bf 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 @@ -36,9 +36,11 @@ import org.eclipse.tycho.core.DependencyResolverConfiguration; import org.eclipse.tycho.core.TargetPlatformConfiguration; import org.eclipse.tycho.core.TychoProjectManager; +import org.eclipse.tycho.core.osgitools.DefaultReactorProject; import org.eclipse.tycho.p2.repository.RepositoryBlackboardKey; import org.eclipse.tycho.p2.resolver.ResolverException; import org.eclipse.tycho.p2.tools.RepositoryReferences; +import org.eclipse.tycho.repository.registry.facade.ReactorRepositoryManager; /** * Tool to obtain the list of p2 repositories that contain the dependencies of a module. @@ -60,6 +62,9 @@ public class RepositoryReferenceTool { @Requirement private TychoProjectManager projectManager; + @Requirement + private ReactorRepositoryManager repositoryManager; + /** * Returns the list of visible p2 repositories for the build of the current module. The list * includes the p2 repositories of the referenced reactor modules, the target platform, and @@ -90,6 +95,8 @@ public RepositoryReferences getVisibleRepositories(MavenProject module, MavenSes RepositoryReferences repositories = new RepositoryReferences(); if ((flags & REPOSITORIES_INCLUDE_CURRENT_MODULE) != 0) { + //This is to enforce the repository is there e.g. if no p2 metadata is generated yet it will init it now + repositoryManager.getPublishingRepository(DefaultReactorProject.adapt(module)); File publisherResults = new File(module.getBuild().getDirectory()); repositories.addMetadataRepository(publisherResults); repositories.addArtifactRepository(publisherResults); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/repository/registry/facade/ReactorRepositoryManager.java b/tycho-core/src/main/java/org/eclipse/tycho/repository/registry/facade/ReactorRepositoryManager.java index 94533d1a7c..0edffc59b4 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/repository/registry/facade/ReactorRepositoryManager.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/repository/registry/facade/ReactorRepositoryManager.java @@ -32,8 +32,6 @@ public interface ReactorRepositoryManager { */ PublishingRepository getPublishingRepository(ReactorProjectIdentities project); - default PublishingRepository getPublishingRepository(ReactorProject project) { - return getPublishingRepository(project.getIdentities()); - } + PublishingRepository getPublishingRepository(ReactorProject project); } diff --git a/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/P2MetadataMojo.java b/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/P2MetadataMojo.java index 68a9e91382..3dd612dc94 100644 --- a/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/P2MetadataMojo.java +++ b/tycho-p2-plugin/src/main/java/org/eclipse/tycho/plugins/p2/P2MetadataMojo.java @@ -13,16 +13,11 @@ package org.eclipse.tycho.plugins.p2; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; import java.util.Set; import org.apache.maven.artifact.Artifact; @@ -36,8 +31,6 @@ import org.apache.maven.project.MavenProjectHelper; import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.equinox.p2.metadata.IInstallableUnit; -import org.eclipse.tycho.ArtifactType; -import org.eclipse.tycho.IArtifactFacade; import org.eclipse.tycho.IDependencyMetadata.DependencyMetadataType; import org.eclipse.tycho.ReactorProject; import org.eclipse.tycho.TychoConstants; @@ -45,8 +38,7 @@ import org.eclipse.tycho.core.osgitools.DefaultReactorProject; import org.eclipse.tycho.p2.metadata.IP2Artifact; import org.eclipse.tycho.p2.metadata.P2Generator; -import org.eclipse.tycho.p2.metadata.PublisherOptions; -import org.eclipse.tycho.p2resolver.ArtifactFacade; +import org.eclipse.tycho.p2.metadata.P2Generator.FileInfo; @Mojo(name = "p2-metadata", threadSafe = true) public class P2MetadataMojo extends AbstractMojo { @@ -171,27 +163,9 @@ protected void attachP2Metadata() throws MojoExecutionException { agent.getService(Object.class); //needed to make checksum computation work see https://github.com/eclipse-equinox/p2/issues/214 - File targetDir = new File(project.getBuild().getDirectory()); - - ArtifactFacade projectDefaultArtifact = new ArtifactFacade(project.getArtifact()); - try { - List artifacts = new ArrayList<>(); - - artifacts.add(projectDefaultArtifact); - - for (Artifact attachedArtifact : project.getAttachedArtifacts()) { - if (attachedArtifact.getFile() != null && (attachedArtifact.getFile().getName().endsWith(".jar") - || (attachedArtifact.getFile().getName().endsWith(".zip") - && project.getPackaging().equals(ArtifactType.TYPE_INSTALLABLE_UNIT)))) { - artifacts.add(new ArtifactFacade(attachedArtifact)); - } - } - - PublisherOptions options = new PublisherOptions(); - options.setGenerateDownloadStats(generateDownloadStatsProperty); - options.setGenerateChecksums(generateChecksums); - Map generatedMetadata = p2generator.generateMetadata(artifacts, options, targetDir); + Map generatedMetadata = p2generator.generateMetadata(project, + generateDownloadStatsProperty, generateChecksums); if (baselineMode != BaselineMode.disable) { ComparisonData data = new ComparisonData(ignoredPatterns, writeComparatorDelta); @@ -199,13 +173,11 @@ protected void attachP2Metadata() throws MojoExecutionException { baselineRepositories, baselineMode, baselineReplace); } - File contentsXml = new File(targetDir, TychoConstants.FILE_NAME_P2_METADATA); - File artifactsXml = new File(targetDir, TychoConstants.FILE_NAME_P2_ARTIFACTS); - p2generator.persistMetadata(generatedMetadata, contentsXml, artifactsXml); + FileInfo info = p2generator.persistMetadata(generatedMetadata, project); attachArtifact(project, TychoConstants.EXTENSION_P2_METADATA, TychoConstants.CLASSIFIER_P2_METADATA, - contentsXml); + info.metadata()); attachArtifact(project, TychoConstants.EXTENSION_P2_ARTIFACTS, TychoConstants.CLASSIFIER_P2_ARTIFACTS, - artifactsXml); + info.artifacts()); ReactorProject reactorProject = DefaultReactorProject.adapt(project); @@ -223,12 +195,10 @@ protected void attachP2Metadata() throws MojoExecutionException { // TODO 353889 distinguish between dependency resolution seed units ("primary") and other units of the project reactorProject.setDependencyMetadata(DependencyMetadataType.SEED, installableUnits); reactorProject.setDependencyMetadata(DependencyMetadataType.RESOLVE, Collections.emptySet()); + p2generator.writeArtifactLocations(project); } catch (IOException e) { throw new MojoExecutionException("Could not generate P2 metadata", e); } - - File localArtifactsFile = new File(project.getBuild().getDirectory(), TychoConstants.FILE_NAME_LOCAL_ARTIFACTS); - writeArtifactLocations(localArtifactsFile, getAllProjectArtifacts(project)); } /** @@ -264,43 +234,4 @@ private static String getExtension(File file) { return fileName.substring(separator + 1); } - /** - * Returns a map from classifiers to artifact files of the given project. The classifier - * null is mapped to the project's main artifact. - */ - private static Map getAllProjectArtifacts(MavenProject project) { - Map artifacts = new HashMap<>(); - Artifact mainArtifact = project.getArtifact(); - if (mainArtifact != null) { - artifacts.put(null, mainArtifact.getFile()); - } - for (Artifact attachedArtifact : project.getAttachedArtifacts()) { - artifacts.put(attachedArtifact.getClassifier(), attachedArtifact.getFile()); - } - return artifacts; - } - - static void writeArtifactLocations(File outputFile, Map artifactLocations) - throws MojoExecutionException { - Properties outputProperties = new Properties(); - - for (Entry entry : artifactLocations.entrySet()) { - if (entry.getKey() == null) { - outputProperties.put(TychoConstants.KEY_ARTIFACT_MAIN, entry.getValue().getAbsolutePath()); - } else { - outputProperties.put(TychoConstants.KEY_ARTIFACT_ATTACHED + entry.getKey(), - entry.getValue().getAbsolutePath()); - } - } - - writeProperties(outputProperties, outputFile); - } - - private static void writeProperties(Properties properties, File outputFile) throws MojoExecutionException { - try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { - properties.store(outputStream, null); - } catch (IOException e) { - throw new MojoExecutionException("I/O exception while writing " + outputFile, e); - } - } }