diff --git a/tycho-extras/tycho-version-bump-plugin/pom.xml b/tycho-extras/tycho-version-bump-plugin/pom.xml index e77dee6c93..081f9b2253 100644 --- a/tycho-extras/tycho-version-bump-plugin/pom.xml +++ b/tycho-extras/tycho-version-bump-plugin/pom.xml @@ -41,6 +41,16 @@ org.eclipse.tycho tycho-core + + org.codehaus.mojo.versions + versions-model + 2.17.1 + + + org.codehaus.mojo.versions + versions-common + 2.17.1 + diff --git a/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/MavenLocationUpdater.java b/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/MavenLocationUpdater.java index c3e50b92ed..52e840f7c7 100644 --- a/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/MavenLocationUpdater.java +++ b/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/MavenLocationUpdater.java @@ -12,59 +12,77 @@ *******************************************************************************/ 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 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)); @@ -72,6 +90,9 @@ private static Dependency getDependency(Element 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; } diff --git a/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/UpdateTargetMojo.java b/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/UpdateTargetMojo.java index 49ebd64b61..1536b591e8 100644 --- a/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/UpdateTargetMojo.java +++ b/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/UpdateTargetMojo.java @@ -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; @@ -41,6 +50,16 @@ *
  * mvn -f [path to target project] tycho-version-bump:update-target
  * 
+ *

+ * For updating maven target locations the mojo support + * Version + * number comparison rule-sets similar to the + * Versions Maven Plugin + * please check the documentation there for further information about ruleset files. + *

+ *

+ * For updating installable unit locations (also known as update sites) + *

*/ @Mojo(name = "update-target") public class UpdateTargetMojo extends AbstractUpdateMojo { @@ -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 @@ -71,6 +101,36 @@ public class UpdateTargetMojo extends AbstractUpdateMojo { @Parameter(property = "discovery") private String updateSiteDiscovery; + /** + *

+ * Allows specifying a {@linkplain RuleSet} object describing rules on maven artifact versions + * to ignore when considering updates. + *

+ */ + @Parameter + private RuleSet mavenRuleSet; + + /** + *

+ * Allows specifying ignored maven artifact versions as an alternative to providing a + * {@linkplain #mavenRuleSet} parameter. + *

+ */ + @Parameter(property = "maven.version.ignore") + private Set 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; @@ -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 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 getSegment() { + if (isAllowMajorUpdates() && isAllowMinorUpdates() && isAllowIncrementalUpdates()) { + return Optional.empty(); + } + if (isAllowMinorUpdates() && isAllowIncrementalUpdates()) { + return Optional.of(Segment.MINOR); + } + return Optional.of(Segment.INCREMENTAL); + } + } diff --git a/tycho-its/projects/tycho-version-bump-plugin/update-target/update-target.target b/tycho-its/projects/tycho-version-bump-plugin/update-target/update-target.target index 5b8372e841..2704578b24 100644 --- a/tycho-its/projects/tycho-version-bump-plugin/update-target/update-target.target +++ b/tycho-its/projects/tycho-version-bump-plugin/update-target/update-target.target @@ -29,6 +29,11 @@ 2.0.0 jar + + jakarta.inject + jakarta.inject-api + 2.0.1 + diff --git a/tycho-its/src/test/java/org/eclipse/tycho/test/VersionBumpPluginTest.java b/tycho-its/src/test/java/org/eclipse/tycho/test/VersionBumpPluginTest.java index a4feab3d72..3e76dc3849 100644 --- a/tycho-its/src/test/java/org/eclipse/tycho/test/VersionBumpPluginTest.java +++ b/tycho-its/src/test/java/org/eclipse/tycho/test/VersionBumpPluginTest.java @@ -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();