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