From 3361a6fb5b89a28f451e71bbc2418addb6d5676c Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Tue, 9 May 2023 19:39:22 +0200 Subject: [PATCH] Get rid of ArtifactRepository - ${localRepository} --- .../invoker.properties | 2 + .../pom.xml | 32 +++++++++ .../settings-maven-oss-snapshot.xml | 29 +++++++++ .../verify.groovy | 11 ++++ .../invoker.properties | 5 ++ src/it/remove-project-artifact/pom.xml | 26 ++++++++ src/it/remove-project-artifact/verify.groovy | 8 +++ .../mojo/buildhelper/ReleasedVersionMojo.java | 65 ++++++++++--------- .../buildhelper/RemoveLocalArtifactMojo.java | 24 +++---- 9 files changed, 162 insertions(+), 40 deletions(-) create mode 100644 src/it/released-version-existing-snapshot/invoker.properties create mode 100644 src/it/released-version-existing-snapshot/pom.xml create mode 100644 src/it/released-version-existing-snapshot/settings-maven-oss-snapshot.xml create mode 100644 src/it/released-version-existing-snapshot/verify.groovy create mode 100644 src/it/remove-project-artifact/invoker.properties create mode 100644 src/it/remove-project-artifact/pom.xml create mode 100644 src/it/remove-project-artifact/verify.groovy diff --git a/src/it/released-version-existing-snapshot/invoker.properties b/src/it/released-version-existing-snapshot/invoker.properties new file mode 100644 index 00000000..93b7e528 --- /dev/null +++ b/src/it/released-version-existing-snapshot/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = validate +invoker.settingsFile = src/it/released-version-existing-snapshot/settings-maven-oss-snapshot.xml diff --git a/src/it/released-version-existing-snapshot/pom.xml b/src/it/released-version-existing-snapshot/pom.xml new file mode 100644 index 00000000..501a9873 --- /dev/null +++ b/src/it/released-version-existing-snapshot/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + + + org.codehaus.mojo + versions-maven-plugin + 1.0.0-SNAPHOST + build-helper-maven-plugin-released-version-it + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + released-version + + released-version + + + myReleasedVersion + + + + + + + + diff --git a/src/it/released-version-existing-snapshot/settings-maven-oss-snapshot.xml b/src/it/released-version-existing-snapshot/settings-maven-oss-snapshot.xml new file mode 100644 index 00000000..6fc12b25 --- /dev/null +++ b/src/it/released-version-existing-snapshot/settings-maven-oss-snapshot.xml @@ -0,0 +1,29 @@ + + + + + + default + + + + oss-snap + https://oss.sonatype.org/content/groups/public/ + + false + fail + + + true + fail + + + + + + + default + + diff --git a/src/it/released-version-existing-snapshot/verify.groovy b/src/it/released-version-existing-snapshot/verify.groovy new file mode 100644 index 00000000..9e0650bc --- /dev/null +++ b/src/it/released-version-existing-snapshot/verify.groovy @@ -0,0 +1,11 @@ +File file = new File( basedir, "build.log" ); +assert file.exists(); + +String text = file.getText("utf-8"); + +// we have SNAPSHOT on list +assert text.matches('(?ms)^.*Resolved versions: .*-SNAPSHOT.*$') + +// but released version is not a SNAPSHOT +assert text.matches( '(?ms)^.*Released version: .*$') +assert !text.matches( '(?ms)^.*Released version: .*-SNAPSHOT.*$') diff --git a/src/it/remove-project-artifact/invoker.properties b/src/it/remove-project-artifact/invoker.properties new file mode 100644 index 00000000..ac820416 --- /dev/null +++ b/src/it/remove-project-artifact/invoker.properties @@ -0,0 +1,5 @@ +# first install something +invoker.goals.1 = install + +# next try to delete +invoker.goals.2 = package diff --git a/src/it/remove-project-artifact/pom.xml b/src/it/remove-project-artifact/pom.xml new file mode 100644 index 00000000..f9b421cb --- /dev/null +++ b/src/it/remove-project-artifact/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + org.codehaus.mojo + remove-project-artifact-it + 1.0-SNAPSHOT + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + released-version + + remove-project-artifact + + + + + + + + diff --git a/src/it/remove-project-artifact/verify.groovy b/src/it/remove-project-artifact/verify.groovy new file mode 100644 index 00000000..122decda --- /dev/null +++ b/src/it/remove-project-artifact/verify.groovy @@ -0,0 +1,8 @@ +File file = new File(basedir, "build.log"); +assert file.exists(); + +String text = file.getText("utf-8"); + +def FS = File.separator + +assert 2 == text.count("org${FS}codehaus${FS}mojo${FS}remove-project-artifact-it removed.") : 'removed log should be present twice' diff --git a/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java index 77d3c7fa..67260e47 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java @@ -24,19 +24,21 @@ * SOFTWARE. */ -import java.util.List; import java.util.Objects; import org.apache.maven.artifact.ArtifactUtils; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; -import org.apache.maven.artifact.metadata.ArtifactMetadataSource; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.resolution.VersionRangeRequest; +import org.eclipse.aether.resolution.VersionRangeResolutionException; +import org.eclipse.aether.resolution.VersionRangeResult; /** * Resolve the latest released version of this project. This mojo sets the following properties: @@ -46,6 +48,8 @@ * [propertyPrefix].majorVersion * [propertyPrefix].minorVersion * [propertyPrefix].incrementalVersion + * [propertyPrefix].buildNumber + * [propertyPrefix].qualifier * * * Where the propertyPrefix is the string set in the mojo parameter. @@ -61,17 +65,15 @@ public class ReleasedVersionMojo /** * The artifact metadata source to use. */ - @Component - private ArtifactMetadataSource artifactMetadataSource; @Component - private ArtifactFactory artifactFactory; + private RepositorySystem repoSystem; - @Parameter( defaultValue = "${localRepository}", readonly = true ) - private ArtifactRepository localRepository; + @Component + private ArtifactHandlerManager artifactHandlerManager; - @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true ) - private List remoteArtifactRepositories; + @Parameter( defaultValue = "${repositorySystemSession}", readonly = true ) + private RepositorySystemSession repoSession; /** * Prefix string to use for the set of version properties. @@ -92,6 +94,7 @@ private void defineVersionProperty( String name, int value ) @SuppressWarnings( "unchecked" ) public void execute() { + /* * We use a dummy version "0" here to check for all released version. * Reason: The current project's version is completely irrelevant for the check to retrieve all available versions. @@ -100,23 +103,27 @@ public void execute() * Using the dummy version "0" which looks like a released version, the repos with releases are requested. * see https://github.com/mojohaus/build-helper-maven-plugin/issues/108 */ - final String DUMMY_VERSION = "0"; - org.apache.maven.artifact.Artifact artifact = - artifactFactory.createArtifact( getProject().getGroupId(), getProject().getArtifactId(), DUMMY_VERSION, "", "" ); try { - ArtifactVersion releasedVersion = null; - List versions = - artifactMetadataSource.retrieveAvailableVersions( artifact, localRepository, - remoteArtifactRepositories ); - for ( ArtifactVersion version : versions ) - { - if ( !ArtifactUtils.isSnapshot( version.toString() ) - && ( releasedVersion == null || version.compareTo( releasedVersion ) > 0 ) ) - { - releasedVersion = version; - } - } + + DefaultArtifact artifact = + new DefaultArtifact(getProject().getGroupId(), getProject().getArtifactId(), + artifactHandlerManager.getArtifactHandler(getProject().getPackaging()).getExtension(), "[0,)"); + + getLog().debug("Artifact for lookup released version: " + artifact); + VersionRangeRequest request = new VersionRangeRequest(artifact, getProject().getRemoteProjectRepositories(), null); + + VersionRangeResult versionRangeResult = repoSystem.resolveVersionRange(repoSession, request); + + getLog().debug("Resolved versions: " + versionRangeResult.getVersions()); + + DefaultArtifactVersion releasedVersion = versionRangeResult.getVersions().stream() + .filter(v -> !ArtifactUtils.isSnapshot(v.toString())) + .map(v -> new DefaultArtifactVersion(v.toString())) + .max(DefaultArtifactVersion::compareTo) + .orElse(null); + + getLog().debug("Released version: " + releasedVersion); if ( releasedVersion != null ) { @@ -142,7 +149,7 @@ public void execute() } } - catch ( ArtifactMetadataRetrievalException e ) + catch (VersionRangeResolutionException e ) { if ( getLog().isWarnEnabled() ) { diff --git a/src/main/java/org/codehaus/mojo/buildhelper/RemoveLocalArtifactMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/RemoveLocalArtifactMojo.java index 1e2c59b0..9603b9cf 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/RemoveLocalArtifactMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/RemoveLocalArtifactMojo.java @@ -27,14 +27,16 @@ import java.io.File; import java.io.IOException; -import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.RepositoryUtils; import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.FileUtils; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.repository.LocalRepositoryManager; /** * Remove project's artifacts from local repository. Useful to keep only one copy of large local snapshot, for example: @@ -71,18 +73,18 @@ public class RemoveLocalArtifactMojo @Parameter( readonly = true, defaultValue = "${project}" ) private MavenProject project; - /** - * @since 1.1 - */ - @Parameter( defaultValue = "${localRepository}", readonly = true ) - private ArtifactRepository localRepository; + @Parameter( readonly = true, defaultValue = "${repositorySystemSession}" ) + private RepositorySystemSession repoSession; public void execute() - throws MojoFailureException + throws MojoExecutionException { - File localArtifactFile = - new File( localRepository.getBasedir(), localRepository.pathOf( project.getArtifact() ) ); + LocalRepositoryManager lrm = repoSession.getLocalRepositoryManager(); + + String artifactPath = lrm.getPathForLocalArtifact(RepositoryUtils.toArtifact(project.getArtifact())); + File repoBasedir = lrm.getRepository().getBasedir(); + File localArtifactFile = new File( repoBasedir, artifactPath ); File localArtifactDirectory = localArtifactFile.getParentFile(); if ( removeAll ) @@ -104,7 +106,7 @@ public void execute() final String failureMessage = "Cannot delete " + localArtifactDirectory; if ( failOnError ) { - throw new MojoFailureException( failureMessage ); + throw new MojoExecutionException( failureMessage ); } else {