Skip to content

Commit

Permalink
Update versions if the project references another pom as module
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Jan 10, 2024
1 parent 86db29d commit f5968e4
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test Bundle
Bundle-SymbolicName: bundle
Bundle-Version: 1.0.0.qualifier
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.tycho.its</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
<artifactId>bundle</artifactId>
<packaging>eclipse-plugin</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.tycho.its</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../bundle</module>
</modules>
<properties>
<tycho-version>5.0.0-SNAPSHOT</tycho-version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.tycho.its</groupId>
<artifactId>root</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>parent</module>
</modules>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,27 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
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;
import org.apache.maven.model.Parent;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
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 {

Expand Down Expand Up @@ -99,6 +106,38 @@ 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();
String bundlePom = "bundle/pom.xml";
List<String> poms = List.of("pom.xml", "parent/pom.xml", bundlePom);
for (String pom : poms) {
MavenXpp3Reader pomReader = new MavenXpp3Reader();
Model pomModel = pomReader.read(new FileReader(new File(verifier.getBasedir(), pom)));
if (bundlePom.equals(pom)) {
assertNull("child should inherit version from parent", pomModel.getVersion());
Parent parent = pomModel.getParent();
assertNotNull("project > parent is null", parent);
assertEquals("project > parent > version in " + pom + " has not been changed!", expectedNewVersion,
parent.getVersion());
} else {
assertEquals("project > version 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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -26,12 +33,29 @@ public class VersionChangesDescriptor {
private final Set<PackageVersionChange> packageVersionChanges;

private final VersionRangeUpdateStrategy versionRangeUpdateStrategy;
private Collection<ProjectMetadata> projects;

public VersionChangesDescriptor(Set<PomVersionChange> originalVersionChanges,
VersionRangeUpdateStrategy versionRangeUpdateStrategy) {
this.aritfactsVersionChanges = new HashSet<>(originalVersionChanges);
VersionRangeUpdateStrategy versionRangeUpdateStrategy, Collection<ProjectMetadata> projects) {
this.projects = projects;
this.aritfactsVersionChanges = new LinkedHashSet<>(originalVersionChanges);
this.versionRangeUpdateStrategy = versionRangeUpdateStrategy;
this.packageVersionChanges = new HashSet<>();
this.packageVersionChanges = new LinkedHashSet<>();
}

public Optional<ProjectMetadata> 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<PomVersionChange> getVersionChanges() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -43,30 +45,59 @@

@Component(role = MetadataManipulator.class, hint = PomManipulator.HINT)
public class PomManipulator extends AbstractMetadataManipulator {
private static final String POM = "pom";

private static final String NULL = "<null>";

public static final String HINT = "pom";
public static final String HINT = POM;

private static final Pattern CI_FRIENDLY_EXPRESSION = Pattern.compile("\\$\\{(.+?)\\}");

@Override
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<PomVersionChange> thisChange = versionChangeContext.getVersionChanges().stream()
.filter(change -> change.getProject() == pom).findFirst();
if (thisChange.isPresent()) {
PomVersionChange change = thisChange.get();
List<String> 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
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>targetplatform</groupId>
<artifactId>targetplatform</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<packaging>pom</packaging>

<build>
Expand Down

0 comments on commit f5968e4

Please sign in to comment.