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