1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.codehaus.mojo.cobertura; |
21 | |
|
22 | |
import org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.artifact.ArtifactUtils; |
24 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
25 | |
import org.apache.maven.artifact.handler.ArtifactHandler; |
26 | |
import org.apache.maven.plugin.MojoExecutionException; |
27 | |
import org.apache.maven.project.MavenProjectHelper; |
28 | |
import org.codehaus.mojo.cobertura.configuration.ConfigInstrumentation; |
29 | |
import org.codehaus.mojo.cobertura.tasks.InstrumentTask; |
30 | |
import org.codehaus.plexus.util.FileUtils; |
31 | |
import org.codehaus.plexus.util.IOUtil; |
32 | |
|
33 | |
import java.io.File; |
34 | |
import java.io.FileOutputStream; |
35 | |
import java.io.IOException; |
36 | |
import java.util.ArrayList; |
37 | |
import java.util.LinkedHashSet; |
38 | |
import java.util.List; |
39 | |
import java.util.Map; |
40 | |
import java.util.Properties; |
41 | |
import java.util.Set; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | public class CoberturaInstrumentMojo |
51 | |
extends AbstractCoberturaMojo |
52 | |
{ |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private ArtifactFactory factory; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private boolean attach; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private String classifier; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
private MavenProjectHelper projectHelper; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private ConfigInstrumentation instrumentation; |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public void execute() |
94 | |
throws MojoExecutionException |
95 | |
{ |
96 | 0 | if ( skipMojo() ) |
97 | |
{ |
98 | 0 | return; |
99 | |
} |
100 | |
|
101 | 0 | ArtifactHandler artifactHandler = getProject().getArtifact().getArtifactHandler(); |
102 | 0 | if ( !"java".equals( artifactHandler.getLanguage() ) ) |
103 | |
{ |
104 | 0 | getLog().info( |
105 | |
"Not executing cobertura:instrument as the project is not a Java classpath-capable package" ); |
106 | |
} |
107 | |
else |
108 | |
{ |
109 | 0 | File instrumentedDirectory = |
110 | |
new File( getProject().getBuild().getDirectory(), "generated-classes/cobertura" ); |
111 | |
|
112 | 0 | if ( !instrumentedDirectory.exists() ) |
113 | |
{ |
114 | 0 | instrumentedDirectory.mkdirs(); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | 0 | if ( instrumentation == null ) |
120 | |
{ |
121 | 0 | instrumentation = new ConfigInstrumentation(); |
122 | |
} |
123 | |
|
124 | |
|
125 | 0 | if ( instrumentation.getIncludes().isEmpty() ) |
126 | |
{ |
127 | 0 | instrumentation.addInclude( "**/*.class" ); |
128 | |
} |
129 | |
|
130 | 0 | File outputDirectory = new File( getProject().getBuild().getOutputDirectory() ); |
131 | 0 | if ( !outputDirectory.exists() ) |
132 | |
{ |
133 | 0 | outputDirectory.mkdirs(); |
134 | |
} |
135 | |
|
136 | |
|
137 | |
try |
138 | |
{ |
139 | 0 | FileUtils.copyDirectoryStructure( outputDirectory, instrumentedDirectory ); |
140 | |
} |
141 | 0 | catch ( IOException e ) |
142 | |
{ |
143 | 0 | throw new MojoExecutionException( "Unable to prepare instrumentation directory.", e ); |
144 | 0 | } |
145 | |
|
146 | 0 | instrumentation.setBasedir( instrumentedDirectory ); |
147 | |
|
148 | |
|
149 | 0 | if ( !getDataFile().getParentFile().exists() ) |
150 | |
{ |
151 | 0 | getDataFile().getParentFile().mkdirs(); |
152 | |
} |
153 | |
|
154 | |
|
155 | 0 | InstrumentTask task = new InstrumentTask(); |
156 | 0 | setTaskDefaults( task ); |
157 | 0 | List<Artifact> classpath = new ArrayList<Artifact>(); |
158 | |
|
159 | 0 | classpath.addAll( pluginClasspathList ); |
160 | 0 | classpath.addAll( getProject().getArtifacts() ); |
161 | 0 | task.setPluginClasspathList( classpath ); |
162 | 0 | task.setConfig( instrumentation ); |
163 | 0 | task.setDestinationDir( instrumentedDirectory ); |
164 | 0 | task.setDataFile( getDataFile() ); |
165 | |
|
166 | 0 | task.execute(); |
167 | |
|
168 | 0 | addCoberturaDependenciesToTestClasspath(); |
169 | |
|
170 | |
|
171 | 0 | System.setProperty( "net.sourceforge.cobertura.datafile", getDataFile().getPath() ); |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | 0 | Properties props = new Properties(); |
179 | 0 | props.setProperty( "net.sourceforge.cobertura.datafile", getDataFile().getPath() ); |
180 | |
|
181 | 0 | File coberturaPropertiesFile = new File( instrumentedDirectory, "cobertura.properties" ); |
182 | 0 | FileOutputStream fos = null; |
183 | |
try |
184 | |
{ |
185 | 0 | fos = new FileOutputStream( coberturaPropertiesFile ); |
186 | 0 | props.store( fos, "Generated by maven-cobertura-plugin for project " + getProject().getId() ); |
187 | |
} |
188 | 0 | catch ( IOException e ) |
189 | |
{ |
190 | 0 | throw new MojoExecutionException( "Unable to write cobertura.properties file.", e ); |
191 | |
} |
192 | |
finally |
193 | |
{ |
194 | 0 | IOUtil.close( fos ); |
195 | 0 | } |
196 | |
|
197 | |
|
198 | 0 | getProject().getBuild().setOutputDirectory( instrumentedDirectory.getPath() ); |
199 | 0 | System.setProperty( "project.build.outputDirectory", instrumentedDirectory.getPath() ); |
200 | 0 | attachCoberturaArtifactIfAppropriate(); |
201 | |
} |
202 | 0 | } |
203 | |
|
204 | |
private void attachCoberturaArtifactIfAppropriate() |
205 | |
{ |
206 | 0 | if ( attach ) |
207 | |
{ |
208 | 0 | if ( getDataFile().exists() ) |
209 | |
{ |
210 | 0 | projectHelper.attachArtifact( getProject(), "ser", classifier, getDataFile() ); |
211 | |
} |
212 | |
else |
213 | |
{ |
214 | 0 | getLog().info( "No cobertura ser file exists to include in the attached artifacts list." ); |
215 | |
} |
216 | |
} |
217 | |
else |
218 | |
{ |
219 | 0 | getLog().info( "NOT adding cobertura ser file to attached artifacts list." ); |
220 | |
} |
221 | 0 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
private void addCoberturaDependenciesToTestClasspath() |
229 | |
throws MojoExecutionException |
230 | |
{ |
231 | 0 | Map<String, Artifact> pluginArtifactMap = ArtifactUtils.artifactMapByVersionlessId( pluginClasspathList ); |
232 | 0 | Artifact coberturaArtifact = pluginArtifactMap.get( "net.sourceforge.cobertura:cobertura-runtime" ); |
233 | |
|
234 | 0 | if ( coberturaArtifact == null ) |
235 | |
{ |
236 | 0 | getLog().error( "pluginArtifactMap: " + pluginArtifactMap ); |
237 | |
|
238 | 0 | throw new MojoExecutionException( "Couldn't find 'cobertura' artifact in plugin dependencies" ); |
239 | |
} |
240 | |
|
241 | 0 | coberturaArtifact = artifactScopeToProvided( coberturaArtifact ); |
242 | |
|
243 | 0 | if ( this.getProject().getDependencyArtifacts() != null ) |
244 | |
{ |
245 | 0 | Set<Artifact> set = new LinkedHashSet<Artifact>( this.getProject().getDependencyArtifacts() ); |
246 | 0 | set.add( coberturaArtifact ); |
247 | 0 | this.getProject().setDependencyArtifacts( set ); |
248 | |
} |
249 | 0 | } |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
private Artifact artifactScopeToProvided( Artifact artifact ) |
258 | |
{ |
259 | 0 | return factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), |
260 | |
Artifact.SCOPE_PROVIDED, artifact.getType() ); |
261 | |
} |
262 | |
} |