Skip to content

Commit e0eaac0

Browse files
committed
Do not update to the next major version unless explicitly requested
Currently the update-target mojo just updates to the highest version, but in some cases it is desirable to not update to the next "major" version. This adds a new parameter to prevent major updates unless explicitly requested.
1 parent 6af35ac commit e0eaac0

File tree

4 files changed

+101
-16
lines changed

4 files changed

+101
-16
lines changed

tycho-core/src/main/java/org/eclipse/tycho/core/maven/MavenDependenciesResolver.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.HashSet;
1818
import java.util.Iterator;
1919
import java.util.List;
20+
import java.util.Objects;
2021
import java.util.Set;
2122

2223
import org.apache.maven.RepositoryUtils;
@@ -49,6 +50,7 @@
4950
import org.eclipse.aether.resolution.VersionRangeResolutionException;
5051
import org.eclipse.aether.resolution.VersionRangeResult;
5152
import org.eclipse.aether.version.Version;
53+
import org.eclipse.tycho.TychoConstants;
5254

5355
@Component(role = MavenDependenciesResolver.class)
5456
public class MavenDependenciesResolver {
@@ -104,14 +106,10 @@ public Collection<org.apache.maven.artifact.Artifact> resolve(MavenProject proje
104106
DependencyNode rootNode = collectResult.getRoot();
105107

106108
CumulativeScopeArtifactFilter scopeArtifactFilter = new CumulativeScopeArtifactFilter(scopesToResolve);
107-
DependencyRequest dependencyRequest = new DependencyRequest(collect, new DependencyFilter() {
109+
DependencyRequest dependencyRequest = new DependencyRequest(collect, (DependencyFilter) (node, parents) -> {
108110

109-
@Override
110-
public boolean accept(DependencyNode node, List<DependencyNode> parents) {
111-
112-
Artifact artifact = RepositoryUtils.toArtifact(node.getArtifact());
113-
return artifact != null && scopeArtifactFilter.include(artifact);
114-
}
111+
Artifact artifact = RepositoryUtils.toArtifact(node.getArtifact());
112+
return artifact != null && scopeArtifactFilter.include(artifact);
115113
});
116114
dependencyRequest.setRoot(rootNode);
117115

@@ -169,8 +167,9 @@ public Artifact resolveHighestVersion(MavenProject project, MavenSession session
169167
if (version.endsWith(".0)")) {
170168
version = version.substring(0, version.length() - 3) + ")";
171169
}
170+
String typeId = Objects.requireNonNullElse(dependency.getType(), TychoConstants.JAR_EXTENSION);
172171
DefaultArtifact artifact = new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(),
173-
stereotypes.get(dependency.getType()).getExtension(), version);
172+
stereotypes.get(typeId).getExtension(), version);
174173
VersionRangeRequest request = new VersionRangeRequest(artifact, project.getRemoteProjectRepositories(), null);
175174
VersionRangeResult versionResult = repoSystem.resolveVersionRange(repositorySession, request);
176175
for (Iterator<Version> iterator = versionResult.getVersions().iterator(); iterator.hasNext();) {

tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/UpdateTargetMojo.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.maven.plugins.annotations.Parameter;
4040
import org.eclipse.aether.resolution.ArtifactResolutionException;
4141
import org.eclipse.aether.resolution.VersionRangeResolutionException;
42+
import org.eclipse.aether.version.InvalidVersionSpecificationException;
4243
import org.eclipse.tycho.TargetEnvironment;
4344
import org.eclipse.tycho.core.maven.MavenDependenciesResolver;
4445
import org.eclipse.tycho.core.resolver.P2ResolutionResult;
@@ -64,6 +65,7 @@
6465
*/
6566
@Mojo(name = "update-target")
6667
public class UpdateTargetMojo extends AbstractUpdateMojo {
68+
6769
@Parameter(property = "target")
6870
private File targetFile;
6971

@@ -76,9 +78,19 @@ public class UpdateTargetMojo extends AbstractUpdateMojo {
7678
@Component
7779
private MavenSession mavenSession;
7880

81+
/**
82+
* If specified also update to new major versions of the dependency otherwise only perform
83+
* minor, micro or "qualifier" changes, please note that for maven locations the semantic might
84+
* be slightly different as maven does not follow OSGi version scheme, in this case we interpret
85+
* the first part of the version as the major version.
86+
*/
87+
@Parameter(property = "major", defaultValue = "true")
88+
private boolean updateMajorVersion;
89+
7990
@Override
8091
protected void doUpdate() throws IOException, URISyntaxException, ParserConfigurationException,
81-
TargetResolveException, MojoFailureException, VersionRangeResolutionException, ArtifactResolutionException {
92+
TargetResolveException, MojoFailureException, VersionRangeResolutionException, ArtifactResolutionException,
93+
InvalidVersionSpecificationException {
8294
File file = getFileToBeUpdated();
8395
getLog().info("Update target file " + file);
8496
//we use the descent xml parser here because we need to retain the formating of the original file
@@ -127,10 +139,24 @@ protected void doUpdate() throws IOException, URISyntaxException, ParserConfigur
127139
mavenDependency.setVersion(getElementValue("version", dependency));
128140
mavenDependency.setType(getElementValue("type", dependency));
129141
mavenDependency.setClassifier(getElementValue("classifier", dependency));
130-
Artifact highestVersionArtifact = resolver.resolveHighestVersion(project, mavenSession,
142+
String oldVersion = mavenDependency.getVersion();
143+
if (!updateMajorVersion) {
144+
try {
145+
String[] strings = oldVersion.split("\\.");
146+
mavenDependency.setVersion("[," + (Integer.parseInt(strings[0]) + 1) + ")");
147+
} catch (RuntimeException e) {
148+
getLog().warn("Can't check for update of " + mavenDependency
149+
+ " because the version format is not parseable: " + e);
150+
continue;
151+
}
152+
}
153+
Artifact newArtifactVersion = resolver.resolveHighestVersion(project, mavenSession,
131154
mavenDependency);
132-
String newVersion = highestVersionArtifact.getVersion();
133-
if (newVersion.equals(mavenDependency.getVersion())) {
155+
if (newArtifactVersion == null) {
156+
continue;
157+
}
158+
String newVersion = newArtifactVersion.getVersion();
159+
if (newVersion.equals(oldVersion)) {
134160
getLog().debug(mavenDependency + " is already up-to date");
135161
} else {
136162
changed = true;

tycho-its/projects/tycho-version-bump-plugin/update-target/update-target.target

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
<version>1.2</version>
1818
<type>jar</type>
1919
</dependency>
20+
<dependency>
21+
<groupId>jakarta.annotation</groupId>
22+
<artifactId>jakarta.annotation-api</artifactId>
23+
<version>1.3.5</version>
24+
<type>jar</type>
25+
</dependency>
26+
<dependency>
27+
<groupId>jakarta.annotation</groupId>
28+
<artifactId>jakarta.annotation-api</artifactId>
29+
<version>2.0.0</version>
30+
<type>jar</type>
31+
</dependency>
2032
</dependencies>
2133
</location>
2234
</locations>

tycho-its/src/test/java/org/eclipse/tycho/test/VersionBumpPluginTest.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313
package org.eclipse.tycho.test;
1414

1515
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertTrue;
1617
import static org.junit.Assert.fail;
1718

1819
import java.io.File;
1920
import java.io.FileInputStream;
2021
import java.util.Collection;
2122
import java.util.List;
2223
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
2325

2426
import org.apache.maven.it.Verifier;
27+
import org.eclipse.aether.util.version.GenericVersionScheme;
28+
import org.eclipse.aether.version.Version;
29+
import org.eclipse.aether.version.VersionScheme;
2530
import org.eclipse.tycho.targetplatform.TargetDefinition.InstallableUnitLocation;
2631
import org.eclipse.tycho.targetplatform.TargetDefinition.Location;
2732
import org.eclipse.tycho.targetplatform.TargetDefinition.MavenDependency;
@@ -39,7 +44,6 @@ public void testUpdateTarget() throws Exception {
3944
Verifier verifier = getVerifier("tycho-version-bump-plugin/update-target", false, true);
4045
String sourceTargetFile = "update-target.target";
4146
verifier.setSystemProperty("target", sourceTargetFile);
42-
verifier.setSystemProperty("tycho.localArtifacts", "ignore");
4347
verifier.executeGoal("org.eclipse.tycho.extras:tycho-version-bump-plugin:" + TychoVersion.getTychoVersion()
4448
+ ":update-target");
4549
verifier.verifyErrorFreeLog();
@@ -61,13 +65,57 @@ public void testUpdateTarget() throws Exception {
6165
MavenGAVLocation maven = locations.stream().filter(MavenGAVLocation.class::isInstance)
6266
.map(MavenGAVLocation.class::cast).findFirst()
6367
.orElseThrow(() -> new AssertionError("Maven Location not found!"));
64-
Collection<MavenDependency> roots = maven.getRoots();
65-
assertEquals(1, roots.size());
66-
MavenDependency dependency = roots.iterator().next();
68+
MavenDependency dependency = dependencies(maven, "javax.annotation", "javax.annotation-api").findFirst()
69+
.orElseThrow(() -> new AssertionError("javax.annotation dependency not found"));
6770
assertEquals("Maven version was not updated correctly in " + targetFile, "1.3.2", dependency.getVersion());
71+
List<MavenDependency> list = dependencies(maven, "jakarta.annotation", "jakarta.annotation-api").toList();
72+
assertEquals(2, list.size());
73+
VersionScheme scheme = new GenericVersionScheme();
74+
// we can not know the exact latest major version, but we know it must be larger
75+
// than 3.0
76+
Version version3 = scheme.parseVersion("3");
77+
assertTrue("Maven version was not updated correctly in " + targetFile + " for jakarta.annotation-api 1.3.5",
78+
scheme.parseVersion(list.get(0).getVersion()).compareTo(version3) >= 0);
79+
assertTrue(
80+
"No Update for Maven version was expected in " + targetFile + " for jakarta.annotation-api 2.0.0",
81+
scheme.parseVersion(list.get(1).getVersion()).compareTo(version3) >= 0);
6882
}
6983
}
7084

85+
@Test
86+
public void testUpdateTargetWithoutMajor() throws Exception {
87+
Verifier verifier = getVerifier("tycho-version-bump-plugin/update-target", false, true);
88+
String sourceTargetFile = "update-target.target";
89+
verifier.setSystemProperty("target", sourceTargetFile);
90+
verifier.setSystemProperty("major", "false");
91+
verifier.executeGoal("org.eclipse.tycho.extras:tycho-version-bump-plugin:" + TychoVersion.getTychoVersion()
92+
+ ":update-target");
93+
verifier.verifyErrorFreeLog();
94+
File targetFile = new File(verifier.getBasedir(), sourceTargetFile);
95+
try (FileInputStream input = new FileInputStream(targetFile)) {
96+
Document target = TargetDefinitionFile.parseDocument(input);
97+
TargetDefinitionFile parsedTarget = TargetDefinitionFile.parse(target, targetFile.getAbsolutePath());
98+
List<? extends Location> locations = parsedTarget.getLocations();
99+
MavenGAVLocation maven = locations.stream().filter(MavenGAVLocation.class::isInstance)
100+
.map(MavenGAVLocation.class::cast).findFirst()
101+
.orElseThrow(() -> new AssertionError("Maven Location not found!"));
102+
List<MavenDependency> list = dependencies(maven, "jakarta.annotation", "jakarta.annotation-api").toList();
103+
assertEquals(2, list.size());
104+
assertEquals(
105+
"No Update for Maven version was expected in " + targetFile + " for jakarta.annotation-api 1.3.5",
106+
"1.3.5", list.get(0).getVersion());
107+
assertEquals(
108+
"Maven version was not updated correctly in " + targetFile + " for jakarta.annotation-api 2.0.0",
109+
"2.1.1", list.get(1).getVersion());
110+
111+
}
112+
}
113+
114+
private Stream<MavenDependency> dependencies(MavenGAVLocation maven, String g, String a) {
115+
Collection<MavenDependency> roots = maven.getRoots();
116+
return roots.stream().filter(md -> md.getGroupId().equals(g)).filter(md -> md.getArtifactId().equals(a));
117+
}
118+
71119
private void assertIUVersion(String id, String version, List<? extends Unit> units, File targetFile) {
72120
for (Unit unit : units) {
73121
if (unit.getId().equals(id) && unit.getVersion().equals(version)) {

0 commit comments

Comments
 (0)