1
2
3
4
5
6
7
8
9
10
11
12 package org.sonatype.plugins.sisu;
13
14 import java.io.File;
15 import java.net.MalformedURLException;
16 import java.net.URL;
17 import java.net.URLClassLoader;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.apache.maven.artifact.Artifact;
22 import org.apache.maven.plugin.AbstractMojo;
23 import org.apache.maven.project.MavenProject;
24 import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
25 import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
26 import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
27 import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
28 import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
29 import org.apache.maven.shared.artifact.filter.collection.ProjectTransitivityFilter;
30 import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
31 import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
32 import org.codehaus.plexus.util.StringUtils;
33 import org.sonatype.guice.bean.reflect.URLClassSpace;
34 import org.sonatype.guice.bean.scanners.index.SisuIndex;
35
36
37
38
39
40
41
42 public class IndexMojo
43 extends AbstractMojo
44 {
45
46
47
48
49
50
51
52
53 protected File outputDirectory;
54
55
56
57
58
59
60
61 protected boolean includeDependencies;
62
63
64
65
66
67
68
69 protected String excludeGroupIds;
70
71
72
73
74
75
76
77 protected String includeGroupIds;
78
79
80
81
82
83
84
85 protected String excludeArtifactIds;
86
87
88
89
90
91
92
93 protected String includeArtifactIds;
94
95
96
97
98
99
100
101 protected String excludeClassifiers;
102
103
104
105
106
107
108
109 protected String includeClassifiers;
110
111
112
113
114
115
116
117 protected String excludeTypes;
118
119
120
121
122
123
124
125 protected String includeTypes;
126
127
128
129
130
131
132
133 protected String excludeScope;
134
135
136
137
138
139
140
141 protected String includeScope;
142
143
144
145
146
147
148
149 protected boolean excludeTransitive;
150
151
152
153
154
155
156
157
158
159
160
161
162 private MavenProject project;
163
164
165
166
167
168 public void setProject( final MavenProject project )
169 {
170 this.project = project;
171 }
172
173 public void setOutputDirectory( final File outputDirectory )
174 {
175 this.outputDirectory = outputDirectory;
176 }
177
178 public void execute()
179 {
180 new SisuIndex( outputDirectory ).index( new URLClassSpace( getProjectClassLoader(), getIndexPath() ) );
181 }
182
183
184
185
186
187 private ClassLoader getProjectClassLoader()
188 {
189 final List<URL> classPath = new ArrayList<URL>();
190 appendToClassPath( classPath, outputDirectory );
191 appendToClassPath( classPath, new File( project.getBuild().getOutputDirectory() ) );
192 for ( final Object artifact : project.getArtifacts() )
193 {
194 appendToClassPath( classPath, ( (Artifact) artifact ).getFile() );
195 }
196 return URLClassLoader.newInstance( classPath.toArray( new URL[classPath.size()] ) );
197 }
198
199 private URL[] getIndexPath()
200 {
201 final List<URL> indexPath = new ArrayList<URL>();
202 appendToClassPath( indexPath, outputDirectory );
203 if ( includeDependencies )
204 {
205 final FilterArtifacts filter = new FilterArtifacts();
206
207 filter.addFilter( new ProjectTransitivityFilter( project.getDependencyArtifacts(), excludeTransitive ) );
208 filter.addFilter( new ScopeFilter( cleanList( includeScope ), cleanList( excludeScope ) ) );
209 filter.addFilter( new TypeFilter( cleanList( includeTypes ), cleanList( excludeTypes ) ) );
210 filter.addFilter( new ClassifierFilter( cleanList( includeClassifiers ), cleanList( excludeClassifiers ) ) );
211 filter.addFilter( new GroupIdFilter( cleanList( includeGroupIds ), cleanList( excludeGroupIds ) ) );
212 filter.addFilter( new ArtifactIdFilter( cleanList( includeArtifactIds ), cleanList( excludeArtifactIds ) ) );
213
214 try
215 {
216 for ( final Object artifact : filter.filter( project.getArtifacts() ) )
217 {
218 appendToClassPath( indexPath, ( (Artifact) artifact ).getFile() );
219 }
220 }
221 catch ( final ArtifactFilterException e )
222 {
223 getLog().warn( e.getLocalizedMessage() );
224 }
225 }
226 return indexPath.toArray( new URL[indexPath.size()] );
227 }
228
229 private void appendToClassPath( final List<URL> urls, final File file )
230 {
231 if ( null != file )
232 {
233 try
234 {
235 urls.add( file.toURI().toURL() );
236 }
237 catch ( final MalformedURLException e )
238 {
239 getLog().warn( e.getLocalizedMessage() );
240 }
241 }
242 }
243
244 private static String cleanList( final String list )
245 {
246 return StringUtils.isEmpty( list ) ? "" : StringUtils.join( StringUtils.split( list ), "," );
247 }
248 }