slawekjaranowski commented on code in PR #1021:
URL: https://github.com/apache/maven-enforcer/pull/1021#discussion_r4009070381


##########
maven-enforcer-extension/src/it/projects/no-pom-enforcer-skip/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 = archetype:generate -Denforcer.skip 
-DgroupId=org.apache.maven.its -DartifactId=test-project 
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Review Comment:
   **Pin the archetype plugin version.** `archetype:generate` resolves the 
prefix to whatever the repository serves. The IT only exercises the bug because 
`generate` forks a lifecycle whose `validate` phase triggers the extension's 
execution, so the coverage depends on that plugin's behaviour. Please use full 
coordinates:
   
   ```
   invoker.goals = 
org.apache.maven.plugins:maven-archetype-plugin:3.4.1:generate -Denforcer.skip 
...
   ```



##########
maven-enforcer-extension/src/it/projects/no-pom-enforcer-skip/setup.groovy:
##########
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+File extensionsXml = new File(basedir, '.mvn/extensions.xml')
+
+if (extensionsXml.exists()) {
+    File extDir = new File(localRepositoryPath, 
'org/apache/maven/extensions/maven-enforcer-extension')
+    String version = extDir.list()?.find { it != 'maven-metadata-local.xml' }
+
+    extensionsXml.text = extensionsXml.text.replace('@project.version@', 
version)
+}

Review Comment:
   **Pass the version to the hook script.** `maven-invoker-plugin` has 
`scriptVariables`, which lets `setup.groovy` drop the local-repository scan:
   
   ```xml
   <scriptVariables>
     <projectVersion>${project.version}</projectVersion>
   </scriptVariables>
   ```
   
   ```groovy
   extensionsXml.text = extensionsXml.text.replace('@project.version@', 
projectVersion)
   ```



##########
maven-enforcer-extension/src/main/java/org/apache/maven/extensions/enforcer/EnforceExtension.java:
##########
@@ -58,6 +58,14 @@ public class EnforceExtension extends 
AbstractMavenLifecycleParticipant {
 
     @Override
     public void afterProjectsRead(MavenSession session) throws 
MavenExecutionException {
+        String skip = session.getUserProperties().getProperty("enforcer.skip");
+        if (skip == null) {
+            skip = session.getSystemProperties().getProperty("enforcer.skip");
+        }
+        if ("true".equalsIgnoreCase(skip) || (skip != null && skip.isEmpty())) 
{
+            return;
+        }

Review Comment:
   **Skip projects with no POM file.** Without a POM, Maven builds the 
`org.apache.maven:standalone-pom` stub and the extension attaches the execution 
to it. Please guard the loop:
   
   ```java
   for (MavenProject project : session.getProjects()) {
       if (project.getFile() == null) {
           continue;
       }
   ```
   
   This fixes the POM-less invocation in general, not only when 
`-Denforcer.skip` is passed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to