@@ -25,6 +25,7 @@ class ScoveragePlugin implements Plugin<PluginAware> {
25
25
static final String COMPILE_NAME = ' compileScoverageScala'
26
26
static final String AGGREGATE_NAME = ' aggregateScoverage'
27
27
static final String DEFAULT_SCALA_VERSION = ' 2.13.6'
28
+ static final String SCOVERAGE_COMPILE_ONLY_PROPERTY = ' scoverageCompileOnly' ;
28
29
29
30
static final String DEFAULT_REPORT_DIR = ' reports' + File . separatorChar + ' scoverage'
30
31
@@ -97,7 +98,6 @@ class ScoveragePlugin implements Plugin<PluginAware> {
97
98
98
99
def compileTask = project. tasks[instrumentedSourceSet. getCompileTaskName(" scala" )]
99
100
compileTask. mustRunAfter(originalCompileTask)
100
- originalJarTask. mustRunAfter(compileTask)
101
101
102
102
def globalReportTask = project. tasks. register(REPORT_NAME , ScoverageAggregate )
103
103
def globalCheckTask = project. tasks. register(CHECK_NAME )
@@ -154,24 +154,6 @@ class ScoveragePlugin implements Plugin<PluginAware> {
154
154
155
155
configureCheckTask(project, extension, globalCheckTask, globalReportTask)
156
156
157
- // make this project's scoverage compilation depend on scoverage compilation of any other project
158
- // which this project depends on its normal compilation
159
- // (essential when running without normal compilation on multi-module projects with inner dependencies)
160
- def originalCompilationDependencies = recursiveDependenciesOf(compileTask). findAll {
161
- it instanceof ScalaCompile
162
- }
163
- originalCompilationDependencies. each {
164
- def dependencyProjectCompileTask = it. project. tasks. findByName(COMPILE_NAME )
165
- def dependencyProjectReportTask = it. project. tasks. findByName(REPORT_NAME )
166
- if (dependencyProjectCompileTask != null ) {
167
- compileTask. dependsOn(dependencyProjectCompileTask)
168
- // we don't want this project's tests to affect the other project's report
169
- testTasks. each {
170
- it. mustRunAfter(dependencyProjectReportTask)
171
- }
172
- }
173
- }
174
-
175
157
compileTask. configure {
176
158
List<String > parameters = []
177
159
List<String > existingParameters = scalaCompileOptions. additionalParameters
@@ -207,6 +189,79 @@ class ScoveragePlugin implements Plugin<PluginAware> {
207
189
}
208
190
}
209
191
192
+ if (project. hasProperty(SCOVERAGE_COMPILE_ONLY_PROPERTY )) {
193
+ project. logger. info(" Making scoverage compilation the primary compilation task (instead of compileScala)" )
194
+
195
+ originalCompileTask. enabled = false ;
196
+ compileTask. destinationDirectory = originalCompileTask. destinationDirectory
197
+ originalJarTask. mustRunAfter(compileTask)
198
+
199
+ // make this project's scoverage compilation depend on scoverage compilation of any other project
200
+ // which this project depends on its normal compilation
201
+ def originalCompilationDependencies = recursiveDependenciesOf(compileTask). findAll {
202
+ it instanceof ScalaCompile
203
+ }
204
+ originalCompilationDependencies. each {
205
+ def dependencyProjectCompileTask = it. project. tasks. findByName(COMPILE_NAME )
206
+ def dependencyProjectReportTask = it. project. tasks. findByName(REPORT_NAME )
207
+ if (dependencyProjectCompileTask != null ) {
208
+ compileTask. dependsOn(dependencyProjectCompileTask)
209
+ // we don't want this project's tests to affect the other project's report
210
+ testTasks. each {
211
+ it. mustRunAfter(dependencyProjectReportTask)
212
+ }
213
+ }
214
+ }
215
+ } else {
216
+ compileTask. configure {
217
+ doFirst {
218
+ destinationDir. deleteDir()
219
+ }
220
+
221
+ // delete non-instrumented classes by comparing normally compiled classes to those compiled with scoverage
222
+ doLast {
223
+ project. logger. info(" Deleting classes compiled by scoverage but non-instrumented (identical to normal compilation)" )
224
+ def originalCompileTaskName = project. sourceSets. getByName(SourceSet . MAIN_SOURCE_SET_NAME )
225
+ .getCompileTaskName(" scala" )
226
+ def originalDestinationDirectory = project. tasks[originalCompileTaskName]. destinationDirectory
227
+ def originalDestinationDir = originalDestinationDirectory. get(). asFile
228
+ def destinationDir = destinationDirectory. get(). asFile
229
+
230
+
231
+ def findFiles = { File dir , Closure<Boolean > condition = null ->
232
+ def files = []
233
+
234
+ if (dir. exists()) {
235
+ dir. eachFileRecurse(FILES ) { f ->
236
+ if (condition == null || condition(f)) {
237
+ def relativePath = dir. relativePath(f)
238
+ files << relativePath
239
+ }
240
+ }
241
+ }
242
+
243
+ files
244
+ }
245
+
246
+ def isSameFile = { String relativePath ->
247
+ def fileA = new File (originalDestinationDir, relativePath)
248
+ def fileB = new File (destinationDir, relativePath)
249
+ FileUtils . contentEquals(fileA, fileB)
250
+ }
251
+
252
+ def originalClasses = findFiles(originalDestinationDir)
253
+ def identicalInstrumentedClasses = findFiles(destinationDir, { f ->
254
+ def relativePath = destinationDir. relativePath(f)
255
+ originalClasses. contains(relativePath) && isSameFile(relativePath)
256
+ })
257
+
258
+ identicalInstrumentedClasses. each { f ->
259
+ Files . deleteIfExists(destinationDir. toPath(). resolve(f))
260
+ }
261
+ }
262
+ }
263
+ }
264
+
210
265
project. gradle. taskGraph. whenReady { graph ->
211
266
def hasAnyReportTask = reportTasks. any { graph. hasTask(it) }
212
267
@@ -228,59 +283,6 @@ class ScoveragePlugin implements Plugin<PluginAware> {
228
283
}
229
284
}
230
285
}
231
-
232
- compileTask. configure {
233
- if (! graph. hasTask(originalCompileTask)) {
234
- project. logger. info(" Making scoverage compilation the primary compilation task (instead of compileScala)" )
235
- destinationDirectory = originalCompileTask. destinationDirectory
236
- } else {
237
- doFirst {
238
- def destinationDir = destinationDirectory. get(). asFile
239
- destinationDir. deleteDir()
240
- }
241
-
242
- // delete non-instrumented classes by comparing normally compiled classes to those compiled with scoverage
243
- doLast {
244
- project. logger. info(" Deleting classes compiled by scoverage but non-instrumented (identical to normal compilation)" )
245
- def originalCompileTaskName = project. sourceSets. getByName(SourceSet . MAIN_SOURCE_SET_NAME )
246
- .getCompileTaskName(" scala" )
247
- def originalDestinationDirectory = project. tasks[originalCompileTaskName]. destinationDirectory
248
- def originalDestinationDir = originalDestinationDirectory. get(). asFile
249
- def destinationDir = destinationDirectory. get(). asFile
250
-
251
- def findFiles = { File dir , Closure<Boolean > condition = null ->
252
- def files = []
253
-
254
- if (dir. exists()) {
255
- dir. eachFileRecurse(FILES ) { f ->
256
- if (condition == null || condition(f)) {
257
- def relativePath = dir. relativePath(f)
258
- files << relativePath
259
- }
260
- }
261
- }
262
-
263
- files
264
- }
265
-
266
- def isSameFile = { String relativePath ->
267
- def fileA = new File (originalDestinationDir, relativePath)
268
- def fileB = new File (destinationDir, relativePath)
269
- FileUtils . contentEquals(fileA, fileB)
270
- }
271
-
272
- def originalClasses = findFiles(originalDestinationDir)
273
- def identicalInstrumentedClasses = findFiles(destinationDir, { f ->
274
- def relativePath = destinationDir. relativePath(f)
275
- originalClasses. contains(relativePath) && isSameFile(relativePath)
276
- })
277
-
278
- identicalInstrumentedClasses. each { f ->
279
- Files . deleteIfExists(destinationDir. toPath(). resolve(f))
280
- }
281
- }
282
- }
283
- }
284
286
}
285
287
286
288
// define aggregation task
0 commit comments