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

Move away from legacy maven-compat #151

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ under the License.
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<!-- maven-compat: is must due AbstractMojoTestCase :( -->
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that if you don not need to Inject parameters into the Mojo and thus do not test the Mojos run method, one can also go with PlexusTestcase see:
https://github.com/eclipse-tycho/tycho/blob/fdda7227e6cd714c2e120f70dd624cde2cca4ce1/tycho-p2/tycho-p2-facade/src/test/java/org/eclipse/tycho/p2/facade/test/MetadataSerializableImplTest.java

Still, I think this is a bit of a "blind-spot" that there is not a JUnit5 / Non-Compat replacement for a MojoTestCase.

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,8 @@
import java.util.Properties;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
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.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -56,7 +50,6 @@
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.shared.incremental.IncrementalBuildHelper;
import org.apache.maven.shared.incremental.IncrementalBuildHelperRequest;
import org.apache.maven.shared.utils.ReaderFactory;
Expand Down Expand Up @@ -85,6 +78,16 @@
import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;
import org.codehaus.plexus.languages.java.version.JavaVersion;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResult;
import org.eclipse.aether.util.artifact.JavaScopes;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

Expand Down Expand Up @@ -583,12 +586,6 @@ public abstract class AbstractCompilerMojo
@Component
private ArtifactHandlerManager artifactHandlerManager;

/**
* Throws an exception on artifact resolution errors.
*/
@Component
private ResolutionErrorHandler resolutionErrorHandler;

protected abstract SourceInclusionScanner getSourceInclusionScanner( int staleMillis );

protected abstract SourceInclusionScanner getSourceInclusionScanner( String inputFileEnding );
Expand Down Expand Up @@ -1847,29 +1844,36 @@ private List<String> resolveProcessorPathEntries()
ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( coord.getType() );

Artifact artifact = new DefaultArtifact(
coord.getGroupId(),
coord.getArtifactId(),
VersionRange.createFromVersionSpec( coord.getVersion() ),
Artifact.SCOPE_RUNTIME,
coord.getType(),
coord.getClassifier(),
handler,
false );

ArtifactResolutionRequest request = new ArtifactResolutionRequest()
.setArtifact( artifact )
.setResolveRoot( true )
.setResolveTransitively( true )
.setLocalRepository( session.getLocalRepository() )
.setRemoteRepositories( project.getRemoteArtifactRepositories() );

ArtifactResolutionResult resolutionResult = repositorySystem.resolve( request );

resolutionErrorHandler.throwErrors( request, resolutionResult );

for ( Artifact resolved : resolutionResult.getArtifacts() )
coord.getGroupId(),
coord.getArtifactId(),
coord.getClassifier(),
handler.getExtension(),
coord.getVersion()
);

CollectRequest collectRequest = new CollectRequest( new Dependency( artifact, JavaScopes.RUNTIME ),
project.getRemoteProjectRepositories() );
DependencyRequest dependencyRequest = new DependencyRequest();
dependencyRequest.setCollectRequest( collectRequest );
DependencyResult dependencyResult = repositorySystem.resolveDependencies(
session.getRepositorySession(), dependencyRequest );

ArrayList<ArtifactResult> failed = new ArrayList<>();
for ( ArtifactResult resolved : dependencyResult.getArtifactResults() )
{
if ( resolved.getArtifact() != null && resolved.getArtifact().getFile() != null )
{
elements.add( resolved.getArtifact().getFile().getAbsolutePath() );
}
else
{
failed.add( resolved );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this actually happen or would this not just throw a DependencyResolutionException already?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you are right. If the one the dependencies cannot be resolved, DependencyResolutionException is thrown on line 1858, so the manual handling of failed artifacts should no be needed?

Caused by: org.apache.maven.plugin.MojoExecutionException: Resolution of annotationProcessorPath dependencies failed: Could not find artifact org.issue:annotation-processor2:jar:1.0-SNAPSHOT in local.central (file:///home/psiroky/.m2/repository)
	at org.apache.maven.plugin.compiler.AbstractCompilerMojo.resolveProcessorPathEntries(AbstractCompilerMojo.java:1884)
	at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:792)
	at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:205)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
	at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2(MojoExecutor.java:370)
	... 20 common frames omitted
Caused by: org.eclipse.aether.resolution.DependencyResolutionException: Could not find artifact org.issue:annotation-processor2:jar:1.0-SNAPSHOT in local.central (file:///home/psiroky/.m2/repository)
	at org.eclipse.aether.internal.impl.DefaultRepositorySystem.resolveDependencies(DefaultRepositorySystem.java:374)
	at org.apache.maven.plugin.compiler.AbstractCompilerMojo.resolveProcessorPathEntries(AbstractCompilerMojo.java:1858)
	... 24 common frames omitted
Caused by: org.eclipse.aether.resolution.ArtifactResolutionException: Could not find artifact org.issue:annotation-processor2:jar:1.0-SNAPSHOT in local.central (file:///home/psiroky/.m2/repository)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:431)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:235)
	at org.eclipse.aether.internal.impl.DefaultRepositorySystem.resolveDependencies(DefaultRepositorySystem.java:357)
	... 25 common frames omitted
Caused by: org.eclipse.aether.transfer.ArtifactNotFoundException: Could not find artifact org.issue:annotation-processor2:jar:1.0-SNAPSHOT in local.central (file:///home/psiroky/.m2/repository)
	at org.eclipse.aether.connector.basic.ArtifactTransportListener.transferFailed(ArtifactTransportListener.java:48)
	at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:369)
	at org.eclipse.aether.util.concurrency.RunnableErrorForwarder.lambda$wrap$0(RunnableErrorForwarder.java:73)
	at org.eclipse.aether.connector.basic.BasicRepositoryConnector$DirectExecutor.execute(BasicRepositoryConnector.java:627)
	at org.eclipse.aether.connector.basic.BasicRepositoryConnector.get(BasicRepositoryConnector.java:262)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(DefaultArtifactResolver.java:520)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:408)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. In addition, result.getExceptions() will contain the list of all gathered exceptions (if there are more than one).

}
}
if ( !failed.isEmpty() )
{
elements.add( resolved.getFile().getAbsolutePath() );
throw new MojoExecutionException( "Error resolving processor path entries",
new ArtifactResolutionException( failed ) );
}
}
return new ArrayList<>( elements );
Expand Down