Coverage Report - org.apache.maven.report.projectinfo.PluginsReport
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginsReport
0 %
0/8
0 %
0/8
2,167
PluginsReport$PluginsRenderer
0 %
0/48
0 %
0/16
2,167
PluginsReport$PluginsRenderer$1
0 %
0/5
0 %
0/2
2,167
 
 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.plugin.logging.Log;
 28  
 import org.apache.maven.plugins.annotations.Component;
 29  
 import org.apache.maven.plugins.annotations.Mojo;
 30  
 import org.apache.maven.plugins.annotations.ResolutionScope;
 31  
 import org.apache.maven.project.MavenProject;
 32  
 import org.apache.maven.project.MavenProjectBuilder;
 33  
 import org.apache.maven.project.ProjectBuildingException;
 34  
 import org.codehaus.plexus.i18n.I18N;
 35  
 import org.codehaus.plexus.util.StringUtils;
 36  
 
 37  
 import java.util.ArrayList;
 38  
 import java.util.Collections;
 39  
 import java.util.Comparator;
 40  
 import java.util.List;
 41  
 import java.util.Locale;
 42  
 import java.util.Set;
 43  
 
 44  
 /**
 45  
  * Generates the Project Plugins report.
 46  
  *
 47  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 48  
  * @version $Id: PluginsReport.java 1367255 2012-07-30 20:01:21Z hboutemy $
 49  
  * @since 2.1
 50  
  */
 51  
 @Mojo( name = "plugins", requiresDependencyResolution = ResolutionScope.TEST )
 52  0
 public class PluginsReport
 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  
         @SuppressWarnings( "unchecked" )
 79  0
         PluginsRenderer r =
 80  
             new PluginsRenderer( getLog(), getSink(), locale, getI18N( locale ), project.getPluginArtifacts(),
 81  
                                  project.getReportArtifacts(), project, mavenProjectBuilder, artifactFactory,
 82  
                                  localRepository );
 83  0
         r.render();
 84  0
     }
 85  
 
 86  
     /** {@inheritDoc} */
 87  
     public String getOutputName()
 88  
     {
 89  0
         return "plugins";
 90  
     }
 91  
 
 92  
     @Override
 93  
     protected String getI18Nsection()
 94  
     {
 95  0
         return "plugins";
 96  
     }
 97  
 
 98  
     @Override
 99  
     public boolean canGenerateReport()
 100  
     {
 101  0
         return ( project.getPluginArtifacts() != null && !project.getPluginArtifacts().isEmpty() )
 102  
             || ( project.getReportArtifacts() != null && !project.getReportArtifacts().isEmpty() );
 103  
     }
 104  
 
 105  
     // ----------------------------------------------------------------------
 106  
     // Private
 107  
     // ----------------------------------------------------------------------
 108  
 
 109  
     /**
 110  
      * Internal renderer class
 111  
      */
 112  0
     protected static class PluginsRenderer
 113  
         extends AbstractProjectInfoRenderer
 114  
     {
 115  
         private final Log log;
 116  
 
 117  
         private final List<Artifact> plugins;
 118  
 
 119  
         private final List<Artifact> reports;
 120  
 
 121  
         private final MavenProject project;
 122  
 
 123  
         private final MavenProjectBuilder mavenProjectBuilder;
 124  
 
 125  
         private final ArtifactFactory artifactFactory;
 126  
 
 127  
         private final ArtifactRepository localRepository;
 128  
 
 129  
         /**
 130  
          * @param log
 131  
          * @param sink
 132  
          * @param locale
 133  
          * @param i18n
 134  
          * @param plugins
 135  
          * @param reports
 136  
          * @param project
 137  
          * @param mavenProjectBuilder
 138  
          * @param artifactFactory
 139  
          * @param localRepository
 140  
          */
 141  
         public PluginsRenderer( Log log, Sink sink, Locale locale, I18N i18n, Set<Artifact> plugins,
 142  
                                 Set<Artifact> reports, MavenProject project, MavenProjectBuilder mavenProjectBuilder,
 143  
                                 ArtifactFactory artifactFactory, ArtifactRepository localRepository )
 144  
         {
 145  0
             super( sink, i18n, locale );
 146  
 
 147  0
             this.log = log;
 148  
 
 149  0
             this.plugins = new ArrayList<Artifact>( plugins );
 150  
 
 151  0
             this.reports = new ArrayList<Artifact>( reports );
 152  
 
 153  0
             this.project = project;
 154  
 
 155  0
             this.mavenProjectBuilder = mavenProjectBuilder;
 156  
 
 157  0
             this.artifactFactory = artifactFactory;
 158  
 
 159  0
             this.localRepository = localRepository;
 160  0
         }
 161  
 
 162  
         @Override
 163  
         protected String getI18Nsection()
 164  
         {
 165  0
             return "plugins";
 166  
         }
 167  
 
 168  
         @Override
 169  
         public void renderBody()
 170  
         {
 171  
             // === Section: Project Plugins.
 172  0
             renderSectionPlugins( true );
 173  
 
 174  
             // === Section: Project Reports.
 175  0
             renderSectionPlugins( false );
 176  0
         }
 177  
 
 178  
         /**
 179  
          * @param isPlugins <code>true</code> to use <code>plugins</code> variable, <code>false</code> to use
 180  
          * <code>reports</code> variable.
 181  
          */
 182  
         private void renderSectionPlugins( boolean isPlugins )
 183  
         {
 184  0
             List<Artifact> list = ( isPlugins ? plugins : reports );
 185  0
             String[] tableHeader = getPluginTableHeader();
 186  
 
 187  0
             startSection( ( isPlugins ? getI18nString( "title" )
 188  
                                      : getI18nString( "report.title" ) ) );
 189  
 
 190  0
             if ( list == null || list.isEmpty() )
 191  
             {
 192  
 
 193  0
                 paragraph(  ( isPlugins ? getI18nString( "nolist" )
 194  
                                         : getI18nString( "report.nolist" ) ) );
 195  
 
 196  0
                 endSection();
 197  
 
 198  0
                 return;
 199  
             }
 200  
 
 201  0
             Collections.sort( list, getArtifactComparator() );
 202  
 
 203  0
             startTable();
 204  0
             tableHeader( tableHeader );
 205  
 
 206  0
             for ( Artifact artifact : list )
 207  
             {
 208  
                 VersionRange versionRange;
 209  0
                 if ( StringUtils.isEmpty( artifact.getVersion() ) )
 210  
                 {
 211  0
                     versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION );
 212  
                 }
 213  
                 else
 214  
                 {
 215  0
                     versionRange = VersionRange.createFromVersion( artifact.getVersion() );
 216  
                 }
 217  
 
 218  0
                 Artifact pluginArtifact = artifactFactory.createParentArtifact( artifact.getGroupId(), artifact
 219  
                     .getArtifactId(), versionRange.toString() );
 220  
                 @SuppressWarnings( "unchecked" )
 221  0
                 List<ArtifactRepository> artifactRepositories = project.getPluginArtifactRepositories();
 222  0
                 if ( artifactRepositories == null )
 223  
                 {
 224  0
                     artifactRepositories = new ArrayList<ArtifactRepository>();
 225  
                 }
 226  
                 try
 227  
                 {
 228  0
                     MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact,
 229  
                                                                                           artifactRepositories,
 230  
                                                                                           localRepository );
 231  0
                     tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject
 232  
                                             .getVersion(), pluginProject.getUrl() ) );
 233  
                 }
 234  0
                 catch ( ProjectBuildingException e )
 235  
                 {
 236  0
                     log.info( "Could not build project for: " + artifact.getArtifactId() + ":" + e.getMessage(), e );
 237  0
                     tableRow( getPluginRow( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
 238  
                                             null ) );
 239  0
                 }
 240  
 
 241  0
             }
 242  0
             endTable();
 243  
 
 244  0
             endSection();
 245  0
         }
 246  
 
 247  
         // ----------------------------------------------------------------------
 248  
         // Private methods
 249  
         // ----------------------------------------------------------------------
 250  
 
 251  
         private String[] getPluginTableHeader()
 252  
         {
 253  
             // reused key...
 254  0
             String groupId = getI18nString( "dependencyManagement", "column.groupId" );
 255  0
             String artifactId = getI18nString( "dependencyManagement", "column.artifactId" );
 256  0
             String version = getI18nString( "dependencyManagement", "column.version" );
 257  0
             return new String[] { groupId, artifactId, version };
 258  
         }
 259  
 
 260  
         private String[] getPluginRow( String groupId, String artifactId, String version, String link )
 261  
         {
 262  0
             artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link );
 263  0
             return new String[] { groupId, artifactId, version };
 264  
         }
 265  
 
 266  
         private Comparator<Artifact> getArtifactComparator()
 267  
         {
 268  0
             return new Comparator<Artifact>()
 269  0
             {
 270  
                 /** {@inheritDoc} */
 271  
                 public int compare( Artifact a1, Artifact a2 )
 272  
                 {
 273  0
                     int result = a1.getGroupId().compareTo( a2.getGroupId() );
 274  0
                     if ( result == 0 )
 275  
                     {
 276  0
                         result = a1.getArtifactId().compareTo( a2.getArtifactId() );
 277  
                     }
 278  0
                     return result;
 279  
                 }
 280  
             };
 281  
         }
 282  
     }
 283  
 }