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.tasks;
21
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.codehaus.mojo.cobertura.configuration.ConfigInstrumentation;
24 import org.codehaus.plexus.util.FileUtils;
25 import org.codehaus.plexus.util.StringUtils;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.Iterator;
30 import java.util.List;
31
32
33
34
35
36
37 public class InstrumentTask
38 extends AbstractTask
39 {
40 private ConfigInstrumentation config = null;
41
42 private File dataFile = null;
43
44 private File destinationDir = null;
45
46
47
48
49 public InstrumentTask()
50 {
51 super( net.sourceforge.cobertura.instrument.InstrumentMain.class.getName() );
52 }
53
54
55
56
57 public void execute()
58 throws MojoExecutionException
59 {
60
61 cmdLineArgs.setUseCommandsFile( true );
62
63 if ( StringUtils.isNotEmpty( config.getMaxmem() ) )
64 {
65 this.setMaxmem( config.getMaxmem() );
66 }
67
68 if ( dataFile != null )
69 {
70 cmdLineArgs.addArg( "--datafile", dataFile.getAbsolutePath() );
71 }
72
73 if ( destinationDir != null )
74 {
75 cmdLineArgs.addArg( "--destination", destinationDir.getAbsolutePath() );
76 }
77
78 if ( config.getIgnoreTrivial() )
79 {
80 cmdLineArgs.addArg( "--ignoreTrivial" );
81 }
82
83 for ( String ignoreMethodAnnotation : config.getIgnoreMethodAnnotations() )
84 {
85 cmdLineArgs.addArg( "--ignoreMethodAnnotation", ignoreMethodAnnotation );
86 }
87
88 for ( String ignore : config.getIgnores() )
89 {
90 cmdLineArgs.addArg( "--ignore", ignore );
91 }
92
93 String includes = joinCludes( config.getIncludes() );
94 String excludes = joinCludes( config.getExcludes() );
95 @SuppressWarnings( "unchecked" ) String defaultExcludes = joinCludes( FileUtils.getDefaultExcludesAsList() );
96
97 if ( StringUtils.isNotEmpty( excludes ) )
98 {
99 excludes += "," + defaultExcludes;
100 }
101 else
102 {
103 excludes = defaultExcludes;
104 }
105
106 try
107 {
108 if ( getLog().isDebugEnabled() )
109 {
110 getLog().debug( "Config : " + config );
111 getLog().debug( "Basedir: " + config.getBasedir() );
112 getLog().debug( "Include: " + includes );
113 getLog().debug( "Exclude: " + excludes );
114 getLog().debug( "Max Mem: " + config.getMaxmem() );
115 }
116
117 @SuppressWarnings( "unchecked" ) List<String> filenames =
118 FileUtils.getFileNames( config.getBasedir(), includes, excludes, false );
119
120 if ( filenames.isEmpty() )
121 {
122 getLog().warn( "No files to instrument." );
123 return;
124 }
125
126 cmdLineArgs.addArg( "--basedir", config.getBasedir().getAbsolutePath() );
127 for ( String filename : filenames )
128 {
129 if ( getLog().isDebugEnabled() )
130 {
131 getLog().debug( "To Instrument: " + filename );
132 }
133 cmdLineArgs.addArg( filename );
134 }
135 }
136 catch ( IOException e )
137 {
138 throw new MojoExecutionException( "Unable to obtain file list from includes/excludes.", e );
139 }
140
141 int returnCode = executeJava();
142
143
144 if ( returnCode == 0 )
145 {
146 if ( !isQuiet() )
147 {
148 getLog().info( "Instrumentation was successful." );
149 }
150 }
151 else
152 {
153 throw new MojoExecutionException( "Unable to instrument project." );
154 }
155
156 }
157
158
159
160
161 public ConfigInstrumentation getConfig()
162 {
163 return config;
164 }
165
166
167
168
169 public File getDataFile()
170 {
171 return dataFile;
172 }
173
174
175
176
177 public File getDestinationDir()
178 {
179 return destinationDir;
180 }
181
182
183
184
185
186
187
188 public String joinCludes( List<String> cludes )
189 {
190 StringBuffer sb = new StringBuffer();
191 Iterator<String> it = cludes.iterator();
192 while ( it.hasNext() )
193 {
194 String clude = it.next();
195 sb.append( clude );
196 if ( it.hasNext() )
197 {
198 sb.append( "," );
199 }
200 }
201 return sb.toString();
202 }
203
204
205
206
207
208
209 public void setConfig( ConfigInstrumentation config )
210 {
211 this.config = config;
212 }
213
214
215
216
217
218
219 public void setDataFile( File dataFile )
220 {
221 this.dataFile = dataFile;
222 }
223
224
225
226
227
228
229 public void setDestinationDir( File destinationDir )
230 {
231 this.destinationDir = destinationDir;
232 }
233
234 @Override
235 public String createClasspath()
236 throws MojoExecutionException
237 {
238 return this.config.getBasedir().getAbsolutePath() + super.createClasspath();
239 }
240
241
242 }