You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2023/01/22 19:15:20 UTC

[maven-compiler-plugin] branch master updated: [MCOMPILER-522] Use maven-resolver to resolve 'annotationProcessorPaths' dependencies

This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-compiler-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 52fb27e  [MCOMPILER-522] Use maven-resolver to resolve 'annotationProcessorPaths' dependencies
52fb27e is described below

commit 52fb27ea14e0aa96fc1acd30e8c3e7fbd07c5563
Author: Petr Široký <pe...@pm.me>
AuthorDate: Thu Jan 19 11:57:36 2023 +0100

    [MCOMPILER-522] Use maven-resolver to resolve 'annotationProcessorPaths' 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)
---
 pom.xml                                            |  1 +
 .../invoker.properties                             | 19 +++++++
 .../MCOMPILER-522-unresolvable-dependency/pom.xml  | 52 +++++++++++++++++++
 .../SimpleAnnotation.java                          | 32 ++++++++++++
 .../SimpleObject.java                              | 26 ++++++++++
 .../verify.groovy                                  | 26 ++++++++++
 .../plugin/compiler/AbstractCompilerMojo.java      | 60 +++++++++-------------
 7 files changed, 181 insertions(+), 35 deletions(-)

diff --git a/pom.xml b/pom.xml
index 3f6c961..a3f6746 100644
--- a/pom.xml
+++ b/pom.xml
@@ -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>
diff --git a/src/it/MCOMPILER-522-unresolvable-dependency/invoker.properties b/src/it/MCOMPILER-522-unresolvable-dependency/invoker.properties
new file mode 100644
index 0000000..9e2dad8
--- /dev/null
+++ b/src/it/MCOMPILER-522-unresolvable-dependency/invoker.properties
@@ -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
diff --git a/src/it/MCOMPILER-522-unresolvable-dependency/pom.xml b/src/it/MCOMPILER-522-unresolvable-dependency/pom.xml
new file mode 100644
index 0000000..0614cd0
--- /dev/null
+++ b/src/it/MCOMPILER-522-unresolvable-dependency/pom.xml
@@ -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>
diff --git a/src/it/MCOMPILER-522-unresolvable-dependency/src/main/java/org/apache.maven.plugins.compiler.it/SimpleAnnotation.java b/src/it/MCOMPILER-522-unresolvable-dependency/src/main/java/org/apache.maven.plugins.compiler.it/SimpleAnnotation.java
new file mode 100644
index 0000000..0c79335
--- /dev/null
+++ b/src/it/MCOMPILER-522-unresolvable-dependency/src/main/java/org/apache.maven.plugins.compiler.it/SimpleAnnotation.java
@@ -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
+{
+
+}
diff --git a/src/it/MCOMPILER-522-unresolvable-dependency/src/main/java/org/apache.maven.plugins.compiler.it/SimpleObject.java b/src/it/MCOMPILER-522-unresolvable-dependency/src/main/java/org/apache.maven.plugins.compiler.it/SimpleObject.java
new file mode 100644
index 0000000..5d8760e
--- /dev/null
+++ b/src/it/MCOMPILER-522-unresolvable-dependency/src/main/java/org/apache.maven.plugins.compiler.it/SimpleObject.java
@@ -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
+{
+
+}
diff --git a/src/it/MCOMPILER-522-unresolvable-dependency/verify.groovy b/src/it/MCOMPILER-522-unresolvable-dependency/verify.groovy
new file mode 100644
index 0000000..7c86212
--- /dev/null
+++ b/src/it/MCOMPILER-522-unresolvable-dependency/verify.groovy
@@ -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" )
diff --git a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
index 8da9213..19265a7 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
@@ -40,14 +40,8 @@ import java.util.Map;
 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;
@@ -56,7 +50,6 @@ import org.apache.maven.plugin.MojoExecutionException;
 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;
@@ -83,6 +76,15 @@ import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
 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;
 
@@ -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 );
@@ -1814,29 +1810,23 @@ public abstract class AbstractCompilerMojo
                 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() );
+                    elements.add( resolved.getArtifact().getFile().getAbsolutePath() );
                 }
             }
             return new ArrayList<>( elements );