Skip to content

Commit

Permalink
Fix Issue #99: compileOnly dependencies aren't properly added to clas…
Browse files Browse the repository at this point in the history
…spath

Changed the mechanism used to get the compile classpath to reflect on
the currently compiled source set's own compileClasspath as documented
by the Gradle user guides. This change enables proper handling of
compile classpath in all cases and should eradicate the problem reported.
Integration test to prove this works as expected is added to java-project.

Fix Issue #89: Grails 3 project fails to find Groovyc class

Corrected the GroovyC classpath to reflect the built-in GroovyCompile task
own groovyClasspath. This preserves the ability to use a compile dependency
on groovy as well as default to the local Gradle Groovy instance.
  • Loading branch information
Alex-Vol-SV committed Sep 17, 2017
1 parent 1a792fb commit 2dd37fb
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -39,7 +39,7 @@ example on how to retrieve it from Bintray:
}

dependencies {
classpath 'com.bmuschko:gradle-clover-plugin:2.1.2'
classpath 'com.bmuschko:gradle-clover-plugin:2.1.3'
}
}

Expand Down
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
@@ -1,5 +1,7 @@
### Version 2.1.3 (TBD)

* Fix compileOnly dependencies aren't properly added to classpath - [Issue 99](https://github.com/bmuschko/gradle-clover-plugin/issues/99)
* Fix Grails 3 project fails to find Groovyc class - [Issue 89](https://github.com/bmuschko/gradle-clover-plugin/issues/89)
* Fix Make compilerArgs configurable - [Issue 98](https://github.com/bmuschko/gradle-clover-plugin/issues/98)
* Fix Plugin version 2.1.2 does not work with Gradle 2.x - [Issue 94](https://github.com/bmuschko/gradle-clover-plugin/issues/94)

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -7,7 +7,7 @@ buildscript {
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
if (project.hasProperty('selfCheck')) {
classpath 'com.bmuschko:gradle-clover-plugin:2.1.0'
classpath 'com.bmuschko:gradle-clover-plugin:2.1.3'
}
}
}
Expand All @@ -18,7 +18,7 @@ apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

group = 'com.bmuschko'
version = '2.1.3-SNAPSHOT'
version = '2.1.3'
defaultTasks 'clean', 'build'

sourceSets {
Expand Down
Expand Up @@ -51,12 +51,12 @@ class CloverPluginIntegSpec extends Specification {
cloverXmlReport.exists()
def coverage = new XmlSlurper().parse(cloverXmlReport)
coverage.project.metrics.@classes == '1'
coverage.project.metrics.@methods == '3'
coverage.project.metrics.@coveredmethods == '2'
coverage.project.metrics.@methods == '4'
coverage.project.metrics.@coveredmethods == '3'
coverage.testproject.metrics.@files == '1'
coverage.testproject.metrics.@classes == '1'
coverage.testproject.metrics.@methods == '2'
coverage.testproject.metrics.@coveredmethods == '2'
coverage.testproject.metrics.@methods == '3'
coverage.testproject.metrics.@coveredmethods == '3'
cloverHtmlReport.exists()
cloverJsonReport.exists()
cloverPdfReport.exists()
Expand Down
2 changes: 2 additions & 0 deletions src/integTest/projects/java-project/build.gradle
Expand Up @@ -17,6 +17,8 @@ repositories {
dependencies {
testCompile deps.junit
clover deps.clover
compileOnly group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
testRuntime group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
}

tasks.withType(JavaCompile) {
Expand Down
6 changes: 6 additions & 0 deletions src/integTest/projects/java-project/src/main/java/Book.java
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import org.apache.commons.lang3.JavaVersion;

public class Book {

// tested
Expand All @@ -24,6 +26,10 @@ public boolean open() {
public void throwsRuntimeException() {
throw new RuntimeException("Testing");
}

public boolean isAtLeastJavaVersion() {
return JavaVersion.JAVA_1_8.atLeast(JavaVersion.JAVA_1_7);
}

// untested
public boolean close() {
Expand Down
Expand Up @@ -34,4 +34,8 @@ public void testThrowsRuntimeException() {
new Book().throwsRuntimeException();
}

@Test
public void testIsAtLeastJavaVersion() {
new Book().isAtLeastJavaVersion();
}
}
34 changes: 32 additions & 2 deletions src/main/groovy/com/bmuschko/gradle/clover/CloverPlugin.groovy
Expand Up @@ -28,6 +28,7 @@ import org.gradle.api.tasks.bundling.Jar
import org.gradle.api.tasks.testing.Test

import java.lang.reflect.Constructor
import java.util.concurrent.Callable

/**
* <p>A {@link org.gradle.api.Plugin} that provides a task for creating a code coverage report using Clover.</p>
Expand Down Expand Up @@ -339,25 +340,34 @@ class CloverPlugin implements Plugin<Project> {
private Set<CloverSourceSet> getSourceSets(Project project, CloverPluginConvention cloverPluginConvention) {
def sourceSets = []

Callable<FileCollection> classpathProvider = new Callable<FileCollection>() {
FileCollection call() {
project.sourceSets.main.getCompileClasspath() + project.configurations.getByName(CONFIGURATION_NAME)
}
}

if(hasGroovyPlugin(project)) {
CloverSourceSet cloverSourceSet = new CloverSourceSet()
cloverSourceSet.srcDirs.addAll(filterNonExistentDirectories(project.sourceSets.main.java.srcDirs))
cloverSourceSet.srcDirs.addAll(filterNonExistentDirectories(project.sourceSets.main.groovy.srcDirs))
cloverSourceSet.classesDir = project.sourceSets.main.output.classesDir
cloverSourceSet.backupDir = cloverPluginConvention.classesBackupDir ?: new File("${project.sourceSets.main.output.classesDir}-bak")
cloverSourceSet.classpathProvider = classpathProvider
sourceSets << cloverSourceSet
}
else if(hasJavaPlugin(project)) {
CloverSourceSet cloverSourceSet = new CloverSourceSet()
cloverSourceSet.srcDirs.addAll(filterNonExistentDirectories(project.sourceSets.main.java.srcDirs))
cloverSourceSet.classesDir = project.sourceSets.main.output.classesDir
cloverSourceSet.backupDir = cloverPluginConvention.classesBackupDir ?: new File("${project.sourceSets.main.output.classesDir}-bak")
cloverSourceSet.classpathProvider = classpathProvider
sourceSets << cloverSourceSet
}

if(cloverPluginConvention.additionalSourceSets) {
cloverPluginConvention.additionalSourceSets.each { additionalSourceSet ->
additionalSourceSet.backupDir = cloverPluginConvention.classesBackupDir ?: new File("${additionalSourceSet.classesDir}-bak")
additionalSourceSet.classpathProvider = classpathProvider
sourceSets << additionalSourceSet
}
}
Expand All @@ -368,25 +378,34 @@ class CloverPlugin implements Plugin<Project> {
private Set<CloverSourceSet> getTestSourceSets(Project project, CloverPluginConvention cloverPluginConvention) {
def testSourceSets = []

Callable<FileCollection> classpathProvider = new Callable<FileCollection>() {
FileCollection call() {
project.sourceSets.test.getCompileClasspath() + project.configurations.getByName(CONFIGURATION_NAME)
}
}

if(hasGroovyPlugin(project)) {
CloverSourceSet cloverSourceSet = new CloverSourceSet()
cloverSourceSet.srcDirs.addAll(filterNonExistentDirectories(project.sourceSets.test.java.srcDirs))
cloverSourceSet.srcDirs.addAll(filterNonExistentDirectories(project.sourceSets.test.groovy.srcDirs))
cloverSourceSet.classesDir = project.sourceSets.test.output.classesDir
cloverSourceSet.backupDir = cloverPluginConvention.testClassesBackupDir ?: new File("${project.sourceSets.test.output.classesDir}-bak")
cloverSourceSet.classpathProvider = classpathProvider
testSourceSets << cloverSourceSet
}
else if(hasJavaPlugin(project)) {
CloverSourceSet cloverSourceSet = new CloverSourceSet()
cloverSourceSet.srcDirs.addAll(filterNonExistentDirectories(project.sourceSets.test.java.srcDirs))
cloverSourceSet.classesDir = project.sourceSets.test.output.classesDir
cloverSourceSet.backupDir = cloverPluginConvention.testClassesBackupDir ?: new File("${project.sourceSets.test.output.classesDir}-bak")
cloverSourceSet.classpathProvider = classpathProvider
testSourceSets << cloverSourceSet
}

if(cloverPluginConvention.additionalTestSourceSets) {
cloverPluginConvention.additionalTestSourceSets.each { additionalTestSourceSet ->
additionalTestSourceSet.backupDir = cloverPluginConvention.classesBackupDir ?: new File("${additionalTestSourceSet.classesDir}-bak")
additionalTestSourceSet.classpathProvider = classpathProvider
testSourceSets << additionalTestSourceSet
}
}
Expand Down Expand Up @@ -499,6 +518,17 @@ class CloverPlugin implements Plugin<Project> {
}

private FileCollection getGroovyClasspath(Project project) {
project.configurations.compile.asFileTree
// We use the test sourceSet to derive the GroovyCompile built-in task name
// and from there extract the correct GroovyClasspath. This is more closely
// matched to the built-in Groovy compiler and still supports a build dependency.
def taskName = project.sourceSets.test.getCompileTaskName('groovy')
def task = project.tasks.findByName(taskName)
if (task == null) {
// Fall back to main source set to get this. We should have this
// or the test source set using Groovy if this method is called.
taskName = project.sourceSets.main.getCompileTaskName('groovy')
task = project.tasks.getByName(taskName)
}
task.getGroovyClasspath() + project.configurations.getByName(CONFIGURATION_NAME)
}
}
}
@@ -1,10 +1,19 @@
package com.bmuschko.gradle.clover

import java.util.concurrent.Callable

import org.gradle.api.file.FileCollection

class CloverSourceSet implements Serializable {
static final long serialVersionUID = 7526472295622776147L
def srcDirs = [] as Set<File>
File classesDir
File backupDir
private transient Callable<FileCollection> classpathProvider

FileCollection getCompileClasspath() {
return classpathProvider.call()
}

@Override
String toString() {
Expand Down
Expand Up @@ -224,8 +224,7 @@ class InstrumentCodeAction implements Action<Task> {
}

/**
* Gets Groovyc classpath. Make sure the Groovy version defined in project is used on classpath first to avoid using the
* default version bundled with Gradle.
* Gets Groovyc classpath.
*
* @return Classpath
*/
Expand All @@ -234,12 +233,14 @@ class InstrumentCodeAction implements Action<Task> {
}

/**
* Gets Javac classpath.
* Gets the compile classpath for the given sourceSet.
*
* @param sourceSet the CloverSourceSet to extract the classpath
*
* @return Classpath
*/
private String getJavacClasspath() {
getTestRuntimeClasspath().asPath
private String getCompileClasspath(CloverSourceSet sourceSet) {
sourceSet.getCompileClasspath().asPath
}

/**
Expand All @@ -249,7 +250,7 @@ class InstrumentCodeAction implements Action<Task> {
*/
private void compileGroovyAndJavaSrcFiles(AntBuilder ant) {
for(CloverSourceSet sourceSet : getSourceSets()) {
compileGroovyAndJava(ant, sourceSet.srcDirs, sourceSet.classesDir, getGroovycClasspath())
compileGroovyAndJava(ant, sourceSet.srcDirs, sourceSet.classesDir, getCompileClasspath(sourceSet))
}
}

Expand All @@ -260,7 +261,7 @@ class InstrumentCodeAction implements Action<Task> {
*/
private void compileJavaSrcFiles(AntBuilder ant) {
for(CloverSourceSet sourceSet : getSourceSets()) {
compileJava(ant, sourceSet.srcDirs, sourceSet.classesDir, getJavacClasspath())
compileJava(ant, sourceSet.srcDirs, sourceSet.classesDir, getCompileClasspath(sourceSet))
}
}

Expand All @@ -271,7 +272,7 @@ class InstrumentCodeAction implements Action<Task> {
*/
private void compileGroovyAndJavaTestSrcFiles(AntBuilder ant) {
for(CloverSourceSet sourceSet : getTestSourceSets()) {
String classpath = addClassesDirToClasspath(getGroovycClasspath(), getSourceSets().collect { it.classesDir })
String classpath = addClassesDirToClasspath(getCompileClasspath(sourceSet), getSourceSets().collect { it.classesDir })
compileGroovyAndJava(ant, sourceSet.srcDirs, sourceSet.classesDir, classpath)
}
}
Expand All @@ -283,7 +284,7 @@ class InstrumentCodeAction implements Action<Task> {
*/
private void compileJavaTestSrcFiles(AntBuilder ant) {
for(CloverSourceSet sourceSet : getTestSourceSets()) {
String classpath = addClassesDirToClasspath(getJavacClasspath(), getSourceSets().collect { it.classesDir })
String classpath = addClassesDirToClasspath(getCompileClasspath(sourceSet), getSourceSets().collect { it.classesDir })
compileJava(ant, sourceSet.srcDirs, sourceSet.classesDir, classpath)
}
}
Expand Down

0 comments on commit 2dd37fb

Please sign in to comment.