1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.maven;
22
23 import java.io.File;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Vector;
27
28 import net.sf.jour.PreProcessor;
29
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugin.logging.Log;
35 import org.apache.maven.project.MavenProject;
36
37 import com.pyx4j.log4j.MavenLogAppender;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class InstrumentationMojo extends AbstractMojo {
53
54 private Log log;
55
56
57
58
59
60
61 private String jourConfig;
62
63
64
65
66
67
68
69 private File classesDirectory;
70
71
72
73
74
75
76
77 protected File outputDirectory;
78
79
80
81
82
83
84
85 protected String output;
86
87
88
89
90
91
92 private boolean copyClasses;
93
94
95
96
97
98
99 private boolean copyResources;
100
101
102
103
104
105
106
107
108
109 private boolean useSystemClassPath;
110
111
112
113
114
115
116
117
118
119
120 protected MavenProject mavenProject;
121
122 public void execute() throws MojoExecutionException, MojoFailureException {
123 log = getLog();
124 MavenLogAppender.startPluginLog(this);
125 try {
126
127 log.info("jourConfig: " + jourConfig);
128
129 File out = new File(outputDirectory, output);
130
131 List classpath = new Vector();
132
133 List dependancy = this.mavenProject.getTestArtifacts();
134 for (Iterator i = dependancy.iterator(); i.hasNext();) {
135 Artifact artifact = (Artifact) i.next();
136 File file = getClasspathElement(artifact, mavenProject);
137 log.debug("dependancy:" + file.toString());
138 classpath.add(file.toString());
139 }
140
141 PreProcessor pp = new PreProcessor(jourConfig, classesDirectory, out, classpath);
142
143 pp.setUseSystemClassPath(useSystemClassPath);
144 pp.setCopyClasses(copyClasses);
145 pp.setCopyResources(copyResources);
146
147 try {
148 pp.process();
149 } catch (Exception e) {
150 log.error("PreProcessing error", e);
151 throw new MojoExecutionException("PreProcessing error", e);
152 }
153
154 } finally {
155 MavenLogAppender.endPluginLog(this);
156 }
157 }
158
159 public static File getClasspathElement(Artifact artifact, MavenProject mavenProject) throws MojoExecutionException {
160 String refId = artifact.getGroupId() + ":" + artifact.getArtifactId();
161 MavenProject project = (MavenProject) mavenProject.getProjectReferences().get(refId);
162 if (project != null) {
163 return new File(project.getBuild().getOutputDirectory());
164 } else {
165 File file = artifact.getFile();
166 if ((file == null) || (!file.exists())) {
167 throw new MojoExecutionException("Dependency Resolution Required " + artifact);
168 }
169 return file;
170 }
171 }
172 }