1 package org.apache.maven.plugin.dependency;
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.io.File;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.Locale;
26 import java.util.ResourceBundle;
27 import java.util.Set;
28
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.doxia.sink.Sink;
31 import org.apache.maven.doxia.siterenderer.Renderer;
32 import org.apache.maven.project.MavenProject;
33 import org.apache.maven.reporting.AbstractMavenReport;
34 import org.apache.maven.reporting.MavenReportException;
35 import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
36 import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzer;
37 import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzerException;
38
39 /**
40 * Analyzes the dependencies of this project and produces a report that summarizes which are: used and declared; used
41 * and undeclared; unused and declared.
42 *
43 * @version $Id: AnalyzeReportMojo.java 1081021 2011-03-13 00:17:39Z hboutemy $
44 * @since 2.0-alpha-5
45 * @goal analyze-report
46 * @requiresDependencyResolution test
47 * @execute phase="test-compile"
48 */
49 public class AnalyzeReportMojo
50 extends AbstractMavenReport
51 {
52 // fields -----------------------------------------------------------------
53
54 /**
55 * The Maven project to analyze.
56 *
57 * @parameter expression="${project}"
58 * @required
59 * @readonly
60 */
61 private MavenProject project;
62
63 /**
64 * The Maven project dependency analyzer to use.
65 *
66 * @component
67 * @required
68 * @readonly
69 */
70 private ProjectDependencyAnalyzer analyzer;
71
72 /**
73 * @component
74 * @required
75 * @readonly
76 */
77 private Renderer siteRenderer;
78
79 /**
80 * Target folder
81 *
82 * @parameter expression="${project.build.directory}"
83 * @readonly
84 * @since 2.0-alpha-5
85 */
86 private File outputDirectory;
87
88 /**
89 * Ignore Runtime,Provide,Test,System scopes for unused dependency analysis
90 *
91 * @parameter expression="${ignoreNonCompile}" default-value="false"
92 */
93 private boolean ignoreNonCompile;
94
95 // Mojo methods -----------------------------------------------------------
96
97 /*
98 * @see org.apache.maven.plugin.Mojo#execute()
99 */
100 public void executeReport( Locale locale ) throws MavenReportException
101 {
102 // Step 0: Checking pom availability
103 if ( "pom".equals( project.getPackaging() ) )
104 {
105 getLog().info( "Skipping pom project" );
106 return;
107 }
108
109 if ( outputDirectory == null || !outputDirectory.exists() )
110 {
111 getLog().info( "Skipping project with no Target directory" );
112 return;
113 }
114
115 // Step 1: Analyze the project
116 ProjectDependencyAnalysis analysis = null;
117 try
118 {
119 analysis = analyzer.analyze( project );
120 }
121 catch ( ProjectDependencyAnalyzerException exception )
122 {
123 throw new MavenReportException( "Cannot analyze dependencies", exception );
124 }
125
126
127 //remove everything that's not in the compile scope
128 if ( ignoreNonCompile )
129 {
130 Set<Artifact> filteredUnusedDeclared = new HashSet<Artifact>( analysis.getUnusedDeclaredArtifacts() );
131 Iterator<Artifact> iter = filteredUnusedDeclared.iterator();
132 while ( iter.hasNext() )
133 {
134 Artifact artifact = iter.next();
135 if ( !artifact.getScope().equals( Artifact.SCOPE_COMPILE ) )
136 {
137 iter.remove();
138 }
139 }
140
141 ProjectDependencyAnalysis analysisTemp = new ProjectDependencyAnalysis(analysis.getUsedDeclaredArtifacts(),analysis.getUsedUndeclaredArtifacts(),filteredUnusedDeclared);
142 analysis = analysisTemp;
143 }
144
145
146 // Step 2: Create sink and bundle
147 Sink sink = getSink();
148 ResourceBundle bundle = getBundle( locale );
149
150 // Step 3: Generate the report
151 AnalyzeReportView analyzethis = new AnalyzeReportView();
152 analyzethis.generateReport( analysis, sink, bundle );
153 }
154
155 // MavenReport methods ----------------------------------------------------
156
157 /*
158 * @see org.apache.maven.reporting.AbstractMavenReport#getOutputName()
159 */
160 public String getOutputName()
161 {
162 return "dependency-analysis";
163 }
164
165 /*
166 * @see org.apache.maven.reporting.AbstractMavenReport#getName(java.util.Locale)
167 */
168 public String getName( Locale locale )
169 {
170 return getBundle( locale ).getString( "analyze.report.name" );
171 }
172
173 /*
174 * @see org.apache.maven.reporting.AbstractMavenReport#getDescription(java.util.Locale)
175 */
176 public String getDescription( Locale locale )
177 {
178 return getBundle( locale ).getString( "analyze.report.description" );
179 }
180
181 // AbstractMavenReport methods --------------------------------------------
182
183 /*
184 * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
185 */
186 protected MavenProject getProject()
187 {
188 return project;
189 }
190
191 /*
192 * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
193 */
194 protected String getOutputDirectory()
195 {
196 getLog().info( outputDirectory.toString() );
197
198 return outputDirectory.toString();
199 }
200
201 /*
202 * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
203 */
204 protected Renderer getSiteRenderer()
205 {
206 return siteRenderer;
207 }
208
209 // protected methods ------------------------------------------------------
210
211 /**
212 * @param locale the current locale
213 */
214 protected ResourceBundle getBundle( Locale locale )
215 {
216 return ResourceBundle.getBundle( "analyze-report", locale, this.getClass().getClassLoader() );
217 }
218 }