From 9008e900cb05a3234dfa24f0917f66325a2ae6c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Wed, 10 Jan 2024 09:08:54 +0100 Subject: [PATCH] Update versions if the project references another pom as module --- .../bundle/META-INF/MANIFEST.MF | 5 ++ .../set-version/nested_modules/bundle/pom.xml | 15 ++++++ .../set-version/nested_modules/parent/pom.xml | 27 ++++++++++ .../set-version/nested_modules/pom.xml | 15 ++++++ .../TychoVersionsPluginTest.java | 27 ++++++++++ .../engine/VersionChangesDescriptor.java | 30 +++++++++-- .../tycho/versions/engine/VersionsEngine.java | 2 +- .../versions/manipulation/PomManipulator.java | 52 +++++++++++++++---- 8 files changed, 158 insertions(+), 15 deletions(-) create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/nested_modules/bundle/META-INF/MANIFEST.MF create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/nested_modules/bundle/pom.xml create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/nested_modules/parent/pom.xml create mode 100644 tycho-its/projects/tycho-version-plugin/set-version/nested_modules/pom.xml diff --git a/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/bundle/META-INF/MANIFEST.MF b/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/bundle/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..9521f38263 --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/bundle/META-INF/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Test Bundle +Bundle-SymbolicName: bundle +Bundle-Version: 1.0.0.qualifier diff --git a/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/bundle/pom.xml b/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/bundle/pom.xml new file mode 100644 index 0000000000..22d65d15b5 --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/bundle/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + + + org.tycho.its + parent + 1.0.0-SNAPSHOT + ../parent + + bundle + eclipse-plugin + diff --git a/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/parent/pom.xml b/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/parent/pom.xml new file mode 100644 index 0000000000..1af408c257 --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/parent/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + org.tycho.its + parent + 1.0.0-SNAPSHOT + pom + + ../bundle + + + 5.0.0-SNAPSHOT + + + + + org.eclipse.tycho + tycho-maven-plugin + ${tycho-version} + true + + + + diff --git a/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/pom.xml b/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/pom.xml new file mode 100644 index 0000000000..ee35d417a2 --- /dev/null +++ b/tycho-its/projects/tycho-version-plugin/set-version/nested_modules/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + + org.tycho.its + root + 1.0.0-SNAPSHOT + pom + + + parent + + + + diff --git a/tycho-its/src/test/java/org/eclipse/tycho/test/versionsplugin/TychoVersionsPluginTest.java b/tycho-its/src/test/java/org/eclipse/tycho/test/versionsplugin/TychoVersionsPluginTest.java index 40d6f407bf..1caae90fa7 100644 --- a/tycho-its/src/test/java/org/eclipse/tycho/test/versionsplugin/TychoVersionsPluginTest.java +++ b/tycho-its/src/test/java/org/eclipse/tycho/test/versionsplugin/TychoVersionsPluginTest.java @@ -17,10 +17,14 @@ import static org.junit.Assert.assertTrue; import java.io.File; +import java.io.FileInputStream; import java.io.FileReader; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.Properties; +import java.util.jar.JarFile; +import java.util.jar.Manifest; import org.apache.maven.it.Verifier; import org.apache.maven.model.Model; @@ -28,6 +32,7 @@ import org.eclipse.tycho.test.AbstractTychoIntegrationTest; import org.eclipse.tycho.version.TychoVersion; import org.junit.Test; +import org.osgi.framework.Constants; public class TychoVersionsPluginTest extends AbstractTychoIntegrationTest { @@ -99,6 +104,28 @@ public void updateProjectVersionBndTest() throws Exception { assertEquals("Bundle-Version is not as expected!", expectedNewVersion, versionProperty); } + @Test + public void updateProjectVersionWithNestedPom() throws Exception { + String expectedNewVersion = "1.1.0"; + + Verifier verifier = getVerifier("tycho-version-plugin/set-version/nested_modules", true); + + verifier.addCliOption("-DnewVersion=" + expectedNewVersion); + verifier.executeGoal("org.eclipse.tycho:tycho-versions-plugin:" + VERSION + ":set-version"); + + verifier.verifyErrorFreeLog(); + List poms = List.of("pom.xml", "parent/pom.xml", "bundle/pom.xml"); + for (String pom : poms) { + MavenXpp3Reader pomReader = new MavenXpp3Reader(); + Model pomModel = pomReader.read(new FileReader(new File(verifier.getBasedir(), pom))); + assertEquals(" in " + pom + " has not been changed!", expectedNewVersion, pomModel.getVersion()); + } + Manifest manifest = new Manifest( + new FileInputStream(new File(verifier.getBasedir(), "bundle/" + JarFile.MANIFEST_NAME))); + assertEquals("version in manifest was not updated!", expectedNewVersion, + manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION)); + } + @Test public void updateProjectMetadataVersionBndTest() throws Exception { String expectedNewVersion = "2.0.0.qualifier"; diff --git a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionChangesDescriptor.java b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionChangesDescriptor.java index 06f4c99bc6..52668c4f33 100644 --- a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionChangesDescriptor.java +++ b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionChangesDescriptor.java @@ -12,8 +12,15 @@ *******************************************************************************/ package org.eclipse.tycho.versions.engine; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Optional; import java.util.Set; /** @@ -26,12 +33,29 @@ public class VersionChangesDescriptor { private final Set packageVersionChanges; private final VersionRangeUpdateStrategy versionRangeUpdateStrategy; + private Collection projects; public VersionChangesDescriptor(Set originalVersionChanges, - VersionRangeUpdateStrategy versionRangeUpdateStrategy) { - this.aritfactsVersionChanges = new HashSet<>(originalVersionChanges); + VersionRangeUpdateStrategy versionRangeUpdateStrategy, Collection projects) { + this.projects = projects; + this.aritfactsVersionChanges = new LinkedHashSet<>(originalVersionChanges); this.versionRangeUpdateStrategy = versionRangeUpdateStrategy; - this.packageVersionChanges = new HashSet<>(); + this.packageVersionChanges = new LinkedHashSet<>(); + } + + public Optional findMetadataByBasedir(File baseDir) { + Path path = baseDir.toPath(); + for (ProjectMetadata meta : projects) { + Path projectPath = meta.getBasedir().toPath(); + try { + if (Files.isSameFile(projectPath, path)) { + return Optional.of(meta); + } + } catch (IOException e) { + } + } + return Optional.empty(); + } public Set getVersionChanges() { diff --git a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionsEngine.java b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionsEngine.java index b8aed076c9..ce8258f40e 100644 --- a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionsEngine.java +++ b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/engine/VersionsEngine.java @@ -112,7 +112,7 @@ public void reset() { public void apply() throws IOException { VersionChangesDescriptor versionChangeContext = new VersionChangesDescriptor(originalVersionChanges, - new DefaultVersionRangeUpdateStrategy(updateVersionRangeMatchingBounds)); + new DefaultVersionRangeUpdateStrategy(updateVersionRangeMatchingBounds), projects); // collecting secondary changes boolean newChanges = true; diff --git a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/manipulation/PomManipulator.java b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/manipulation/PomManipulator.java index 8977e9928b..699c17086b 100644 --- a/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/manipulation/PomManipulator.java +++ b/tycho-versions-plugin/src/main/java/org/eclipse/tycho/versions/manipulation/PomManipulator.java @@ -23,6 +23,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -43,9 +45,11 @@ @Component(role = MetadataManipulator.class, hint = PomManipulator.HINT) public class PomManipulator extends AbstractMetadataManipulator { + private static final String POM = "pom"; + private static final String NULL = ""; - public static final String HINT = "pom"; + public static final String HINT = POM; private static final Pattern CI_FRIENDLY_EXPRESSION = Pattern.compile("\\$\\{(.+?)\\}"); @@ -53,20 +57,47 @@ public class PomManipulator extends AbstractMetadataManipulator { public boolean addMoreChanges(ProjectMetadata project, VersionChangesDescriptor versionChangeContext) { PomFile pom = project.getMetadata(PomFile.class); if (pom == null) { - throw new RuntimeException("no pom avaiable for " + project.getBasedir()); + throw new RuntimeException("no pom available for " + project.getBasedir()); } GAV parent = pom.getParent(); - boolean moreChanges = false; - for (PomVersionChange change : versionChangeContext.getVersionChanges()) { - if (parent != null && isGavEquals(parent, change)) { - if (isVersionEquals(pom.getVersion(), change.getVersion())) { - moreChanges |= versionChangeContext - .addVersionChange(new PomVersionChange(pom, change.getVersion(), change.getNewVersion())); + AtomicBoolean moreChanges = new AtomicBoolean(); + if (parent != null) { + for (PomVersionChange change : versionChangeContext.getVersionChanges()) { + if (isGavEquals(parent, change)) { + if (isVersionEquals(pom.getVersion(), change.getVersion())) { + if (versionChangeContext.addVersionChange( + new PomVersionChange(pom, change.getVersion(), change.getNewVersion()))) { + moreChanges.set(true); + } + } + } + } + } + //if we are about to change we need to check the submodule + if (POM.equals(pom.getPackaging())) { + Optional thisChange = versionChangeContext.getVersionChanges().stream() + .filter(change -> change.getProject() == pom).findFirst(); + if (thisChange.isPresent()) { + PomVersionChange change = thisChange.get(); + List modules = pom.getModules(); + for (String module : modules) { + versionChangeContext.findMetadataByBasedir(new File(project.getBasedir(), module)) + .ifPresent(moduleMeta -> { + PomFile modulePom = moduleMeta.getMetadata(PomFile.class); + if (modulePom != null && modulePom.isMutable() + && POM.equals(modulePom.getPackaging())) { + if (versionChangeContext.addVersionChange( + new PomVersionChange(modulePom, change.getNewVersion()))) { + moreChanges.set(true); + } + } + }); } } } - return moreChanges; + + return moreChanges.get(); } @Override @@ -115,10 +146,9 @@ public void applyChanges(ProjectMetadata project, VersionChangesDescriptor versi pom.setVersion(newVersion); } } else { - GAV parent = pom.getParent(); if (parent != null && isGavEquals(parent, change) && !isCiFriendly(parent.getVersion())) { - logger.info(" %s//project/version: %s => %s".formatted(pomName, version, newVersion)); + logger.info(" %s//project/parent/version: %s => %s".formatted(pomName, version, newVersion)); parent.setVersion(newVersion); } }