View Javadoc

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