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.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  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          return i18n.getString( "project-info-report", locale, "report.plugins.name" );
81      }
82  
83      /** {@inheritDoc} */
84      public String getDescription( Locale locale )
85      {
86          return i18n.getString( "project-info-report", locale, "report.plugins.description" );
87      }
88  
89      /** {@inheritDoc} */
90      public void executeReport( Locale locale )
91      {
92          PluginsRenderer r = new PluginsRenderer( getLog(), getSink(), locale, i18n, project.getPluginArtifacts(),
93                                                   project.getReportArtifacts(), project, mavenProjectBuilder,
94                                                   artifactFactory, localRepository );
95          r.render();
96      }
97  
98      /** {@inheritDoc} */
99      public String getOutputName()
100     {
101         return "plugins";
102     }
103 
104     /** {@inheritDoc} */
105     public boolean canGenerateReport()
106     {
107         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     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             super( sink );
156 
157             this.log = log;
158 
159             this.locale = locale;
160 
161             this.plugins = new ArrayList( plugins );
162 
163             this.reports = new ArrayList( reports );
164 
165             this.i18n = i18n;
166 
167             this.project = project;
168 
169             this.mavenProjectBuilder = mavenProjectBuilder;
170 
171             this.artifactFactory = artifactFactory;
172 
173             this.localRepository = localRepository;
174         }
175 
176         /** {@inheritDoc} */
177         public String getTitle()
178         {
179             return getReportString( "report.plugins.title" );
180         }
181 
182         /** {@inheritDoc} */
183         public void renderBody()
184         {
185             // === Section: Project Plugins.
186             renderSectionPlugins( true );
187 
188             // === Section: Project Reports.
189             renderSectionPlugins( false );
190         }
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             List list = ( isPlugins ? plugins : reports );
199             String[] tableHeader = getPluginTableHeader();
200 
201             startSection( ( isPlugins ? getReportString( "report.plugins.title" )
202                                      : getReportString( "report.plugins.report.title" ) ) );
203 
204             if ( list == null || list.isEmpty() )
205             {
206 
207                 paragraph(  ( isPlugins ? getReportString( "report.plugins.nolist" )
208                                         : getReportString( "report.plugins.report.nolist" ) ) );
209 
210                 endSection();
211 
212                 return;
213             }
214 
215             Collections.sort( list, getArtifactComparator() );
216 
217             startTable();
218             tableHeader( tableHeader );
219 
220             for ( Iterator iterator = list.iterator(); iterator.hasNext(); )
221             {
222                 Artifact artifact = (Artifact) iterator.next();
223 
224                 VersionRange versionRange;
225                 if ( StringUtils.isEmpty( artifact.getVersion() ) )
226                 {
227                     versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION );
228                 }
229                 else
230                 {
231                     versionRange = VersionRange.createFromVersion( artifact.getVersion() );
232                 }
233 
234                 Artifact pluginArtifact = artifactFactory.createParentArtifact( artifact.getGroupId(), artifact
235                     .getArtifactId(), versionRange.toString() );
236                 List artifactRepositories = project.getPluginArtifactRepositories();
237                 if ( artifactRepositories == null )
238                 {
239                     artifactRepositories = new ArrayList();
240                 }
241                 try
242                 {
243                     MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact,
244                                                                                           artifactRepositories,
245                                                                                           localRepository );
246                     tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject
247                                             .getVersion(), pluginProject.getUrl() ) );
248                 }
249                 catch ( ProjectBuildingException e )
250                 {
251                     log.info( "Could not build project for: " + artifact.getArtifactId() + ":" + e.getMessage(), e );
252                     tableRow( getPluginRow( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
253                                             null ) );
254                 }
255 
256             }
257             endTable();
258 
259             endSection();
260         }
261 
262         // ----------------------------------------------------------------------
263         // Private methods
264         // ----------------------------------------------------------------------
265 
266         private String[] getPluginTableHeader()
267         {
268             // reused key...
269             String groupId = getReportString( "report.dependencyManagement.column.groupId" );
270             String artifactId = getReportString( "report.dependencyManagement.column.artifactId" );
271             String version = getReportString( "report.dependencyManagement.column.version" );
272             return new String[] { groupId, artifactId, version };
273         }
274 
275         private String[] getPluginRow( String groupId, String artifactId, String version, String link )
276         {
277             artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link );
278             return new String[] { groupId, artifactId, version };
279         }
280 
281         private Comparator getArtifactComparator()
282         {
283             return new Comparator()
284             {
285                 /** {@inheritDoc} */
286                 public int compare( Object o1, Object o2 )
287                 {
288                     Artifact a1 = (Artifact) o1;
289                     Artifact a2 = (Artifact) o2;
290 
291                     int result = a1.getGroupId().compareTo( a2.getGroupId() );
292                     if ( result == 0 )
293                     {
294                         result = a1.getArtifactId().compareTo( a2.getArtifactId() );
295                     }
296                     return result;
297                 }
298             };
299         }
300 
301         private String getReportString( String key )
302         {
303             return i18n.getString( "project-info-report", locale, key );
304         }
305     }
306 }