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