Skip to content

Commit

Permalink
[MCOMPILER-522] Use maven-resolver to resolve 'annotationProcessorPat…
Browse files Browse the repository at this point in the history
…hs' dependencies

 * heavily based on work by @cstamas (PR #151)

 * added IT to verify the build fails if one the dependencies
   cannot be resolved (does not exist)
  • Loading branch information
psiroky committed Jan 19, 2023
1 parent c62de5c commit 42f5295
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 35 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ under the License.
<scope>test</scope>
</dependency>
<dependency>
<!-- still needed because of AbstractMojoTestCase -->
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${mavenVersion}</version>
Expand Down
19 changes: 19 additions & 0 deletions src/it/MCOMPILER-522-unresolvable-dependency/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

invoker.goals = compile
invoker.buildResult = failure
52 changes: 52 additions & 0 deletions src/it/MCOMPILER-522-unresolvable-dependency/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<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.apache.maven.plugins.compiler.it</groupId>
<artifactId>mcompiler522-unresolvable-annotation-processor-depdendency</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<annotationProcessors>
<annotationProcessor>org.issue.SimpleAnnotationProcessor</annotationProcessor>
</annotationProcessors>
<annotationProcessorPaths>
<path>
<groupId>org.apache.maven.plugins.compiler.it</groupId>
<artifactId>annotation-processor-non-existing</artifactId>
<version>1.0-SNAPSHOT</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.apache.maven.plugins.compiler.it;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface SimpleAnnotation
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.apache.maven.plugins.compiler.it;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

@SimpleAnnotation
public class SimpleObject
{

}
26 changes: 26 additions & 0 deletions src/it/MCOMPILER-522-unresolvable-dependency/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
def logFile = new File( basedir, 'build.log' )
assert logFile.exists()

def buildLog = logFile.getText('UTF-8')
assert buildLog.contains ("Caused by: org.apache.maven.plugin.MojoExecutionException: " +
"Resolution of annotationProcessorPath dependencies failed: " +
"Could not find artifact org.apache.maven.plugins.compiler.it:annotation-processor-non-existing:jar:1.0-SNAPSHOT" )
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 All @@ -83,6 +76,15 @@
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.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 @@ -581,12 +583,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 @@ -1814,29 +1810,26 @@ 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 );

for ( ArtifactResult resolved : dependencyResult.getArtifactResults() )
{
elements.add( resolved.getFile().getAbsolutePath() );
if ( resolved.getArtifact() != null && resolved.getArtifact().getFile() != null )
{
elements.add( resolved.getArtifact().getFile().getAbsolutePath() );
}
}
}
return new ArrayList<>( elements );
Expand Down

0 comments on commit 42f5295

Please sign in to comment.