[MENFORCER-322] requireProfileIdsExist triggers if profile is defined at parent
authorGabriel Belingueres <belingueres@gmail.com>
Sun, 9 Dec 2018 03:46:08 +0000 (00:46 -0300)
committerKarl Heinz Marbaise <khmarbaise@apache.org>
Sun, 9 Dec 2018 12:57:06 +0000 (13:57 +0100)
 o Fixed iterating over parent poms. Added IT.

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireProfileIdsExist.java
maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/MENFORCER-322-module/pom.xml [new file with mode: 0644]
maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/invoker.properties [new file with mode: 0644]
maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/pom.xml [new file with mode: 0644]
maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/verify.groovy [new file with mode: 0644]

index 770f644..d595497 100644 (file)
@@ -33,6 +33,7 @@ import org.codehaus.plexus.util.StringUtils;
  * Ensure that all profiles mentioned on the commandline do exist. \r
  * \r
  * @author Robert Scholte\r
+ * @author Gabriel Belingueres\r
  */\r
 public class RequireProfileIdsExist extends AbstractNonCacheableEnforcerRule\r
 {\r
@@ -43,24 +44,32 @@ public class RequireProfileIdsExist extends AbstractNonCacheableEnforcerRule
         try\r
         {\r
             MavenSession session = (MavenSession) helper.evaluate( "${session}" );\r
-            \r
+\r
             List<String> profileIds = new ArrayList<String>();\r
             profileIds.addAll( session.getProjectBuildingRequest().getActiveProfileIds() );\r
             profileIds.addAll( session.getProjectBuildingRequest().getInactiveProfileIds() );\r
-            \r
+\r
             for ( MavenProject project : session.getProjects() )\r
             {\r
-                for ( org.apache.maven.model.Profile profile : project.getModel().getProfiles() )\r
+                // iterate over all parents\r
+                MavenProject currentProject = project;\r
+                do\r
                 {\r
-                    profileIds.remove( profile.getId() );\r
-                    \r
-                    if ( profileIds.isEmpty() )\r
+                    for ( org.apache.maven.model.Profile profile : currentProject.getModel().getProfiles() )\r
                     {\r
-                        return;\r
+                        profileIds.remove( profile.getId() );\r
+\r
+                        if ( profileIds.isEmpty() )\r
+                        {\r
+                            return;\r
+                        }\r
                     }\r
+\r
+                    currentProject = currentProject.getParent();\r
                 }\r
+                while ( currentProject != null );\r
             }\r
-            \r
+\r
             for ( org.apache.maven.settings.Profile profile : session.getSettings().getProfiles() )\r
             {\r
                 profileIds.remove( profile.getId() );\r
@@ -81,7 +90,7 @@ public class RequireProfileIdsExist extends AbstractNonCacheableEnforcerRule
                 sb.append( "The requested profile doesn't exist: " );\r
             }\r
             sb.append( StringUtils.join( profileIds.iterator(), ", " ) );\r
-            \r
+\r
             throw new EnforcerRuleException( sb.toString() );\r
         }\r
         catch ( ExpressionEvaluationException e )\r
diff --git a/maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/MENFORCER-322-module/pom.xml b/maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/MENFORCER-322-module/pom.xml
new file mode 100644 (file)
index 0000000..a0540d5
--- /dev/null
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+\r
+<!--\r
+Licensed to the Apache Software Foundation (ASF) under one\r
+or more contributor license agreements.  See the NOTICE file\r
+distributed with this work for additional information\r
+regarding copyright ownership.  The ASF licenses this file\r
+to you under the Apache License, Version 2.0 (the\r
+"License"); you may not use this file except in compliance\r
+with the License.  You may obtain a copy of the License at\r
+\r
+  http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+Unless required by applicable law or agreed to in writing,\r
+software distributed under the License is distributed on an\r
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+KIND, either express or implied.  See the License for the\r
+specific language governing permissions and limitations\r
+under the License.\r
+-->\r
+\r
+<!-- MENFORCER-322 -->\r
+<project>\r
+  <modelVersion>4.0.0</modelVersion>\r
+  \r
+  <parent>\r
+    <groupId>org.apache.maven.its.enforcer</groupId>\r
+    <artifactId>test</artifactId>\r
+    <version>1.0</version>\r
+    <relativePath>..</relativePath>\r
+  </parent>\r
+  \r
+  <groupId>org.apache.maven.its.enforcer</groupId>\r
+  <artifactId>menforcer-322-module</artifactId>\r
+  <version>1.0</version>\r
+\r
+  <build>\r
+    <plugins>\r
+      <plugin>\r
+        <groupId>org.apache.maven.plugins</groupId>\r
+        <artifactId>maven-enforcer-plugin</artifactId>\r
+        <version>@project.version@</version>\r
+        <executions>\r
+          <execution>\r
+            <id>test</id>\r
+            <goals>\r
+              <goal>enforce</goal>\r
+            </goals>\r
+            <configuration>\r
+              <rules>\r
+                <RequireProfileIdsExist/>\r
+              </rules>\r
+            </configuration>\r
+          </execution>\r
+        </executions>\r
+      </plugin>\r
+    </plugins>\r
+  </build>\r
+  \r
+  <profiles>\r
+    <profile>\r
+      <id>b</id>\r
+    </profile>\r
+  </profiles>\r
+</project>\r
diff --git a/maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/invoker.properties b/maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/invoker.properties
new file mode 100644 (file)
index 0000000..8d46b16
--- /dev/null
@@ -0,0 +1,27 @@
+# Licensed to the Apache Software Foundation (ASF) under one\r
+# or more contributor license agreements.  See the NOTICE file\r
+# distributed with this work for additional information\r
+# regarding copyright ownership.  The ASF licenses this file\r
+# to you under the Apache License, Version 2.0 (the\r
+# "License"); you may not use this file except in compliance\r
+# with the License.  You may obtain a copy of the License at\r
+#\r
+#  http://www.apache.org/licenses/LICENSE-2.0\r
+#\r
+# Unless required by applicable law or agreed to in writing,\r
+# software distributed under the License is distributed on an\r
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+# KIND, either express or implied.  See the License for the\r
+# specific language governing permissions and limitations\r
+# under the License.\r
+\r
+# MENFORCER-322\r
+\r
+# test building the top level project \r
+invoker.goals.1 = clean compile\r
+invoker.profiles.1 = a,b\r
+\r
+# test building the module\r
+invoker.project.2 = MENFORCER-322-module\r
+invoker.goals.2 = clean compile\r
+invoker.profiles.2 = a,b\r
diff --git a/maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/pom.xml b/maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/pom.xml
new file mode 100644 (file)
index 0000000..21adc5c
--- /dev/null
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+\r
+<!--\r
+Licensed to the Apache Software Foundation (ASF) under one\r
+or more contributor license agreements.  See the NOTICE file\r
+distributed with this work for additional information\r
+regarding copyright ownership.  The ASF licenses this file\r
+to you under the Apache License, Version 2.0 (the\r
+"License"); you may not use this file except in compliance\r
+with the License.  You may obtain a copy of the License at\r
+\r
+  http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+Unless required by applicable law or agreed to in writing,\r
+software distributed under the License is distributed on an\r
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+KIND, either express or implied.  See the License for the\r
+specific language governing permissions and limitations\r
+under the License.\r
+-->\r
+\r
+<!-- MENFORCER-322 -->\r
+<project>\r
+  <modelVersion>4.0.0</modelVersion>\r
+\r
+  <groupId>org.apache.maven.its.enforcer</groupId>\r
+  <artifactId>test</artifactId>\r
+  <version>1.0</version>\r
+  <packaging>pom</packaging>\r
+\r
+  <build>\r
+    <plugins>\r
+      <plugin>\r
+        <groupId>org.apache.maven.plugins</groupId>\r
+        <artifactId>maven-enforcer-plugin</artifactId>\r
+        <version>@project.version@</version>\r
+        <executions>\r
+          <execution>\r
+            <id>test</id>\r
+            <goals>\r
+              <goal>enforce</goal>\r
+            </goals>\r
+            <configuration>\r
+              <rules>\r
+                <RequireProfileIdsExist/>\r
+              </rules>\r
+            </configuration>\r
+          </execution>\r
+        </executions>\r
+      </plugin>\r
+    </plugins>\r
+  </build>\r
+  \r
+  <modules>\r
+    <module>MENFORCER-322-module</module>\r
+  </modules>\r
+  \r
+  <profiles>\r
+    <profile>\r
+      <id>a</id>\r
+    </profile>\r
+  </profiles>\r
+</project>\r
diff --git a/maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/verify.groovy b/maven-enforcer-plugin/src/it/projects/require-profile-id-exist_defined_in_parent/verify.groovy
new file mode 100644 (file)
index 0000000..4e58239
--- /dev/null
@@ -0,0 +1,22 @@
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one\r
+ * or more contributor license agreements.  See the NOTICE file\r
+ * distributed with this work for additional information\r
+ * regarding copyright ownership.  The ASF licenses this file\r
+ * to you under the Apache License, Version 2.0 (the\r
+ * "License"); you may not use this file except in compliance\r
+ * with the License.  You may obtain a copy of the License at\r
+ *\r
+ *   http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing,\r
+ * software distributed under the License is distributed on an\r
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+ * KIND, either express or implied.  See the License for the\r
+ * specific language governing permissions and limitations\r
+ * under the License.\r
+ */\r
+\r
+def buildLog = new File( basedir, 'build.log' )\r
+assert !( buildLog.text.contains( "The requested profile doesn't exist: a" ) )\r
+assert !( buildLog.text.contains( "The requested profile doesn't exist: b" ) )\r