You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2021/05/26 18:14:35 UTC

[maven-enforcer] branch MENFORCER-257 created (now eadd632)

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

rfscholte pushed a change to branch MENFORCER-257
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git.


      at eadd632  [MENFORCER-257] RequireActiveProfile should respect inherited activated profiles

This branch includes the following new commits:

     new eadd632  [MENFORCER-257] RequireActiveProfile should respect inherited activated profiles

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[maven-enforcer] 01/01: [MENFORCER-257] RequireActiveProfile should respect inherited activated profiles

Posted by rf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch MENFORCER-257
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git

commit eadd63210b0d640b0b2b2672d4499a1771ff4052
Author: rfscholte <rf...@apache.org>
AuthorDate: Wed May 26 20:14:07 2021 +0200

    [MENFORCER-257] RequireActiveProfile should respect inherited activated profiles
---
 .../plugins/enforcer/RequireActiveProfile.java     | 27 +++++------
 .../plugins/enforcer/RequireActiveProfileTest.java | 48 +++++++++----------
 .../invoker.properties                             | 18 ++++++++
 .../parent/pom.xml                                 | 54 ++++++++++++++++++++++
 .../require-active-profile-inherited/pom.xml       | 30 ++++++++++++
 5 files changed, 135 insertions(+), 42 deletions(-)

diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireActiveProfile.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireActiveProfile.java
index 31fde9c..20f8def 100644
--- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireActiveProfile.java
+++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireActiveProfile.java
@@ -21,10 +21,10 @@ package org.apache.maven.plugins.enforcer;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
-import org.apache.maven.model.Profile;
 import org.apache.maven.project.MavenProject;
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
 import org.codehaus.plexus.util.StringUtils;
@@ -82,19 +82,19 @@ public class RequireActiveProfile
             MavenProject project = (MavenProject) theHelper.evaluate( "${project}" );
             if ( StringUtils.isNotEmpty( profiles ) )
             {
-                String[] profs = profiles.split( "," );
-                for ( String profile : profs )
+                String[] profileIds = profiles.split( "," );
+                for ( String profileId : profileIds )
                 {
-                    if ( !isProfileActive( project, profile ) )
+                    if ( !isProfileActive( project, profileId ) )
                     {
-                        missingProfiles.add( profile );
+                        missingProfiles.add( profileId );
                     }
                 }
 
                 boolean fail = false;
                 if ( !missingProfiles.isEmpty() )
                 {
-                    if ( all || missingProfiles.size() == profs.length )
+                    if ( all || missingProfiles.size() == profileIds.length )
                     {
                       fail = true;
                     }
@@ -131,23 +131,18 @@ public class RequireActiveProfile
      * Checks if profile is active.
      *
      * @param project the project
-     * @param profileName the profile name
+     * @param profileId the profile name
      * @return <code>true</code> if profile is active, otherwise <code>false</code>
      */
-    protected boolean isProfileActive( MavenProject project, String profileName )
+    protected boolean isProfileActive( MavenProject project, String profileId )
     {
-        List<Profile> activeProfiles = project.getActiveProfiles();
-        if ( activeProfiles != null && !activeProfiles.isEmpty() )
+        for ( Map.Entry<String, List<String>> entry : project.getInjectedProfileIds().entrySet() )
         {
-            for ( Profile profile : activeProfiles )
+            if ( entry.getValue().contains( profileId ) )
             {
-                if ( profile.getId().equals( profileName ) )
-                {
-                    return true;
-                }
+                return true;
             }
         }
-
         return false;
     }
 }
diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireActiveProfileTest.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireActiveProfileTest.java
index f312119..6fd9897 100644
--- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireActiveProfileTest.java
+++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireActiveProfileTest.java
@@ -19,17 +19,16 @@ package org.apache.maven.plugins.enforcer;
  * under the License.
  */
 
-import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
-import org.apache.maven.model.Profile;
 import org.apache.maven.project.MavenProject;
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
 import org.junit.Before;
@@ -62,7 +61,7 @@ public class RequireActiveProfileTest
     public void testNoActiveProfilesInProjectAndNoProfilesExpectedToBeActivated()
         throws EnforcerRuleException
     {
-        when( project.getActiveProfiles() ).thenReturn( Collections.<Profile> emptyList() );
+        when( project.getInjectedProfileIds() ).thenReturn( Collections.<String, List<String>>emptyMap() );
 
         rule.execute( helper );
     }
@@ -71,9 +70,10 @@ public class RequireActiveProfileTest
     public void testActiveProfileAndExpectedActiveProfile()
         throws EnforcerRuleException
     {
-        List<Profile> profiles = Collections.<Profile> singletonList( createProfile( "profile-2" ) );
+        Map<String, List<String>> profiles =
+            Collections.<String, List<String>>singletonMap( "pom", Arrays.asList( "profile-2" ) );
 
-        when( project.getActiveProfiles() ).thenReturn( profiles );
+        when( project.getInjectedProfileIds() ).thenReturn( profiles );
 
         rule.setProfiles( "profile-2" );
 
@@ -84,7 +84,7 @@ public class RequireActiveProfileTest
     public void testNoActiveProfileButTheRuleRequestedAnActiveProfile()
         throws EnforcerRuleException
     {
-        when( project.getActiveProfiles() ).thenReturn( Collections.<Profile> emptyList() );
+        when( project.getInjectedProfileIds() ).thenReturn( Collections.<String, List<String>>emptyMap() );
 
         rule.setProfiles( "profile-2" );
 
@@ -96,7 +96,7 @@ public class RequireActiveProfileTest
     public void testNoActiveProfileButWeExpectToGetAnExceptionWithAll()
         throws EnforcerRuleException
     {
-        when( project.getActiveProfiles() ).thenReturn( Collections.<Profile> emptyList() );
+        when( project.getInjectedProfileIds() ).thenReturn( Collections.<String, List<String>>emptyMap() );
 
         rule.setProfiles( "profile-2" );
         rule.setAll( true );
@@ -109,9 +109,10 @@ public class RequireActiveProfileTest
     public void testTwoActiveProfilesWithOneRequiredProfile()
         throws EnforcerRuleException
     {
-        List<Profile> profiles = Arrays.asList( createProfile( "profile-1" ), createProfile( "profile-2" ) );
+        Map<String, List<String>> profiles =
+            Collections.singletonMap( "pom", Arrays.asList( "profile-1", "profile-2" ) );
 
-        when( project.getActiveProfiles() ).thenReturn( profiles );
+        when( project.getInjectedProfileIds() ).thenReturn( profiles );
 
         rule.setProfiles( "profile-2" );
 
@@ -122,9 +123,10 @@ public class RequireActiveProfileTest
     public void testTwoActiveProfilesWhereOneProfileIsRequiredToBeActivated()
         throws EnforcerRuleException
     {
-        List<Profile> profiles = Arrays.asList( createProfile( "profile-1" ), createProfile( "profile-2" ) );
+        Map<String, List<String>> profiles =
+                        Collections.singletonMap( "pom", Arrays.asList( "profile-1", "profile-2" ) );
 
-        when( project.getActiveProfiles() ).thenReturn( profiles );
+        when( project.getInjectedProfileIds() ).thenReturn( profiles );
 
         rule.setProfiles( "profile-2" );
         rule.setAll( true );
@@ -136,10 +138,10 @@ public class RequireActiveProfileTest
     public void testTwoActiveProfilesWithTwoRequiredProfilesWhereOneOfThemIsNotPartOfTheActiveProfiles()
         throws EnforcerRuleException, ExpressionEvaluationException
     {
+        Map<String, List<String>> profiles =
+                        Collections.singletonMap( "pom", Arrays.asList( "profile-X", "profile-Y" ) );
 
-        List<Profile> profiles = Arrays.asList( createProfile( "profile-X" ), createProfile( "profile-Y" ) );
-
-        when( project.getActiveProfiles() ).thenReturn( profiles );
+        when( project.getInjectedProfileIds() ).thenReturn( profiles );
 
         rule.setProfiles( "profile-Z,profile-X" );
         rule.setAll( true );
@@ -152,9 +154,10 @@ public class RequireActiveProfileTest
     public void testOneActiveProfilesWithTwoRequiredProfiles()
         throws EnforcerRuleException, ExpressionEvaluationException
     {
-        List<Profile> profiles = Collections.singletonList( createProfile( "profile-X" ) );
+        Map<String, List<String>> profiles =
+                        Collections.singletonMap( "pom", Arrays.asList( "profile-X" ) );
 
-        when( project.getActiveProfiles() ).thenReturn( profiles );
+        when( project.getInjectedProfileIds() ).thenReturn( profiles );
 
         rule.setProfiles( "profile-X,profile-Y" );
         rule.setAll( true );
@@ -167,9 +170,10 @@ public class RequireActiveProfileTest
     public void testOneActiveProfileWithTwoProfilesButNotAll()
         throws EnforcerRuleException, ExpressionEvaluationException
     {
-        List<Profile> profiles = Collections.singletonList( createProfile( "profile-X" ) );
+        Map<String, List<String>> profiles =
+                        Collections.singletonMap( "pom", Arrays.asList( "profile-X" ) );
 
-        when( project.getActiveProfiles() ).thenReturn( profiles );
+        when( project.getInjectedProfileIds() ).thenReturn( profiles );
 
         rule.setProfiles( "profile-X,profile-Y" );
         rule.setAll( false );
@@ -177,12 +181,4 @@ public class RequireActiveProfileTest
         rule.execute( helper );
         // intentionally no assertTrue(..)
     }
-
-    private Profile createProfile( String profileId )
-    {
-        Profile p = new Profile();
-        p.setId( profileId );
-        return p;
-    }
-
 }
diff --git a/maven-enforcer-plugin/src/it/projects/require-active-profile-inherited/invoker.properties b/maven-enforcer-plugin/src/it/projects/require-active-profile-inherited/invoker.properties
new file mode 100644
index 0000000..3018994
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-active-profile-inherited/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.goals = help:active-profiles enforcer:enforce
\ No newline at end of file
diff --git a/maven-enforcer-plugin/src/it/projects/require-active-profile-inherited/parent/pom.xml b/maven-enforcer-plugin/src/it/projects/require-active-profile-inherited/parent/pom.xml
new file mode 100644
index 0000000..d3e1838
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-active-profile-inherited/parent/pom.xml
@@ -0,0 +1,54 @@
+<?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.enforcer.it</groupId>
+  <artifactId>parent</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <rules>
+            <requireActiveProfile >
+              <profiles>active-by-default</profiles>
+            </requireActiveProfile>
+          </rules>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  
+  <profiles>
+    <profile>
+      <id>active-by-default</id>
+      <activation>
+        <activeByDefault>true</activeByDefault>
+      </activation>
+    </profile>
+  </profiles>
+  
+</project>
diff --git a/maven-enforcer-plugin/src/it/projects/require-active-profile-inherited/pom.xml b/maven-enforcer-plugin/src/it/projects/require-active-profile-inherited/pom.xml
new file mode 100644
index 0000000..c7f9b38
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-active-profile-inherited/pom.xml
@@ -0,0 +1,30 @@
+<?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>
+  <parent>
+    <groupId>org.apache.maven.plugins.enforcer.it</groupId>
+    <artifactId>parent</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <relativePath>parent/pom.xml</relativePath>
+  </parent>
+  <artifactId>menforcer257</artifactId>
+</project>