Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tycho-4.0.x] Support specify rulesets when updating maven target locations #4144

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tycho-extras/tycho-version-bump-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-core</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.mojo.versions</groupId>
<artifactId>versions-model</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo.versions</groupId>
<artifactId>versions-common</artifactId>
<version>2.17.1</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,87 @@
*******************************************************************************/
package org.eclipse.tycho.versionbump;

import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.wagon.Wagon;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.DefaultVersionsHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.api.VersionsHelper;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.eclipse.tycho.core.maven.MavenDependenciesResolver;
import org.eclipse.tycho.TychoConstants;

import de.pdark.decentxml.Element;

/**
* Supports updating of maven target locations
*/
@Named
public class MavenLocationUpdater {

@Inject
private MavenDependenciesResolver resolver;
protected ArtifactHandlerManager artifactHandlerManager;

public boolean update(Element mavenLocation, UpdateTargetMojo context)
throws VersionRangeResolutionException, ArtifactResolutionException {
@Inject
protected RepositorySystem repositorySystem;
@Inject
protected Map<String, Wagon> wagonMap;

boolean update(Element mavenLocation, UpdateTargetMojo context) throws VersionRangeResolutionException,
ArtifactResolutionException, MojoExecutionException, VersionRetrievalException {
VersionsHelper helper = getHelper(context);
boolean changed = false;
Element dependencies = mavenLocation.getChild("dependencies");
if (dependencies != null) {
for (Element dependency : dependencies.getChildren("dependency")) {
Dependency mavenDependency = getDependency(dependency);
String oldVersion = mavenDependency.getVersion();
if (!context.isUpdateMajorVersion()) {
try {
String[] strings = oldVersion.split("\\.");
mavenDependency.setVersion("[," + (Integer.parseInt(strings[0]) + 1) + "-alpha)");
} catch (RuntimeException e) {
context.getLog().warn("Can't check for update of " + mavenDependency
+ " because the version format is not parseable: " + e);
continue;
Artifact dependencyArtifact = helper.createDependencyArtifact(mavenDependency);
ArtifactVersions versions = helper.lookupArtifactVersions(dependencyArtifact, false);
ArtifactVersion updateVersion = versions.getNewestUpdateWithinSegment(context.getSegment(), false);
if (updateVersion != null) {
String oldVersion = mavenDependency.getVersion();
String newVersion = updateVersion.toString();
if (newVersion.equals(oldVersion)) {
context.getLog().debug(mavenDependency + " is already up-to date");
} else {
changed = true;
UpdateTargetMojo.setElementValue("version", newVersion, dependency);
context.getLog().info("update " + mavenDependency + " to version " + newVersion);
}
}
Artifact newArtifactVersion = resolver.resolveHighestVersion(context.getProject(),
context.getMavenSession(), mavenDependency);
if (newArtifactVersion == null) {
continue;
}
String newVersion = newArtifactVersion.getVersion();
if (newVersion.equals(oldVersion)) {
context.getLog().debug(mavenDependency + " is already up-to date");
} else {
changed = true;
UpdateTargetMojo.setElementValue("version", newVersion, dependency);
context.getLog().info("update " + mavenDependency + " to version " + newVersion);
}
}
}
return changed;
}

VersionsHelper getHelper(UpdateTargetMojo context) throws MojoExecutionException {
return new DefaultVersionsHelper.Builder().withArtifactHandlerManager(artifactHandlerManager)
.withRepositorySystem(repositorySystem).withWagonMap(wagonMap).withServerId("serverId")
.withRulesUri(context.getMavenRulesUri()).withRuleSet(context.getMavenRuleSet())
.withIgnoredVersions(context.getMavenIgnoredVersions()).withLog(context.getLog())
.withMavenSession(context.getMavenSession()).withMojoExecution(context.getMojoExecution()).build();
}

private static Dependency getDependency(Element dependency) {
Dependency mavenDependency = new Dependency();
mavenDependency.setGroupId(UpdateTargetMojo.getElementValue("groupId", dependency));
mavenDependency.setArtifactId(UpdateTargetMojo.getElementValue("artifactId", dependency));
mavenDependency.setVersion(UpdateTargetMojo.getElementValue("version", dependency));
mavenDependency.setType(UpdateTargetMojo.getElementValue("type", dependency));
mavenDependency.setClassifier(UpdateTargetMojo.getElementValue("classifier", dependency));
if (mavenDependency.getType() == null) {
mavenDependency.setType(TychoConstants.JAR_EXTENSION);
}
return mavenDependency;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import javax.inject.Inject;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.model.RuleSet;
import org.eclipse.tycho.targetplatform.TargetPlatformArtifactResolver;
import org.eclipse.tycho.targetplatform.TargetResolveException;

Expand All @@ -41,6 +50,16 @@
* <pre>
* mvn -f [path to target project] tycho-version-bump:update-target
* </pre>
* <p>
* For updating <b>maven target locations</b> the mojo support
* <a href="https://www.mojohaus.org/versions/versions-maven-plugin/version-rules.html">Version
* number comparison rule-sets</a> similar to the
* <a href="https://www.mojohaus.org/versions/versions-maven-plugin">Versions Maven Plugin</a>
* please check the documentation there for further information about ruleset files.
* </p>
* <p>
* For updating <b>installable unit locations</b> (also known as update sites)
* </p>
*/
@Mojo(name = "update-target")
public class UpdateTargetMojo extends AbstractUpdateMojo {
Expand All @@ -52,13 +71,24 @@ public class UpdateTargetMojo extends AbstractUpdateMojo {
private File targetFile;

/**
* If specified also update to new major versions of the dependency otherwise only perform
* minor, micro or "qualifier" changes, please note that for maven locations the semantic might
* be slightly different as maven does not follow OSGi version scheme, in this case we interpret
* the first part of the version as the major version.
* Whether to allow the major version number to be changed.
*/
@Parameter(property = "major", defaultValue = "true")
private boolean updateMajorVersion;
@Parameter(property = "allowMajorUpdates", defaultValue = "true")
private boolean allowMajorUpdates;

/**
* Whether to allow the minor version number to be changed.
*
*/
@Parameter(property = "allowMinorUpdates", defaultValue = "true")
private boolean allowMinorUpdates;

/**
* Whether to allow the incremental version number to be changed.
*
*/
@Parameter(property = "allowIncrementalUpdates", defaultValue = "true")
private boolean allowIncrementalUpdates;

/**
* A comma separated list of update site discovery strategies, the following is currently
Expand All @@ -71,6 +101,36 @@ public class UpdateTargetMojo extends AbstractUpdateMojo {
@Parameter(property = "discovery")
private String updateSiteDiscovery;

/**
* <p>
* Allows specifying a {@linkplain RuleSet} object describing rules on maven artifact versions
* to ignore when considering updates.
* </p>
*/
@Parameter
private RuleSet mavenRuleSet;

/**
* <p>
* Allows specifying ignored maven artifact versions as an alternative to providing a
* {@linkplain #mavenRuleSet} parameter.
* </p>
*/
@Parameter(property = "maven.version.ignore")
private Set<String> mavenIgnoredVersions;

/**
* URI of a ruleSet file containing the rules that control how to compare version numbers.
*/
@Parameter(property = "maven.version.rules")
private String mavenRulesUri;

@Component
private MavenSession mavenSession;

@Parameter(defaultValue = "${mojoExecution}", required = true, readonly = true)
private MojoExecution mojoExecution;

@Inject
private MavenLocationUpdater mavenLocationUpdater;

Expand Down Expand Up @@ -145,12 +205,68 @@ protected File getFileToBeUpdated() throws MojoFailureException {
}
}

boolean isUpdateMajorVersion() {
return updateMajorVersion;
boolean isAllowIncrementalUpdates() {
return allowIncrementalUpdates;
}

boolean isAllowMajorUpdates() {
return allowMajorUpdates;
}

boolean isAllowMinorUpdates() {
return allowMinorUpdates;
}

MavenSession getMavenSession() {
return mavenSession;
}

MojoExecution getMojoExecution() {
return mojoExecution;
}

String getUpdateSiteDiscovery() {
return updateSiteDiscovery;
}

Set<String> getMavenIgnoredVersions() {
return mavenIgnoredVersions;
}

RuleSet getMavenRuleSet() {
return mavenRuleSet;
}

String getMavenRulesUri() {
if (mavenRulesUri != null && !mavenRulesUri.isBlank()) {
try {
URI u = new URI(mavenRulesUri);
if (u.isAbsolute()) {
return mavenRulesUri;
}
} catch (URISyntaxException e) {
}
File fullPath = new File(mavenRulesUri);
if (fullPath.isFile()) {
return fullPath.toURI().toString();
} else {
File file = new File(getProject().getBasedir(), mavenRulesUri);
if (file.exists()) {
return file.toURI().toString();
}
}
}
return mavenRulesUri;
}

Optional<Segment> getSegment() {
if (isAllowMajorUpdates() && isAllowMinorUpdates() && isAllowIncrementalUpdates()) {
return Optional.empty();
}
if (isAllowMinorUpdates() && isAllowIncrementalUpdates()) {
return Optional.of(Segment.MINOR);
}
return Optional.of(Segment.INCREMENTAL);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<version>2.0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
</location>
</locations>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void testUpdateTargetWithoutMajor() throws Exception {
Verifier verifier = getVerifier("tycho-version-bump-plugin/update-target", false, true);
String sourceTargetFile = "update-target.target";
verifier.setSystemProperty("target", sourceTargetFile);
verifier.setSystemProperty("major", "false");
verifier.setSystemProperty("allowMajorUpdates", "false");
verifier.executeGoal("org.eclipse.tycho.extras:tycho-version-bump-plugin:" + TychoVersion.getTychoVersion()
+ ":update-target");
verifier.verifyErrorFreeLog();
Expand Down
Loading