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.artifact.Artifact;
23 import org.apache.maven.doxia.sink.Sink;
24 import org.apache.maven.doxia.siterenderer.Renderer;
25 import org.apache.maven.plugins.annotations.Component;
26 import org.apache.maven.plugins.annotations.Execute;
27 import org.apache.maven.plugins.annotations.LifecyclePhase;
28 import org.apache.maven.plugins.annotations.Mojo;
29 import org.apache.maven.plugins.annotations.Parameter;
30 import org.apache.maven.plugins.annotations.ResolutionScope;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.reporting.AbstractMavenReport;
33 import org.apache.maven.reporting.MavenReportException;
34 import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
35 import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzer;
36 import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzerException;
37
38 import java.io.File;
39 import java.util.HashSet;
40 import java.util.Iterator;
41 import java.util.Locale;
42 import java.util.ResourceBundle;
43 import java.util.Set;
44
45
46
47
48
49
50
51
52 @Mojo( name = "analyze-report", requiresDependencyResolution = ResolutionScope.TEST )
53 @Execute( phase = LifecyclePhase.TEST_COMPILE )
54 public class AnalyzeReportMojo
55 extends AbstractMavenReport
56 {
57
58
59
60
61
62 @Parameter
63 private MavenProject project;
64
65
66
67
68 @Component
69 private ProjectDependencyAnalyzer analyzer;
70
71
72
73
74 @Component
75 private Renderer siteRenderer;
76
77
78
79
80
81
82 @Parameter( defaultValue = "${project.build.directory}", readonly = true )
83 private File outputDirectory;
84
85
86
87
88 @Parameter( property = "ignoreNonCompile", defaultValue = "false" )
89 private boolean ignoreNonCompile;
90
91
92
93
94
95
96 public void executeReport( Locale locale )
97 throws MavenReportException
98 {
99
100 if ( "pom".equals( project.getPackaging() ) )
101 {
102 getLog().info( "Skipping pom project" );
103 return;
104 }
105
106 if ( outputDirectory == null || !outputDirectory.exists() )
107 {
108 getLog().info( "Skipping project with no Target directory" );
109 return;
110 }
111
112
113 ProjectDependencyAnalysis analysis = null;
114 try
115 {
116 analysis = analyzer.analyze( project );
117 }
118 catch ( ProjectDependencyAnalyzerException exception )
119 {
120 throw new MavenReportException( "Cannot analyze dependencies", exception );
121 }
122
123
124 if ( ignoreNonCompile )
125 {
126 @SuppressWarnings( "unchecked" ) Set<Artifact> filteredUnusedDeclared =
127 new HashSet<Artifact>( analysis.getUnusedDeclaredArtifacts() );
128 Iterator<Artifact> iter = filteredUnusedDeclared.iterator();
129 while ( iter.hasNext() )
130 {
131 Artifact artifact = iter.next();
132 if ( !artifact.getScope().equals( Artifact.SCOPE_COMPILE ) )
133 {
134 iter.remove();
135 }
136 }
137
138 ProjectDependencyAnalysis analysisTemp =
139 new ProjectDependencyAnalysis( analysis.getUsedDeclaredArtifacts(),
140 analysis.getUsedUndeclaredArtifacts(), filteredUnusedDeclared );
141 analysis = analysisTemp;
142 }
143
144
145 Sink sink = getSink();
146 ResourceBundle bundle = getBundle( locale );
147
148
149 AnalyzeReportView analyzethis = new AnalyzeReportView();
150 analyzethis.generateReport( analysis, sink, bundle );
151 }
152
153
154
155
156
157
158 public String getOutputName()
159 {
160 return "dependency-analysis";
161 }
162
163
164
165
166 public String getName( Locale locale )
167 {
168 return getBundle( locale ).getString( "analyze.report.name" );
169 }
170
171
172
173
174 public String getDescription( Locale locale )
175 {
176 return getBundle( locale ).getString( "analyze.report.description" );
177 }
178
179
180
181
182
183
184 protected MavenProject getProject()
185 {
186 return project;
187 }
188
189
190
191
192 protected String getOutputDirectory()
193 {
194 getLog().info( outputDirectory.toString() );
195
196 return outputDirectory.toString();
197 }
198
199
200
201
202 protected Renderer getSiteRenderer()
203 {
204 return siteRenderer;
205 }
206
207
208
209
210
211
212 protected ResourceBundle getBundle( Locale locale )
213 {
214 return ResourceBundle.getBundle( "analyze-report", locale, this.getClass().getClassLoader() );
215 }
216 }