Skip to content

Commit

Permalink
Get rid of maven-compat use Resolver API
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Mar 19, 2023
1 parent 4f547b0 commit fa0f098
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
6 changes: 0 additions & 6 deletions animal-sniffer-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion animal-sniffer-maven-plugin/src/it/issue-24/verify.groovy
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
File log = new File(basedir, 'build.log')
assert log.exists()
assert log.text.contains( 'For artifact {org.codehaus.mojo.signature:java14:null:signature}: The version cannot be empty.' )
assert log.text.contains( 'For artifact {org.codehaus.mojo.signature:java14:signature:}: The version cannot be empty' )
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
*/

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -47,11 +43,15 @@
import org.codehaus.mojo.animal_sniffer.Clazz;
import org.codehaus.mojo.animal_sniffer.SignatureChecker;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -187,25 +187,16 @@ public void setSignature( String signatureId ) {
@Parameter( defaultValue = "true", property = "animal.sniffer.failOnError" )
protected boolean failOnError;

/**
*/
@Component
protected ArtifactResolver resolver;

/**
*/
@Parameter( defaultValue = "${project}", readonly = true )
protected MavenProject project;

/**
*/
@Parameter( defaultValue = "${localRepository}", readonly=true )
protected ArtifactRepository localRepository;
@Parameter( defaultValue = "${repositorySystemSession}", readonly = true )
private RepositorySystemSession repositorySystemSession;

/**
*/
@Component
protected ArtifactFactory artifactFactory;
private RepositorySystem repositorySystem;

static Map<File, Map<String, Clazz>> classes = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -249,9 +240,8 @@ public void execute()

getLog().info( "Checking unresolved references to " + signature );

Artifact a = signature.createArtifact( artifactFactory );
File signatureFile = resolveFileForArtifact( signature.createArtifact() );

resolver.resolve( a, project.getRemoteArtifactRepositories(), localRepository );
// just check code from this module
final Set<String> ignoredPackages = buildPackageList();

Expand All @@ -268,7 +258,7 @@ public void execute()
}

final SignatureChecker signatureChecker =
new SignatureChecker( loadClasses( a.getFile() ), ignoredPackages,
new SignatureChecker( loadClasses( signatureFile ), ignoredPackages,
new MavenLogger( getLog() ) );
signatureChecker.setCheckJars( false ); // don't want to decend into jar files that have been copied to
// the output directory as resources.
Expand Down Expand Up @@ -309,12 +299,24 @@ public void execute()
{
throw new MojoExecutionException( "Failed to check signatures", e );
}
catch ( AbstractArtifactResolutionException e )
catch ( ArtifactResolutionException e )
{
throw new MojoExecutionException( "Failed to obtain signature: " + signature, e );
}
}

private File resolveFileForArtifact(org.eclipse.aether.artifact.Artifact artifact )
throws ArtifactResolutionException, MojoExecutionException {

if ( StringUtils.isBlank( artifact.getVersion() ) ) {
throw new MojoExecutionException( "For artifact {" + artifact + "}: The version cannot be empty." );
}

ArtifactRequest request = new ArtifactRequest( artifact, project.getRemotePluginRepositories(), null );
ArtifactResult result = repositorySystem.resolveArtifact( repositorySystemSession, request );
return result.getArtifact().getFile();
}

private static Map<String, Clazz> loadClasses( File f ) throws IOException
{
Map<String, Clazz> classes = CheckSignatureMojo.classes.get( f );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
*
*/

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;

/**
* Represents artifact in Maven POM.
Expand Down Expand Up @@ -72,9 +72,9 @@ public void setVersion( String version )
this.version = version;
}

public Artifact createArtifact( ArtifactFactory factory )
public Artifact createArtifact()
{
return factory.createArtifact( groupId, artifactId, version, null, "signature" );
return new DefaultArtifact( groupId, artifactId, "signature", version);
}

public String toString()
Expand Down

0 comments on commit fa0f098

Please sign in to comment.