Coverage Report - org.apache.maven.report.projectinfo.PluginManagementReport
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginManagementReport
100 %
8/8
50 %
3/6
1,583
PluginManagementReport$PluginManagementRenderer
92 %
38/41
83 %
5/6
1,583
PluginManagementReport$PluginManagementRenderer$1
100 %
5/5
50 %
1/2
1,583
 
 1  
 package org.apache.maven.report.projectinfo;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.artifact.Artifact;
 23  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 24  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 25  
 import org.apache.maven.artifact.versioning.VersionRange;
 26  
 import org.apache.maven.doxia.sink.Sink;
 27  
 import org.apache.maven.model.Plugin;
 28  
 import org.apache.maven.plugin.logging.Log;
 29  
 import org.apache.maven.plugins.annotations.Component;
 30  
 import org.apache.maven.plugins.annotations.Mojo;
 31  
 import org.apache.maven.plugins.annotations.ResolutionScope;
 32  
 import org.apache.maven.project.MavenProject;
 33  
 import org.apache.maven.project.MavenProjectBuilder;
 34  
 import org.apache.maven.project.ProjectBuildingException;
 35  
 import org.codehaus.plexus.i18n.I18N;
 36  
 import org.codehaus.plexus.util.StringUtils;
 37  
 
 38  
 import java.util.ArrayList;
 39  
 import java.util.Collections;
 40  
 import java.util.Comparator;
 41  
 import java.util.List;
 42  
 import java.util.Locale;
 43  
 
 44  
 /**
 45  
  * Generates the Project Plugin Management report.
 46  
  *
 47  
  * @author Nick Stolwijk
 48  
  * @version $Id: PluginManagementReport.java 1367255 2012-07-30 20:01:21Z hboutemy $
 49  
  * @since 2.1
 50  
  */
 51  
 @Mojo( name = "plugin-management", requiresDependencyResolution = ResolutionScope.TEST )
 52  1
 public class PluginManagementReport
 53  
     extends AbstractProjectInfoReport
 54  
 {
 55  
     // ----------------------------------------------------------------------
 56  
     // Mojo components
 57  
     // ----------------------------------------------------------------------
 58  
 
 59  
     /**
 60  
      * Maven Project Builder component.
 61  
      */
 62  
     @Component
 63  
     private MavenProjectBuilder mavenProjectBuilder;
 64  
 
 65  
     /**
 66  
      * Maven Artifact Factory component.
 67  
      */
 68  
     @Component
 69  
     private ArtifactFactory artifactFactory;
 70  
 
 71  
     // ----------------------------------------------------------------------
 72  
     // Public methods
 73  
     // ----------------------------------------------------------------------
 74  
 
 75  
     @Override
 76  
     public void executeReport( Locale locale )
 77  
     {
 78  1
         PluginManagementRenderer r =
 79  
             new PluginManagementRenderer( getLog(), getSink(), locale, getI18N( locale ),
 80  
                                           project.getPluginManagement().getPlugins(), project, mavenProjectBuilder,
 81  
                                           artifactFactory, localRepository );
 82  1
         r.render();
 83  1
     }
 84  
 
 85  
     /** {@inheritDoc} */
 86  
     public String getOutputName()
 87  
     {
 88  2
         return "plugin-management";
 89  
     }
 90  
 
 91  
     @Override
 92  
     protected String getI18Nsection()
 93  
     {
 94  1
         return "pluginManagement";
 95  
     }
 96  
 
 97  
     @Override
 98  
     public boolean canGenerateReport()
 99  
     {
 100  2
         return project.getPluginManagement() != null && project.getPluginManagement().getPlugins() != null
 101  
             && !project.getPluginManagement().getPlugins().isEmpty();
 102  
     }
 103  
 
 104  
     // ----------------------------------------------------------------------
 105  
     // Private
 106  
     // ----------------------------------------------------------------------
 107  
 
 108  
     /**
 109  
      * Internal renderer class
 110  
      *
 111  
      * @author Nick Stolwijk
 112  
      */
 113  1
     protected static class PluginManagementRenderer
 114  
         extends AbstractProjectInfoRenderer
 115  
     {
 116  
         private final Log log;
 117  
 
 118  
         private final List<Plugin> pluginManagement;
 119  
 
 120  
         private final MavenProject project;
 121  
 
 122  
         private final MavenProjectBuilder mavenProjectBuilder;
 123  
 
 124  
         private final ArtifactFactory artifactFactory;
 125  
 
 126  
         private final ArtifactRepository localRepository;
 127  
 
 128  
         /**
 129  
          * @param log
 130  
          * @param sink
 131  
          * @param locale
 132  
          * @param i18n
 133  
          * @param plugins
 134  
          * @param project
 135  
          * @param mavenProjectBuilder
 136  
          * @param artifactFactory
 137  
          * @param localRepository
 138  
          */
 139  
         public PluginManagementRenderer( Log log, Sink sink, Locale locale, I18N i18n, List<Plugin> plugins,
 140  
                                          MavenProject project, MavenProjectBuilder mavenProjectBuilder,
 141  
                                          ArtifactFactory artifactFactory, ArtifactRepository localRepository )
 142  
         {
 143  1
             super( sink, i18n, locale );
 144  
 
 145  1
             this.log = log;
 146  
 
 147  1
             this.pluginManagement = plugins;
 148  
 
 149  1
             this.project = project;
 150  
 
 151  1
             this.mavenProjectBuilder = mavenProjectBuilder;
 152  
 
 153  1
             this.artifactFactory = artifactFactory;
 154  
 
 155  1
             this.localRepository = localRepository;
 156  1
         }
 157  
 
 158  
         @Override
 159  
         protected String getI18Nsection()
 160  
         {
 161  2
             return "pluginManagement";
 162  
         }
 163  
 
 164  
         @Override
 165  
         public void renderBody()
 166  
         {
 167  
             // === Section: Project PluginManagement.
 168  1
             renderSectionPluginManagement();
 169  1
         }
 170  
 
 171  
         private void renderSectionPluginManagement()
 172  
         {
 173  1
             String[] tableHeader = getPluginTableHeader();
 174  
 
 175  1
             startSection( getTitle() );
 176  
 
 177  
             // can't use straight artifact comparison because we want optional last
 178  1
             Collections.sort( pluginManagement, getPluginComparator() );
 179  
 
 180  1
             startTable();
 181  1
             tableHeader( tableHeader );
 182  
 
 183  1
             for ( Plugin plugin : pluginManagement )
 184  
             {
 185  
                 VersionRange versionRange;
 186  2
                 if ( StringUtils.isEmpty( plugin.getVersion() ) )
 187  
                 {
 188  1
                     versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION );
 189  
                 }
 190  
                 else
 191  
                 {
 192  1
                     versionRange = VersionRange.createFromVersion( plugin.getVersion() );
 193  
                 }
 194  
 
 195  2
                 Artifact pluginArtifact = artifactFactory.createParentArtifact( plugin.getGroupId(), plugin
 196  
                     .getArtifactId(), versionRange.toString() );
 197  
                 @SuppressWarnings( "unchecked" )
 198  2
                 List<ArtifactRepository> artifactRepositories = project.getPluginArtifactRepositories();
 199  2
                 if ( artifactRepositories == null )
 200  
                 {
 201  2
                     artifactRepositories = new ArrayList<ArtifactRepository>();
 202  
                 }
 203  
                 try
 204  
                 {
 205  2
                     MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact,
 206  
                                                                                           artifactRepositories,
 207  
                                                                                           localRepository );
 208  2
                     tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject
 209  
                         .getVersion(), pluginProject.getUrl() ) );
 210  
                 }
 211  0
                 catch ( ProjectBuildingException e )
 212  
                 {
 213  0
                     log.info( "Could not build project for: " + plugin.getArtifactId() + ":" + e.getMessage(), e );
 214  0
                     tableRow( getPluginRow( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), null ) );
 215  2
                 }
 216  
 
 217  2
             }
 218  1
             endTable();
 219  
 
 220  1
             endSection();
 221  1
         }
 222  
 
 223  
         // ----------------------------------------------------------------------
 224  
         // Private methods
 225  
         // ----------------------------------------------------------------------
 226  
 
 227  
         private String[] getPluginTableHeader()
 228  
         {
 229  
             // reused key...
 230  1
             String groupId = getI18nString( "dependencyManagement", "column.groupId" );
 231  1
             String artifactId = getI18nString( "dependencyManagement", "column.artifactId" );
 232  1
             String version = getI18nString( "dependencyManagement", "column.version" );
 233  1
             return new String[] { groupId, artifactId, version };
 234  
         }
 235  
 
 236  
         private String[] getPluginRow( String groupId, String artifactId, String version, String link )
 237  
         {
 238  2
             artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link );
 239  2
             return new String[] { groupId, artifactId, version };
 240  
         }
 241  
 
 242  
         private Comparator<Plugin> getPluginComparator()
 243  
         {
 244  1
             return new Comparator<Plugin>()
 245  2
             {
 246  
                 /** {@inheritDoc} */
 247  
                 public int compare( Plugin a1, Plugin a2 )
 248  
                 {
 249  1
                     int result = a1.getGroupId().compareTo( a2.getGroupId() );
 250  1
                     if ( result == 0 )
 251  
                     {
 252  1
                         result = a1.getArtifactId().compareTo( a2.getArtifactId() );
 253  
                     }
 254  1
                     return result;
 255  
                 }
 256  
             };
 257  
         }
 258  
     }
 259  
 }