1 package org.apache.maven.plugin.dependency.analyze;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.doxia.sink.Sink;
23 import org.apache.maven.doxia.siterenderer.Renderer;
24 import org.apache.maven.plugins.annotations.Component;
25 import org.apache.maven.plugins.annotations.Execute;
26 import org.apache.maven.plugins.annotations.LifecyclePhase;
27 import org.apache.maven.plugins.annotations.Mojo;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.apache.maven.plugins.annotations.ResolutionScope;
30 import org.apache.maven.project.MavenProject;
31 import org.apache.maven.reporting.AbstractMavenReport;
32 import org.apache.maven.reporting.MavenReportException;
33 import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
34 import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzer;
35 import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzerException;
36
37 import java.io.File;
38 import java.util.Locale;
39 import java.util.ResourceBundle;
40
41
42
43
44
45
46
47
48 @Mojo( name = "analyze-report", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
49 @Execute( phase = LifecyclePhase.TEST_COMPILE )
50 public class AnalyzeReportMojo
51 extends AbstractMavenReport
52 {
53
54
55
56
57
58 @Parameter( defaultValue = "${project}", readonly = true, required = true )
59 private MavenProject project;
60
61
62
63
64 @Component
65 private ProjectDependencyAnalyzer analyzer;
66
67
68
69
70 @Component
71 private Renderer siteRenderer;
72
73
74
75
76
77
78 @Parameter( defaultValue = "${project.build.directory}", readonly = true )
79 private File outputDirectory;
80
81
82
83
84
85 @Parameter( property = "ignoreNonCompile", defaultValue = "false" )
86 private boolean ignoreNonCompile;
87
88
89
90
91
92
93
94 @Parameter
95 private String[] usedDependencies;
96
97
98
99
100
101
102 @Parameter( property = "mdep.analyze.skip", defaultValue = "false" )
103 private boolean skip;
104
105
106
107
108
109
110 public void executeReport( Locale locale )
111 throws MavenReportException
112 {
113 if ( isSkip() )
114 {
115 getLog().info( "Skipping plugin execution" );
116 return;
117 }
118
119
120 if ( "pom".equals( project.getPackaging() ) )
121 {
122 getLog().info( "Skipping pom project" );
123 return;
124 }
125
126 if ( outputDirectory == null || !outputDirectory.exists() )
127 {
128 getLog().info( "Skipping project with no Target directory" );
129 return;
130 }
131
132
133 ProjectDependencyAnalysis analysis;
134 try
135 {
136 analysis = analyzer.analyze( project );
137
138 if ( usedDependencies != null )
139 {
140 analysis = analysis.forceDeclaredDependenciesUsage( usedDependencies );
141 }
142 }
143 catch ( ProjectDependencyAnalyzerException exception )
144 {
145 throw new MavenReportException( "Cannot analyze dependencies", exception );
146 }
147
148
149 if ( ignoreNonCompile )
150 {
151 analysis = analysis.ignoreNonCompile();
152 }
153
154
155 Sink sink = getSink();
156 ResourceBundle bundle = getBundle( locale );
157
158
159 AnalyzeReportView analyzethis = new AnalyzeReportView();
160 analyzethis.generateReport( analysis, sink, bundle );
161 }
162
163
164
165
166
167
168 public String getOutputName()
169 {
170 return "dependency-analysis";
171 }
172
173
174
175
176 public String getName( Locale locale )
177 {
178 return getBundle( locale ).getString( "analyze.report.name" );
179 }
180
181
182
183
184 public String getDescription( Locale locale )
185 {
186 return getBundle( locale ).getString( "analyze.report.description" );
187 }
188
189
190
191
192
193
194 protected MavenProject getProject()
195 {
196 return project;
197 }
198
199
200
201
202 protected String getOutputDirectory()
203 {
204 getLog().info( outputDirectory.toString() );
205
206 return outputDirectory.toString();
207 }
208
209
210
211
212 protected Renderer getSiteRenderer()
213 {
214 return siteRenderer;
215 }
216
217
218
219
220
221
222 protected ResourceBundle getBundle( Locale locale )
223 {
224 return ResourceBundle.getBundle( "analyze-report", locale, this.getClass().getClassLoader() );
225 }
226
227 public boolean isSkip()
228 {
229 return skip;
230 }
231
232 public void setSkip( boolean skip )
233 {
234 this.skip = skip;
235 }
236 }