Skip to content

Commit

Permalink
Get rid of ArtifactRepository - ${localRepository}
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 10, 2023
1 parent 0bdc352 commit 3361a6f
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 40 deletions.
2 changes: 2 additions & 0 deletions src/it/released-version-existing-snapshot/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = validate
invoker.settingsFile = src/it/released-version-existing-snapshot/settings-maven-oss-snapshot.xml
32 changes: 32 additions & 0 deletions src/it/released-version-existing-snapshot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- We use artifact which contains released and snapshot versions. -->

<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>1.0.0-SNAPHOST</version>
<name>build-helper-maven-plugin-released-version-it</name>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>released-version</id>
<goals>
<goal>released-version</goal>
</goals>
<configuration>
<propertyPrefix>myReleasedVersion</propertyPrefix>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<profiles>
<profile>
<id>default</id>
<!-- use oss repo which provide release and snapshot artifacts -->
<repositories>
<repository>
<id>oss-snap</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
<releases>
<enabled>false</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>default</activeProfile>
</activeProfiles>
</settings>
11 changes: 11 additions & 0 deletions src/it/released-version-existing-snapshot/verify.groovy
Original file line number Diff line number Diff line change
@@ -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.*$')
5 changes: 5 additions & 0 deletions src/it/remove-project-artifact/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# first install something
invoker.goals.1 = install

# next try to delete
invoker.goals.2 = package
26 changes: 26 additions & 0 deletions src/it/remove-project-artifact/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.mojo</groupId>
<artifactId>remove-project-artifact-it</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>released-version</id>
<goals>
<goal>remove-project-artifact</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
8 changes: 8 additions & 0 deletions src/it/remove-project-artifact/verify.groovy
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -46,6 +48,8 @@
* [propertyPrefix].majorVersion
* [propertyPrefix].minorVersion
* [propertyPrefix].incrementalVersion
* [propertyPrefix].buildNumber
* [propertyPrefix].qualifier
* </pre>
*
* Where the propertyPrefix is the string set in the mojo parameter.
Expand All @@ -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<ArtifactRepository> remoteArtifactRepositories;
@Parameter( defaultValue = "${repositorySystemSession}", readonly = true )
private RepositorySystemSession repoSession;

/**
* Prefix string to use for the set of version properties.
Expand All @@ -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.
Expand All @@ -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<ArtifactVersion> 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 )
{
Expand All @@ -142,7 +149,7 @@ public void execute()
}

}
catch ( ArtifactMetadataRetrievalException e )
catch (VersionRangeResolutionException e )
{
if ( getLog().isWarnEnabled() )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 )
Expand All @@ -104,7 +106,7 @@ public void execute()
final String failureMessage = "Cannot delete " + localArtifactDirectory;
if ( failOnError )
{
throw new MojoFailureException( failureMessage );
throw new MojoExecutionException( failureMessage );
}
else
{
Expand Down

0 comments on commit 3361a6f

Please sign in to comment.