1 package org.apache.maven.plugin.dependency;
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 @Component
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 public void executeReport( Locale locale )
103 throws MavenReportException
104 {
105
106 if ( "pom".equals( project.getPackaging() ) )
107 {
108 getLog().info( "Skipping pom project" );
109 return;
110 }
111
112 if ( outputDirectory == null || !outputDirectory.exists() )
113 {
114 getLog().info( "Skipping project with no Target directory" );
115 return;
116 }
117
118
119 ProjectDependencyAnalysis analysis = null;
120 try
121 {
122 analysis = analyzer.analyze( project );
123
124 if ( usedDependencies != null )
125 {
126 analysis = analysis.forceDeclaredDependenciesUsage( usedDependencies );
127 }
128 }
129 catch ( ProjectDependencyAnalyzerException exception )
130 {
131 throw new MavenReportException( "Cannot analyze dependencies", exception );
132 }
133
134
135 if ( ignoreNonCompile )
136 {
137 analysis = analysis.ignoreNonCompile();
138 }
139
140
141 Sink sink = getSink();
142 ResourceBundle bundle = getBundle( locale );
143
144
145 AnalyzeReportView analyzethis = new AnalyzeReportView();
146 analyzethis.generateReport( analysis, sink, bundle );
147 }
148
149
150
151
152
153
154 public String getOutputName()
155 {
156 return "dependency-analysis";
157 }
158
159
160
161
162 public String getName( Locale locale )
163 {
164 return getBundle( locale ).getString( "analyze.report.name" );
165 }
166
167
168
169
170 public String getDescription( Locale locale )
171 {
172 return getBundle( locale ).getString( "analyze.report.description" );
173 }
174
175
176
177
178
179
180 protected MavenProject getProject()
181 {
182 return project;
183 }
184
185
186
187
188 protected String getOutputDirectory()
189 {
190 getLog().info( outputDirectory.toString() );
191
192 return outputDirectory.toString();
193 }
194
195
196
197
198 protected Renderer getSiteRenderer()
199 {
200 return siteRenderer;
201 }
202
203
204
205
206
207
208 protected ResourceBundle getBundle( Locale locale )
209 {
210 return ResourceBundle.getBundle( "analyze-report", locale, this.getClass().getClassLoader() );
211 }
212 }