1 package org.appfuse;
2
3 import org.apache.maven.artifact.Artifact;
4 import org.apache.maven.artifact.factory.ArtifactFactory;
5 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
6 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
7 import org.apache.maven.model.Dependency;
8 import org.apache.maven.plugin.AbstractMojo;
9 import org.apache.maven.plugin.MojoExecutionException;
10 import org.apache.maven.project.MavenProject;
11 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
12
13 import java.io.File;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Set;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class AddClassesMojo extends AbstractMojo
41 {
42
43
44
45
46
47
48
49
50 private File workDirectory;
51
52
53
54
55
56
57
58 private List reactorProjects;
59
60
61
62
63
64
65
66
67 private MavenProject project;
68
69
70
71
72
73
74
75
76
77 private ArtifactFactory artifactFactory;
78
79
80
81
82
83
84
85 private String warpathIncludes;
86
87
88
89
90
91
92
93
94 private String warpathExcludes;
95
96
97 public void execute()
98 throws MojoExecutionException
99 {
100 File f = workDirectory;
101
102 if (!f.exists())
103 {
104 f.mkdirs();
105 }
106
107
108 Set artifacts = project.getArtifacts();
109
110 List duplicates = findDuplicates(artifacts);
111
112 Set newDependencies = new HashSet();
113
114 for (Iterator iterator = artifacts.iterator(); iterator.hasNext();)
115 {
116 Artifact artifact = (Artifact) iterator.next();
117 ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_COMPILE);
118 if ("warpath".equals(artifact.getType()) && filter.include(artifact) && !artifact.isOptional())
119 {
120
121 String warWorkingDir = getDefaultFinalName(artifact);
122
123 getLog().debug("Processing war dependency " + warWorkingDir);
124
125 if (duplicates.contains(warWorkingDir))
126 {
127 getLog().debug("Duplicate war dependency found:" + warWorkingDir);
128 warWorkingDir = artifact.getGroupId() + "-" + warWorkingDir;
129 getLog().debug("Deplicate war dependency renamed to " + warWorkingDir);
130 }
131
132 File warClassesDirectory = new File(workDirectory, warWorkingDir);
133 try
134 {
135 WarPathUtils.unpackWarClassesIfNewer(artifact.getFile(), warClassesDirectory, warpathIncludes, warpathExcludes);
136 }
137 catch (IOException e)
138 {
139 throw new MojoExecutionException("I/O error while processing WAR dependencies.", e);
140 }
141 getLog().debug("Adding new dependenvy artifact entry for " + warWorkingDir);
142 try
143 {
144 newDependencies.add(getWarClassesDependency(artifact, warClassesDirectory));
145 }
146 catch (OverConstrainedVersionException e)
147 {
148 throw new MojoExecutionException("Failed to created war classes dependency for artifact " + warWorkingDir, e);
149 }
150 }
151 }
152
153 if (newDependencies.size() > 0)
154 {
155
156 addDepenciesToProject(project, newDependencies);
157
158
159
160
161 for (int i = 0; i < reactorProjects.size(); i++)
162 {
163 MavenProject mavenProject = (MavenProject) reactorProjects.get(i);
164 if (mavenProject.getArtifactId().equals(project.getArtifactId()) &&
165 mavenProject.getGroupId().equals(project.getGroupId()) &&
166 mavenProject != project)
167 {
168 getLog().debug("Adding dependencies to reactor project: " + mavenProject.getGroupId() + "-" + mavenProject.getArtifactId());
169 addDepenciesToProject(mavenProject, newDependencies);
170 }
171 }
172 }
173
174 }
175
176 private void addDepenciesToProject(MavenProject project, Set dependencies)
177 throws MojoExecutionException
178 {
179 List amalgamatedDependencies = new ArrayList();
180 amalgamatedDependencies.addAll(dependencies);
181 amalgamatedDependencies.addAll(project.getDependencies());
182 project.setDependencies(amalgamatedDependencies);
183 try
184 {
185 project.setDependencyArtifacts(project.createArtifacts(artifactFactory, null, null));
186 }
187 catch (InvalidDependencyVersionException e)
188 {
189 throw new MojoExecutionException("Failed to resolve dependency artifacts.", e);
190 }
191 }
192
193
194
195
196
197
198
199
200 private Dependency getWarClassesDependency(Artifact artifact, File warClassesDirectory) throws OverConstrainedVersionException
201 {
202 Dependency dependency = new Dependency();
203 dependency.setArtifactId(artifact.getArtifactId());
204 dependency.setGroupId(artifact.getGroupId());
205 dependency.setType("classes");
206 dependency.setScope(Artifact.SCOPE_SYSTEM);
207 dependency.setOptional(true);
208 dependency.setVersion(artifact.getSelectedVersion().toString());
209 dependency.setSystemPath(warClassesDirectory.getPath());
210 return dependency;
211 }
212
213
214
215
216
217
218
219 private List findDuplicates(Set artifacts)
220 {
221 List duplicates = new ArrayList();
222 List identifiers = new ArrayList();
223 for (Iterator iter = artifacts.iterator(); iter.hasNext();)
224 {
225 Artifact artifact = (Artifact) iter.next();
226 String candidate = getDefaultFinalName(artifact);
227 if (identifiers.contains(candidate))
228 {
229 duplicates.add(candidate);
230 }
231 else
232 {
233 identifiers.add(candidate);
234 }
235 }
236 return duplicates;
237 }
238
239
240
241
242
243
244
245 private String getDefaultFinalName(Artifact artifact)
246 {
247 if ("warpath".equals(artifact.getType()))
248 {
249 return artifact.getArtifactId() + "-" + artifact.getVersion() + "." +
250 artifact.getType() + ".jar";
251 }
252 else
253 {
254 return artifact.getArtifactId() + "-" + artifact.getVersion() + "." +
255 artifact.getArtifactHandler().getExtension();
256 }
257 }
258
259 }