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

Fix #231 - Use Resolver Api for resolving artifacts #238

Merged
merged 1 commit into from
Nov 23, 2022
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
2 changes: 2 additions & 0 deletions src/it/local-project-repo/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = enforcer:enforce
invoker.buildResult = success
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>dumy</groupId>
<artifactId>dumy-local-repo</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
</project>
52 changes: 52 additions & 0 deletions src/it/local-project-repo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.mojo.extra-enforcer-rules.it</groupId>
<artifactId>local-project-repo</artifactId>
<version>1.0-SNAPSHOT</version>
<description>Resolve artifact from local repo</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>dumy</groupId>
<artifactId>dumy-local-repo</artifactId>
<type>pom</type>
<version>1.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@enforcerPluginVersion@</version>
<dependencies>
<dependency>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
<configuration>
<rules>
<enforceBytecodeVersion>
<maxJdkVersion>1.8</maxJdkVersion>
</enforceBytecodeVersion>
</rules>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>local-project-repo</id>
<url>${project.baseUri}local-repo</url>
</repository>
</repositories>
</project>
8 changes: 8 additions & 0 deletions src/it/local-project-repo/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" );

assert text.contains( '[DEBUG] Analyzing artifact dumy:dumy-local-repo:pom:1.0:compile' )

return true;
1 change: 1 addition & 0 deletions src/it/setup-prefill-repo/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = compile
25 changes: 25 additions & 0 deletions src/it/setup-prefill-repo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<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>localhost</groupId>
<artifactId>setup-prefill-repo</artifactId>
<version>1.0</version>
<name>prefill local repo</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- it is needed to be resolved for enforce-bytecode-version-with-corrupted-jar -->
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;

/**
* Abstract rule for when the content of the artifacts matters.
Expand All @@ -38,7 +38,6 @@ public abstract class AbstractResolveDependencies extends AbstractMojoHausEnforc

private MavenSession session;
private RepositorySystem repositorySystem;
private ResolutionErrorHandler resolutionErrorHandler;

private EnforcerRuleHelper helper;

Expand All @@ -51,7 +50,6 @@ public void execute( EnforcerRuleHelper helper )
try
{
repositorySystem = helper.getComponent( RepositorySystem.class );
resolutionErrorHandler = helper.getComponent( ResolutionErrorHandler.class );
graphBuilder = helper.getComponent( DependencyGraphBuilder.class );
}
catch ( ComponentLookupException e )
Expand Down Expand Up @@ -142,16 +140,15 @@ private Set<Artifact> getAllDescendants( DependencyNode node )

private void resolveArtifact( Artifact artifact ) throws ArtifactResolutionException
{
ArtifactResolutionRequest request = new ArtifactResolutionRequest()
.setArtifact( artifact )
.setLocalRepository( session.getLocalRepository() )
.setRemoteRepositories( session.getRequest().getProjectBuildingRequest().getRemoteRepositories() )
.setOffline( session.isOffline() )
.setForceUpdate( session.getRequest().isUpdateSnapshots() );
ArtifactRequest request = new ArtifactRequest();
request.setRepositories( session.getCurrentProject().getRemoteProjectRepositories() );
request.setArtifact( RepositoryUtils.toArtifact( artifact ) );

ArtifactResolutionResult result = repositorySystem.resolve( request );
ArtifactResult artifactResult = repositorySystem.resolveArtifact( session.getRepositorySession(), request );

resolutionErrorHandler.throwErrors( request, result );
artifact.setFile( artifactResult.getArtifact().getFile() );
artifact.setVersion( artifactResult.getArtifact().getVersion() );
artifact.setResolved( true );
}

protected Log getLog()
Expand Down