1 | |
package org.apache.maven.plugin.reactor; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Arrays; |
25 | |
import java.util.HashSet; |
26 | |
import java.util.List; |
27 | |
import java.util.Set; |
28 | |
|
29 | |
import org.apache.maven.artifact.ArtifactUtils; |
30 | |
import org.apache.maven.plugin.AbstractMojo; |
31 | |
import org.apache.maven.plugin.MojoExecutionException; |
32 | |
import org.apache.maven.plugin.MojoFailureException; |
33 | |
import org.apache.maven.project.MavenProject; |
34 | |
import org.apache.maven.shared.invoker.Invoker; |
35 | |
import org.codehaus.plexus.util.StringUtils; |
36 | |
import org.codehaus.plexus.util.dag.DAG; |
37 | |
import org.codehaus.plexus.util.dag.Vertex; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 4 | public class MakeMojo |
48 | |
extends AbstractMojo |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
File baseDir; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
List collectedProjects; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
String defaultGroup; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
String artifactList; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
String folderList; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
String goals; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
Invoker invoker; |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | 4 | private boolean printOnly = false; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
SimpleInvoker simpleInvoker; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
String continueFromProject; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
File continueFromFolder; |
118 | |
|
119 | |
public void execute() |
120 | |
throws MojoExecutionException, MojoFailureException |
121 | |
{ |
122 | 4 | if ( artifactList == null && folderList == null ) { |
123 | 0 | throw new MojoFailureException("You must specify either folders or projects with -Dmake.folders=foo,baz/bar or -Dmake.projects=com.mycompany:foo,com.mycompany:bar"); |
124 | |
} |
125 | |
String[] reactorIncludes; |
126 | |
List sortedProjects; |
127 | |
try |
128 | |
{ |
129 | 4 | if (collectedProjects.size() == 0) { |
130 | 0 | throw new NonReactorException(); |
131 | |
} |
132 | 4 | SuperProjectSorter ps = new SuperProjectSorter( collectedProjects ); |
133 | 4 | DAG dag = ps.getDAG(); |
134 | |
|
135 | |
|
136 | 4 | collectArtifactListFromFolderList( collectedProjects ); |
137 | 4 | String[] artifacts = StringUtils.split( artifactList, "," ); |
138 | 4 | Set visited = new HashSet(); |
139 | 4 | Set out = new HashSet(); |
140 | 8 | for (int i = 0; i < artifacts.length; i++) { |
141 | 4 | String project = artifacts[i]; |
142 | 4 | if ( project.indexOf(':') == -1 ) { |
143 | 0 | project = defaultGroup + ":" + project; |
144 | |
} |
145 | 4 | Vertex projectVertex = dag.getVertex( project ); |
146 | 4 | if ( projectVertex == null ) throw new MissingProjectException(project); |
147 | 4 | gatherProjects( projectVertex, ps, visited, out ); |
148 | |
} |
149 | |
|
150 | |
|
151 | 4 | ps = new SuperProjectSorter( new ArrayList( out ) ); |
152 | 4 | sortedProjects = ps.getSortedProjects(); |
153 | |
|
154 | |
|
155 | 4 | reactorIncludes = new String[sortedProjects.size()]; |
156 | 13 | for ( int i = 0; i < sortedProjects.size(); i++ ) |
157 | |
{ |
158 | 9 | MavenProject mp = (MavenProject) sortedProjects.get( i ); |
159 | 9 | String path = RelativePather.getRelativePath( baseDir, mp.getFile() ); |
160 | 9 | reactorIncludes[i] = path; |
161 | |
} |
162 | |
} |
163 | 0 | catch (MojoFailureException e) { |
164 | 0 | throw e; |
165 | |
} |
166 | 0 | catch ( Exception e ) |
167 | |
{ |
168 | 0 | throw new MojoExecutionException( "Problem generating dependency tree", e ); |
169 | 4 | } |
170 | |
|
171 | 4 | if (continueFromFolder != null || continueFromProject != null) { |
172 | 1 | ResumeMojo resumer = new ResumeMojo(); |
173 | 1 | resumer.baseDir = baseDir; |
174 | 1 | resumer.collectedProjects = sortedProjects; |
175 | 1 | resumer.continueFromFolder = continueFromFolder; |
176 | 1 | resumer.continueFromProject = continueFromProject; |
177 | 1 | resumer.goals = goals; |
178 | 1 | resumer.invoker = invoker; |
179 | 1 | resumer.simpleInvoker = simpleInvoker; |
180 | 1 | resumer.printOnly = printOnly; |
181 | 1 | resumer.continueFromGroup = defaultGroup; |
182 | 1 | resumer.execute(); |
183 | 1 | } else { |
184 | 3 | simpleInvoker.runReactor( reactorIncludes, Arrays.asList( goals.split( "," ) ), invoker, printOnly, getLog() ); |
185 | |
} |
186 | |
|
187 | 4 | } |
188 | |
|
189 | |
void collectArtifactListFromFolderList(List collectedProjects) throws MojoFailureException |
190 | |
{ |
191 | 4 | if ( folderList == null ) |
192 | 3 | return; |
193 | 1 | String[] folders = StringUtils.split( folderList, "," ); |
194 | 1 | Set pathSet = new HashSet(); |
195 | 2 | for ( int i = 0; i < folders.length; i++ ) |
196 | |
{ |
197 | 1 | File file = new File( baseDir, folders[i] ); |
198 | 1 | if ( !file.exists() ) |
199 | |
{ |
200 | 0 | throw new MojoFailureException("Folder doesn't exist: " + file.getAbsolutePath() ); |
201 | |
} |
202 | 1 | String path = file.getAbsolutePath(); |
203 | 1 | pathSet.add( path ); |
204 | |
} |
205 | 1 | if (artifactList == null) artifactList = ""; |
206 | 1 | StringBuffer artifactBuffer = new StringBuffer(artifactList); |
207 | 4 | for ( int i = 0; i < collectedProjects.size(); i++ ) |
208 | |
{ |
209 | 3 | MavenProject mp = (MavenProject) collectedProjects.get( i ); |
210 | 3 | if ( pathSet.contains( mp.getFile().getParentFile().getAbsolutePath() ) ) |
211 | |
{ |
212 | 1 | if ( artifactBuffer.length() > 0 ) |
213 | |
{ |
214 | 0 | artifactBuffer.append( ',' ); |
215 | |
} |
216 | 1 | String id = ArtifactUtils.versionlessKey( mp.getGroupId(), mp.getArtifactId() ); |
217 | 1 | artifactBuffer.append( id ); |
218 | |
} |
219 | |
} |
220 | 1 | if ( artifactBuffer.length() == 0 ) |
221 | |
{ |
222 | 0 | throw new MojoFailureException("No folders matched: " + folderList); |
223 | |
} |
224 | 1 | artifactList = artifactBuffer.toString(); |
225 | 1 | } |
226 | |
|
227 | |
protected Set gatherProjects( Vertex v, SuperProjectSorter ps, Set visited, Set out ) |
228 | |
{ |
229 | 5 | visited.add( v ); |
230 | 5 | out.add( ps.getProjectMap().get( v.getLabel() ) ); |
231 | 5 | List children = v.getChildren(); |
232 | 8 | for ( int i = 0; i < children.size(); i++ ) |
233 | |
{ |
234 | 3 | Vertex child = (Vertex) children.get( i ); |
235 | 3 | if ( visited.contains( child ) ) |
236 | 0 | continue; |
237 | 3 | gatherProjects( child, ps, visited, out ); |
238 | |
} |
239 | 5 | return out; |
240 | |
} |
241 | |
} |