You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gb...@apache.org on 2017/07/04 20:17:57 UTC

svn commit: r1800814 - in /maven/plugins/trunk/maven-help-plugin/src: it/all-profiles-parent-pom/ it/all-profiles-parent-pom/child/ main/java/org/apache/maven/plugins/help/ test/java/org/apache/maven/plugins/help/

Author: gboue
Date: Tue Jul  4 20:17:57 2017
New Revision: 1800814

URL: http://svn.apache.org/viewvc?rev=1800814&view=rev
Log:
[MPH-123] all-profiles does not show right active status

MavenProject.getActiveProfiles only considers profiles from the project POM, so we also need to get the active profiles from the parents. With Maven 3, while it would be possible to use MavenProject.getInjectedProfileIds, it is more direct and symmetrical here to walk the parent chain ourselves (and makes sure the printed 'source' is consistent for active and inactive profiles from the settings).

Added:
    maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/
    maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/child/
    maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/child/pom.xml   (with props)
    maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/invoker.properties   (with props)
    maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/pom.xml   (with props)
    maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/verify.groovy   (with props)
Modified:
    maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java
    maven/plugins/trunk/maven-help-plugin/src/test/java/org/apache/maven/plugins/help/AllProfilesMojoTest.java

Added: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/child/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/child/pom.xml?rev=1800814&view=auto
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/child/pom.xml (added)
+++ maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/child/pom.xml Tue Jul  4 20:17:57 2017
@@ -0,0 +1,43 @@
+<?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>
+  <parent>
+    <groupId>org.apache.maven.its.help</groupId>
+    <artifactId>all-profiles-parent-pom</artifactId>
+    <version>1.0</version>
+  </parent>
+  <artifactId>child</artifactId>
+  <profiles>
+    <profile>
+      <id>profile-active-from-child-pom</id>
+      <activation>
+        <file>
+          <exists>pom.xml</exists>
+        </file>
+      </activation>
+    </profile>
+    <profile>
+      <id>profile-inactive-from-child-pom</id>
+    </profile>
+  </profiles>
+</project>

Propchange: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/child/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/child/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/invoker.properties
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/invoker.properties?rev=1800814&view=auto
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/invoker.properties (added)
+++ maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/invoker.properties Tue Jul  4 20:17:57 2017
@@ -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 = ${project.groupId}:${project.artifactId}:${project.version}:all-profiles
+invoker.maven.version = 3.0-, 3.0.4+

Propchange: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/invoker.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/invoker.properties
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/pom.xml?rev=1800814&view=auto
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/pom.xml (added)
+++ maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/pom.xml Tue Jul  4 20:17:57 2017
@@ -0,0 +1,45 @@
+<?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.help</groupId>
+  <artifactId>all-profiles-parent-pom</artifactId>
+  <version>1.0</version>
+  <packaging>pom</packaging>
+  <description>Tests that profiles active in a parent POM are shown as active in all-profiles goal</description>
+  <modules>
+    <module>child</module>
+  </modules>
+  <profiles>
+    <profile>
+      <id>profile-active-from-parent-pom</id>
+      <activation>
+        <file>
+          <exists>pom.xml</exists>
+        </file>
+      </activation>
+    </profile>
+    <profile>
+      <id>profile-inactive-from-parent-pom</id>
+    </profile>
+  </profiles>
+</project>

Propchange: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/verify.groovy
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/verify.groovy?rev=1800814&view=auto
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/verify.groovy (added)
+++ maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/verify.groovy Tue Jul  4 20:17:57 2017
@@ -0,0 +1,28 @@
+/*
+ * 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.exists()
+
+// 2 = parent + child
+assert 2 == buildLog.text.count( '  Profile Id: it-repo (Active: true , Source: settings.xml)' )
+assert 2 == buildLog.text.count( '  Profile Id: profile-active-from-parent-pom (Active: true , Source: pom)' ) 
+assert 2 == buildLog.text.count( '  Profile Id: profile-inactive-from-parent-pom (Active: false , Source: pom)' )
+assert 1 == buildLog.text.count( '  Profile Id: profile-active-from-child-pom (Active: true , Source: pom)' ) 
+assert 1 == buildLog.text.count( '  Profile Id: profile-inactive-from-child-pom (Active: false , Source: pom)' )

Propchange: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/verify.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-help-plugin/src/it/all-profiles-parent-pom/verify.groovy
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java?rev=1800814&r1=1800813&r2=1800814&view=diff
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java (original)
+++ maven/plugins/trunk/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/AllProfilesMojo.java Tue Jul  4 20:17:57 2017
@@ -78,8 +78,9 @@ public class AllProfilesMojo
             descriptionBuffer.append( "Listing Profiles for Project: " ).append( project.getId() ).append( LS );
             
             Map<String, Profile> allProfilesByIds = new HashMap<String, Profile>();
+            Map<String, Profile> activeProfilesByIds = new HashMap<String, Profile>();
             addSettingsProfiles( allProfilesByIds );
-            addProjectPomProfiles( project, allProfilesByIds );
+            addProjectPomProfiles( project, allProfilesByIds, activeProfilesByIds );
 
             // now display
             if ( allProfilesByIds.isEmpty() )
@@ -89,29 +90,10 @@ public class AllProfilesMojo
             else
             {
                 // active Profiles will be a subset of *all* profiles
-                List<Profile> activeProfiles = project.getActiveProfiles();
-                for ( Profile activeProfile : activeProfiles )
-                {
-                    // we already have the active profiles for the project, so remove them from the list of all
-                    // profiles.
-                    allProfilesByIds.remove( activeProfile.getId() );
-                }
-
-                // display active profiles
-                for ( Profile p : activeProfiles )
-                {
-                    descriptionBuffer.append( "  Profile Id: " ).append( p.getId() );
-                    descriptionBuffer.append( " (Active: true , Source: " ).append( p.getSource() ).append( ")" );
-                    descriptionBuffer.append( LS );
-                }
+                allProfilesByIds.keySet().removeAll( activeProfilesByIds.keySet() );
 
-                // display inactive profiles
-                for ( Profile p : allProfilesByIds.values() )
-                {
-                    descriptionBuffer.append( "  Profile Id: " ).append( p.getId() );
-                    descriptionBuffer.append( " (Active: false , Source: " ).append( p.getSource() ).append( ")" );
-                    descriptionBuffer.append( LS );
-                }
+                writeProfilesDescription( descriptionBuffer, activeProfilesByIds, true );
+                writeProfilesDescription( descriptionBuffer, allProfilesByIds, false );
             }
         }
 
@@ -138,13 +120,25 @@ public class AllProfilesMojo
     // Private methods
     // ----------------------------------------------------------------------
 
+    private void writeProfilesDescription( StringBuilder sb, Map<String, Profile> profilesByIds, boolean active )
+    {
+        for ( Profile p : profilesByIds.values() )
+        {
+            sb.append( "  Profile Id: " ).append( p.getId() );
+            sb.append( " (Active: " + active + " , Source: " ).append( p.getSource() ).append( ")" );
+            sb.append( LS );
+        }
+    }
+
     /**
      * Adds the profiles from <code>pom.xml</code> and all of its parents.
      *
      * @param project could be null
      * @param allProfiles Map to add the profiles to.
+     * @param activeProfiles Map to add the active profiles to.
      */
-    private void addProjectPomProfiles( MavenProject project, Map<String, Profile> allProfiles )
+    private void addProjectPomProfiles( MavenProject project, Map<String, Profile> allProfiles,
+                                        Map<String, Profile> activeProfiles )
     {
         if ( project == null )
         {
@@ -155,19 +149,20 @@ public class AllProfilesMojo
 
         getLog().debug( "Attempting to read profiles from pom.xml..." );
 
-        for ( Profile profile : project.getModel().getProfiles() )
-        {
-            allProfiles.put( profile.getId(), profile );
-        }
-
-        MavenProject parent = project.getParent();
-        while ( parent != null )
+        while ( project != null )
         {
-            for ( Profile profile : parent.getModel().getProfiles() )
+            for ( Profile profile : project.getModel().getProfiles() )
             {
                 allProfiles.put( profile.getId(), profile );
             }
-            parent = parent.getParent();
+            if ( project.getActiveProfiles() != null )
+            {
+                for ( Profile profile : project.getActiveProfiles() )
+                {
+                    activeProfiles.put( profile.getId(), profile );
+                }
+            }
+            project = project.getParent();
         }
     }
 

Modified: maven/plugins/trunk/maven-help-plugin/src/test/java/org/apache/maven/plugins/help/AllProfilesMojoTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-help-plugin/src/test/java/org/apache/maven/plugins/help/AllProfilesMojoTest.java?rev=1800814&r1=1800813&r2=1800814&view=diff
==============================================================================
--- maven/plugins/trunk/maven-help-plugin/src/test/java/org/apache/maven/plugins/help/AllProfilesMojoTest.java (original)
+++ maven/plugins/trunk/maven-help-plugin/src/test/java/org/apache/maven/plugins/help/AllProfilesMojoTest.java Tue Jul  4 20:17:57 2017
@@ -104,6 +104,32 @@ public class AllProfilesMojoTest
         assertTrue( file.contains( "Profile Id: pro-2 (Active: false , Source: pom)" ) );
         assertTrue( file.contains( "Profile Id: pro-3 (Active: false , Source: pom)" ) );
     }
+
+    /**
+     * Tests the case when active profiles are present in the parent POM.
+     * 
+     * @throws Exception in case of errors.
+     */
+    public void testProfileFromParentPom()
+        throws Exception
+    {
+        File testPom = new File( getBasedir(), "target/test-classes/unit/all-profiles/plugin-config.xml" );
+
+        AllProfilesMojo mojo = (AllProfilesMojo) lookupMojo( "all-profiles", testPom );
+
+        MavenProjectStub project = new MavenProjectStub();
+        project.setParent( new MavenProjectStub() );
+        project.getParent().getModel().setProfiles( Arrays.asList( newPomProfile( "pro-1", "pom" ) ) );
+        project.getParent().setActiveProfiles( Arrays.asList( newPomProfile( "pro-1", "pom" ) ) );
+        
+        setUpMojo( mojo, Arrays.<MavenProject>asList( project ),
+                   Collections.<org.apache.maven.settings.Profile>emptyList(), "profiles-from-parent-pom.txt" );
+
+        mojo.execute();
+
+        String file = readFile( "profiles-from-parent-pom.txt" );
+        assertTrue( file.contains( "Profile Id: pro-1 (Active: true , Source: pom)" ) );
+    }
     
     /**
      * Tests the case when profiles are present in the settings.