Author: hboutemy
Date: Sun Aug 23 20:52:58 2015
New Revision: 1697251

URL: http://svn.apache.org/r1697251
Log:
[MPIR-300] fixed plugins report: report plugins with version defined in 
pluginManagement was wrong

Added:
    maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/
    
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/invoker.properties
   (with props)
    
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/pom.xml   
(with props)
    
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/verify.groovy
   (with props)
Modified:
    
maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/PluginsReport.java

Added: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/invoker.properties
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/invoker.properties?rev=1697251&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/invoker.properties
 (added)
+++ 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/invoker.properties
 Sun Aug 23 20:52:58 2015
@@ -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}:plugins
+invoker.maven.version = 3.0+

Propchange: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/invoker.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/invoker.properties
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/invoker.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/pom.xml?rev=1697251&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/pom.xml 
(added)
+++ 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/pom.xml 
Sun Aug 23 20:52:58 2015
@@ -0,0 +1,113 @@
+<?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.project-info-reports.its</groupId>
+  <artifactId>mpir-300</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <name>plugins report: check that report with version defined in 
pluginManagement is properly detected</name>
+  <description>see MPIR-300: version injection in report plugins from 
pluginManagement works with Maven 3 (MSITE-443),
+  implemented in maven-reporting-exec.
+  Same version detection algorithm has to be put in MPIR plugins report for 
report plugins.</description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-invoker-plugin</artifactId>
+          <version>1.invoker-pluginManagement</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-javadoc-plugin</artifactId>
+          <version>1.javadoc-pluginManagement</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-changes-plugin</artifactId>
+          <version>1.changes-pluginManagement</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-javadoc-plugin</artifactId>
+        <version>2.javadoc-plugin</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-checkstyle-plugin</artifactId>
+        <version>2.checkstyle-plugin</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-changes-plugin</artifactId>
+        <version>2.changes-plugin</version>
+      </plugin>
+    </plugins>
+  </build>
+
+  <reporting>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-project-info-reports-plugin</artifactId>
+        <version>@pom.version@</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-invoker-plugin</artifactId>
+        <!-- version will be injected from pluginManagement section => 
1.invoker-pluginManagement -->
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-javadoc-plugin</artifactId>
+        <!-- version will be injected from plugin section => 2.javadoc-plugin 
-->
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-checkstyle-plugin</artifactId>
+        <!-- version will be injected from plugin section => 
2.checkstyle-plugin -->
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-changes-plugin</artifactId>
+        <version>3.changes-reporting</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jxr-plugin</artifactId>
+        <!-- no version defined anywhere -->
+      </plugin>
+    </plugins>
+  </reporting>
+</project>

Propchange: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/verify.groovy
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/verify.groovy?rev=1697251&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/verify.groovy
 (added)
+++ 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/verify.groovy
 Sun Aug 23 20:52:58 2015
@@ -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.
+ */
+
+plugins = new File( basedir, 'target/site/plugins.html' ).text;
+
+// version of maven-invoker-plugin is defined in pluginManagement
+assert plugins.contains( '<td>1.invoker-pluginManagement</td>' );
+
+// version of maven-javadoc-plugin is defined in plugins (overriding 
pluginManagement)
+assert plugins.contains( '<td>2.javadoc-plugin</td>' );
+
+// version of maven-checkstyle-plugin is defined in plugins
+assert plugins.contains( '<td>2.checkstyle-plugin</td>' );
+
+// version of maven-changes-plugin is defined in reporting (overriding 
pluginManagement and plugins)
+assert plugins.contains( '<td>3.changes-reporting</td>' );
+
+return true;
\ No newline at end of file

Propchange: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/verify.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/it/MPIR-300/verify.groovy
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: 
maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/PluginsReport.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/PluginsReport.java?rev=1697251&r1=1697250&r2=1697251&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/PluginsReport.java
 (original)
+++ 
maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/PluginsReport.java
 Sun Aug 23 20:52:58 2015
@@ -24,13 +24,14 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Locale;
-import java.util.Set;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.versioning.VersionRange;
 import org.apache.maven.doxia.sink.Sink;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.ReportPlugin;
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Mojo;
@@ -78,8 +79,7 @@ public class PluginsReport
         boolean result = super.canGenerateReport();
         if ( result && skipEmptyReport )
         {
-            result = !isEmpty( getProject().getPluginArtifacts() )
-                    || !isEmpty( getProject().getReportArtifacts() );
+            result = !isEmpty( getProject().getBuildPlugins() ) || !isEmpty( 
getProject().getReportPlugins() );
         }
 
         return result;
@@ -90,8 +90,8 @@ public class PluginsReport
     {
         @SuppressWarnings( "unchecked" )
         PluginsRenderer r =
-            new PluginsRenderer( getLog(), getSink(), locale, getI18N( locale 
), project.getPluginArtifacts(),
-                                 project.getReportArtifacts(), project, 
mavenProjectBuilder, artifactFactory,
+            new PluginsRenderer( getLog(), getSink(), locale, getI18N( locale 
), project.getBuildPlugins(),
+                                 project.getReportPlugins(), project, 
mavenProjectBuilder, artifactFactory,
                                  localRepository );
         r.render();
     }
@@ -120,9 +120,9 @@ public class PluginsReport
     {
         private final Log log;
 
-        private final List<Artifact> plugins;
+        private final List<Plugin> plugins;
 
-        private final List<Artifact> reports;
+        private final List<ReportPlugin> reports;
 
         private final MavenProject project;
 
@@ -145,17 +145,18 @@ public class PluginsReport
          * @param localRepository {@link ArtifactRepository}
          *
          */
-        public PluginsRenderer( Log log, Sink sink, Locale locale, I18N i18n, 
Set<Artifact> plugins,
-                                Set<Artifact> reports, MavenProject project, 
MavenProjectBuilder mavenProjectBuilder,
-                                ArtifactFactory artifactFactory, 
ArtifactRepository localRepository )
+        public PluginsRenderer( Log log, Sink sink, Locale locale, I18N i18n, 
List<Plugin> plugins,
+                                List<ReportPlugin> reports, MavenProject 
project,
+                                MavenProjectBuilder mavenProjectBuilder, 
ArtifactFactory artifactFactory,
+                                ArtifactRepository localRepository )
         {
             super( sink, i18n, locale );
 
             this.log = log;
 
-            this.plugins = new ArrayList<Artifact>( plugins );
+            this.plugins = new ArrayList<Plugin>( plugins );
 
-            this.reports = new ArrayList<Artifact>( reports );
+            this.reports = new ArrayList<ReportPlugin>( reports );
 
             this.project = project;
 
@@ -188,7 +189,7 @@ public class PluginsReport
          */
         private void renderSectionPlugins( boolean isPlugins )
         {
-            List<Artifact> list = ( isPlugins ? plugins : reports );
+            List<GAV> list = ( isPlugins ? GAV.pluginsToGAV( plugins ) : 
GAV.reportPluginsToGAV( reports ) );
             String[] tableHeader = getPluginTableHeader();
 
             startSection( ( isPlugins ? getI18nString( "title" )
@@ -205,25 +206,28 @@ public class PluginsReport
                 return;
             }
 
-            Collections.sort( list, getArtifactComparator() );
+            Collections.sort( list, getPluginComparator() );
 
             startTable();
             tableHeader( tableHeader );
 
-            for ( Artifact artifact : list )
+            for ( GAV plugin : list )
             {
-                VersionRange versionRange;
-                if ( StringUtils.isEmpty( artifact.getVersion() ) )
+                String version;
+                if ( isPlugins )
                 {
-                    versionRange = VersionRange.createFromVersion( 
Artifact.RELEASE_VERSION );
+                    version =
+                        StringUtils.isEmpty( plugin.getVersion() ) ? 
Artifact.RELEASE_VERSION : plugin.getVersion();
                 }
                 else
                 {
-                    versionRange = VersionRange.createFromVersion( 
artifact.getVersion() );
+                    version = resolveReportPluginVersion( plugin, project );
                 }
+                VersionRange versionRange = VersionRange.createFromVersion( 
version );
 
-                Artifact pluginArtifact = 
artifactFactory.createParentArtifact( artifact.getGroupId(), artifact
-                    .getArtifactId(), versionRange.toString() );
+                Artifact pluginArtifact =
+                    artifactFactory.createParentArtifact( plugin.getGroupId(), 
plugin.getArtifactId(),
+                                                          
versionRange.toString() );
                 @SuppressWarnings( "unchecked" )
                 List<ArtifactRepository> artifactRepositories = 
project.getPluginArtifactRepositories();
                 if ( artifactRepositories == null )
@@ -240,8 +244,8 @@ public class PluginsReport
                 }
                 catch ( ProjectBuildingException e )
                 {
-                    log.info( "Could not build project for: " + 
artifact.getArtifactId() + ":" + e.getMessage(), e );
-                    tableRow( getPluginRow( artifact.getGroupId(), 
artifact.getArtifactId(), artifact.getVersion(),
+                    log.info( "Could not build project for " + 
plugin.getArtifactId() + ": " + e.getMessage(), e );
+                    tableRow( getPluginRow( plugin.getGroupId(), 
plugin.getArtifactId(), plugin.getVersion(),
                                             null ) );
                 }
 
@@ -270,21 +274,149 @@ public class PluginsReport
             return new String[] { groupId, artifactId, version };
         }
 
-        private Comparator<Artifact> getArtifactComparator()
+        private static class GAV
         {
-            return new Comparator<Artifact>()
+            private final String groupId;
+            private final String artifactId;
+            private final String version;
+
+            private GAV( Plugin plugin )
+            {
+                groupId = plugin.getGroupId();
+                artifactId = plugin.getArtifactId();
+                version = plugin.getVersion();
+            }
+
+            private GAV( ReportPlugin reportPlugin )
+            {
+                groupId = reportPlugin.getGroupId();
+                artifactId = reportPlugin.getArtifactId();
+                version = reportPlugin.getVersion();
+            }
+
+            public String getGroupId()
+            {
+                return groupId;
+            }
+
+            public String getArtifactId()
+            {
+                return artifactId;
+            }
+
+            public String getVersion()
+            {
+                return version;
+            }
+
+            public static List<GAV> pluginsToGAV( List<Plugin> plugins )
+            {
+                List<GAV> result = new ArrayList<GAV>( plugins.size() );
+                for ( Plugin plugin : plugins )
+                {
+                    result.add( new GAV( plugin ) );
+                }
+                return result;
+            }
+
+            public static List<GAV> reportPluginsToGAV( List<ReportPlugin> 
reportPlugins )
+            {
+                List<GAV> result = new ArrayList<GAV>( reportPlugins.size() );
+                for ( ReportPlugin reportPlugin : reportPlugins )
+                {
+                    result.add( new GAV( reportPlugin ) );
+                }
+                return result;
+            }
+        }
+
+        private Comparator<GAV> getPluginComparator()
+        {
+            return new Comparator<GAV>()
             {
                 /** {@inheritDoc} */
-                public int compare( Artifact a1, Artifact a2 )
+                public int compare( GAV a1, GAV a2 )
                 {
-                    int result = a1.getGroupId().compareTo( a2.getGroupId() );
+                    int result = a1.groupId.compareTo( a2.groupId );
                     if ( result == 0 )
                     {
-                        result = a1.getArtifactId().compareTo( 
a2.getArtifactId() );
+                        result = a1.artifactId.compareTo( a2.artifactId );
                     }
                     return result;
                 }
             };
         }
+
+        /**
+         * Resolve report plugin version. Steps to find a plugin version stop 
after each step if a non <code>null</code>
+         * value has been found:
+         * <ol>
+         * <li>use the one defined in the reportPlugin configuration,</li>
+         * <li>search similar (same groupId and artifactId) mojo in the 
build/plugins section of the pom,</li>
+         * <li>search similar (same groupId and artifactId) mojo in the 
build/pluginManagement section of the pom,</li>
+         * <li>default value is RELEASE.</li>
+         * </ol>
+         * 
+         * @param reportPlugin the report plugin to resolve the version
+         * @param project the current project
+         * @return the report plugin version
+         */
+        protected String resolveReportPluginVersion( GAV reportPlugin, 
MavenProject project )
+        {
+            // look for version defined in the reportPlugin configuration
+            if ( reportPlugin.getVersion() != null )
+            {
+                return reportPlugin.getVersion();
+            }
+
+            // search in the build section
+            if ( project.getBuild() != null )
+            {
+                Plugin plugin = find( reportPlugin, 
project.getBuild().getPlugins() );
+
+                if ( plugin != null && plugin.getVersion() != null )
+                {
+                    return plugin.getVersion();
+                }
+            }
+
+            // search in pluginManagement section
+            if ( project.getBuild() != null && 
project.getBuild().getPluginManagement() != null )
+            {
+                Plugin plugin = find( reportPlugin, 
project.getBuild().getPluginManagement().getPlugins() );
+
+                if ( plugin != null && plugin.getVersion() != null )
+                {
+                    return plugin.getVersion();
+                }
+            }
+
+            // empty version
+            return Artifact.RELEASE_VERSION;
+        }
+
+        /**
+         * Search similar (same groupId and artifactId) plugin as a given 
report plugin.
+         * 
+         * @param reportPlugin the report plugin to search for a similar plugin
+         * @param plugins the candidate plugins
+         * @return the first similar plugin
+         */
+        private Plugin find( GAV reportPlugin, List<Plugin> plugins )
+        {
+            if ( plugins == null )
+            {
+                return null;
+            }
+            for ( Plugin plugin : plugins )
+            {
+                if ( StringUtils.equals( plugin.getArtifactId(), 
reportPlugin.getArtifactId() )
+                    && StringUtils.equals( plugin.getGroupId(), 
reportPlugin.getGroupId() ) )
+                {
+                    return plugin;
+                }
+            }
+            return null;
+        }
     }
 }


Reply via email to