[MENFORCER-282] Add RequireProfileIdsExist to ensure al mentioned cmdline profiles exist
git-svn-id: https://svn.apache.org/repos/asf/maven/enforcer/trunk@1809668 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireProfileIdsExist.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireProfileIdsExist.java
new file mode 100644
index 0000000..9a16546
--- /dev/null
+++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireProfileIdsExist.java
@@ -0,0 +1,92 @@
+package org.apache.maven.plugins.enforcer;
+
+/*
+ * 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.util.ArrayList;
+import java.util.List;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.util.StringUtils;
+
+/**
+ * Ensure that all profiles mentioned on the commandline do exist.
+ *
+ * @author Robert Scholte
+ */
+public class RequireProfileIdsExist extends AbstractNonCacheableEnforcerRule
+{
+ @Override
+ public void execute( EnforcerRuleHelper helper )
+ throws EnforcerRuleException
+ {
+ try
+ {
+ MavenSession session = (MavenSession) helper.evaluate( "${session}" );
+
+ List<String> profileIds = new ArrayList<String>();
+ profileIds.addAll( session.getProjectBuildingRequest().getActiveProfileIds() );
+ profileIds.addAll( session.getProjectBuildingRequest().getInactiveProfileIds() );
+
+ for ( MavenProject project : session.getProjects() )
+ {
+ for ( org.apache.maven.model.Profile profile : project.getModel().getProfiles() )
+ {
+ profileIds.remove( profile.getId() );
+
+ if ( profileIds.isEmpty() )
+ {
+ return;
+ }
+ }
+ }
+
+ for ( org.apache.maven.settings.Profile profile : session.getSettings().getProfiles() )
+ {
+ profileIds.remove( profile.getId() );
+
+ if ( profileIds.isEmpty() )
+ {
+ return;
+ }
+ }
+
+ StringBuilder sb = new StringBuilder();
+ if ( profileIds.size() > 1 )
+ {
+ sb.append( "The requested profiles don't exist: " );
+ }
+ else
+ {
+ sb.append( "The requested profile doesn't exist: " );
+ }
+ sb.append( StringUtils.join( profileIds.iterator(), ", " ) );
+
+ throw new EnforcerRuleException( sb.toString() );
+ }
+ catch ( ExpressionEvaluationException e )
+ {
+ throw new EnforcerRuleException( e.getMessage() );
+ }
+ }
+}
diff --git a/enforcer-rules/src/site/apt/index.apt b/enforcer-rules/src/site/apt/index.apt
index 024f852..fe43cc9 100644
--- a/enforcer-rules/src/site/apt/index.apt
+++ b/enforcer-rules/src/site/apt/index.apt
@@ -73,6 +73,8 @@
* {{{./requirePrerequisite.html}requirePrerequisite}} - enforces that prerequisites have been specified.
+ * {{{./requireProfileIdsExist.html}requireProfileIdsExist}} - enforces the existence of profiles specified on the commandline.
+
* {{{./requireProperty.html}requireProperty}} - enforces the existence and values of properties.
* {{{./requireReleaseDeps.html}requireReleaseDeps}} - enforces that no snapshots are included as dependencies.
diff --git a/enforcer-rules/src/site/apt/requireProfileIdsExist.apt.vm b/enforcer-rules/src/site/apt/requireProfileIdsExist.apt.vm
new file mode 100644
index 0000000..8da72a4
--- /dev/null
+++ b/enforcer-rules/src/site/apt/requireProfileIdsExist.apt.vm
@@ -0,0 +1,61 @@
+ ~~ 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.
+
+ -----
+ Require Upper Bound Dependencies
+ -----
+ -----
+ 2017-09-25
+ -----
+
+ When running Maven with one or more unknown profile ids, Maven will give you a warning.
+ This rule will actually break the build for that reason.
+
+ Here is how a project should be setup to use this rule
+
+-----------------------------------------------------------------------------------
+<project>
+ ...
+ <build>
+ <plugins>
+ ...
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-enforcer-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <id>enforce</id>
+ <configuration>
+ <rules>
+ <requireProfileIdsExist/>
+ </rules>
+ </configuration>
+ <goals>
+ <goal>enforce</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ ...
+ </plugins>
+ </build>
+ ...
+</project>
+-----------------------------------------------------------------------------------
+
+
\ No newline at end of file
diff --git a/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_failure/invoker.properties b/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_failure/invoker.properties
new file mode 100644
index 0000000..bd8df3b
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_failure/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.buildResult=failure
+invoker.profiles = c
\ No newline at end of file
diff --git a/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_failure/pom.xml b/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_failure/pom.xml
new file mode 100644
index 0000000..ccac8b9
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_failure/pom.xml
@@ -0,0 +1,60 @@
+<?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>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.enforcer</groupId>
+ <artifactId>test</artifactId>
+ <version>1.0</version>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-enforcer-plugin</artifactId>
+ <version>@project.version@</version>
+ <executions>
+ <execution>
+ <id>test</id>
+ <goals>
+ <goal>enforce</goal>
+ </goals>
+ <configuration>
+ <rules>
+ <RequireProfileIdsExist/>
+ </rules>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <profiles>
+ <profile>
+ <id>a</id>
+ </profile>
+ <profile>
+ <id>b</id>
+ </profile>
+ </profiles>
+</project>
diff --git a/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_failure/verify.groovy b/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_failure/verify.groovy
new file mode 100644
index 0000000..1e476fc
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_failure/verify.groovy
@@ -0,0 +1,20 @@
+/*
+ * 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 buildLog = new File( basedir, 'build.log' )
+assert buildLog.text.contains( "The requested profile doesn't exist: c" )
diff --git a/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_success/invoker.properties b/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_success/invoker.properties
new file mode 100644
index 0000000..559c244
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_success/invoker.properties
@@ -0,0 +1,18 @@
+# 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.profiles = a
\ No newline at end of file
diff --git a/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_success/pom.xml b/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_success/pom.xml
new file mode 100644
index 0000000..ccac8b9
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-profile-ids-exist_success/pom.xml
@@ -0,0 +1,60 @@
+<?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>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.enforcer</groupId>
+ <artifactId>test</artifactId>
+ <version>1.0</version>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-enforcer-plugin</artifactId>
+ <version>@project.version@</version>
+ <executions>
+ <execution>
+ <id>test</id>
+ <goals>
+ <goal>enforce</goal>
+ </goals>
+ <configuration>
+ <rules>
+ <RequireProfileIdsExist/>
+ </rules>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <profiles>
+ <profile>
+ <id>a</id>
+ </profile>
+ <profile>
+ <id>b</id>
+ </profile>
+ </profiles>
+</project>