Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multiple output source directories #12

Merged
merged 3 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ build
.classpath
.settings
/bin/
.idea/
*.iml
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Currently the versioning policy of this project follows [Semantic Versioning](ht

* Use SpotBugs 3.1.3
* Add `showProgress` option to task mapping.
* Added a minor fix to handle multiple output source directories. With the release of gradle 4.0 the default location of classes changed from "build/classes/main" -> "build/{java, groovy, scala}/main". This caused a bug where the plugin would ignore any classes compiled which were not located within "build/classes/" or for clean groovy/scala projects the plugin would outright fail to perform an analysis due to NO-SOURCE.([#12](https://github.com/spotbugs/spotbugs-gradle-plugin/pull/12))
* Support Gradle 4.8 [#22](https://github.com/spotbugs/spotbugs-gradle-plugin/pull/22)

## 1.6.1 - 2018-02-25
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/com/github/spotbugs/SpotBugsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.stream.StreamSupport;

import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.file.ConfigurableFileTree;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.ConventionMapping;
import org.gradle.api.plugins.quality.CodeQualityExtension;
import org.gradle.api.plugins.quality.internal.AbstractCodeQualityPlugin;
Expand Down Expand Up @@ -182,16 +184,22 @@ protected void configureForSourceSet(final SourceSet sourceSet, SpotBugsTask tas
task.setDescription("Run SpotBugs analysis for " + sourceSet.getName() + " classes");
task.setSource(sourceSet.getAllJava());
ConventionMapping taskMapping = task.getConventionMapping();
taskMapping.map("classes", () -> {
ConfigurableFileTree fileTree = StreamSupport.stream(sourceSet.getOutput().getClassesDirs().spliterator(), false)
.map(project::fileTree)
.reduce((lhs, rhs) -> {
lhs.plus(rhs);
return lhs;
}).orElseThrow(() ->
new InvalidUserDataException("No classes dir configured for source set " + sourceSet.getName())
);
return fileTree.builtBy(sourceSet.getClassesTaskName());
taskMapping.map("classes", (Callable<FileCollection>) () -> {
/*
* As a result of the changes made in gradle 4.0.
* See https://docs.gradle.org/4.0/release-notes.html - Location of classes in the build directory
* Compile no longer bundles all classes in one directory build-gradle/classes/main
* but instead separates classes into build-gradle/classes/{language}/main.
*
* We must therefor retrieve all output directories. Filter away the once that don't exist. Add each
* existing file tree dependency to specified task. And then return the complete fileCollection, contain
* all .class files available for analysis.
*/
FileCollection presentClassDirs = sourceSet.getOutput().getClassesDirs().filter(File::exists);
StreamSupport.stream(presentClassDirs.spliterator(), false)
.map(project::fileTree)
.forEach(tree -> tree.builtBy(sourceSet.getClassesTaskName()));
return presentClassDirs;
});
taskMapping.map("classpath", sourceSet::getCompileClasspath);
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/github/spotbugs/Bar.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.spotbugs

/**
* Simple test class.
*
* @author Kevin Mc Tiernan, 23.05.2018, kemc@skagenfondene.no / kevin.tiernan@knowit.no
*/
class Bar {

private String UNUSED_VARIABLE = "UNUSED"
}
7 changes: 7 additions & 0 deletions src/test/java/com/github/spotbugs/FooTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.spotbugs

/**
* @author Kevin Mc Tiernan, 23.05.2018, kemc@skagenfondene.no / kevin.tiernan@knowit.no
*/
class FooTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package com.github.spotbugs;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;

import org.gradle.internal.impldep.org.apache.commons.io.FileUtils;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* Test's behaviour of the spotbugs plugin when operating with multiple code languages.
* <p>
* Currently tests for groovy, scala - but changes should apply to kotlin as well.
*
* @author Kevin Mc Tiernan, 23.05.2018, kemc@skagenfondene.no / kevin.tiernan@knowit.no
*/
public class MultipleBuildOutputSourceDirsTest {

@Rule
public TemporaryFolder folder= new TemporaryFolder();

@Before
public void cleanUpProjectFolder() {
final File[] directoryContent = folder.getRoot().listFiles();

if (directoryContent != null && directoryContent.length == 0) {
return;
}

for (File currentFile : directoryContent) {
FileUtils.deleteQuietly(currentFile);
}
}

@Test
public void testCombinedScalaJavaSources() throws IOException {
// :: Setup
createProjectScala();
// :: Act
BuildResult result = GradleRunner.create()
.withProjectDir(folder.getRoot())
.withArguments(Arrays.asList("spotbugsMain"))
.withPluginClasspath()
.forwardOutput()
.build();
// :: Verify
assertThat(result.task(":spotbugsMain").getOutcome(), is(TaskOutcome.SUCCESS));
}

@Test
public void testSingleGroovySource() throws IOException {
// :: Setup
createProjectGroovy();
// :: Act
BuildResult result = GradleRunner.create()
.withProjectDir(folder.getRoot())
.withArguments(Arrays.asList("spotbugsMain"))
.withPluginClasspath()
.forwardOutput()
.build();
// :: Verify
assertThat(result.task(":spotbugsMain").getOutcome(), is(TaskOutcome.SUCCESS));
}

@Test
public void testSingleGroovyTestSource() throws IOException {
// :: Setup
createProjectGroovyTest();
// :: Act
BuildResult result = GradleRunner.create()
.withProjectDir(folder.getRoot())
.withArguments(Arrays.asList("spotbugsTest"))
.withPluginClasspath()
.forwardOutput()
.build();
// :: Verify
assertThat(result.task(":spotbugsTest").getOutcome(), is(TaskOutcome.SUCCESS));
}

// =========================== Helper methods =====================================================================

private void createProjectScala() throws IOException {
String buildScript = "plugins {\n"
+ " id 'java'\n"
+ " id 'scala'\n"
+ " id 'com.github.spotbugs' version '1.6.1'\n"
+ "}\n"
+ "version = 1.0\n"
+ "repositories {\n"
+ " mavenCentral()\n"
+ " mavenLocal()\n"
+ "}\n"

+ "apply plugin: 'java'\n"
+ "apply plugin: 'scala'\n"

+ "sourceSets.main.scala.srcDirs = ['src/main/java', 'src/main/scala']\n"
+ "sourceSets.main.java.srcDirs = []\n"

+ "ext.scalaFullVersion = '2.10.3'\n"
+ "dependencies {\n"
+ "compile 'org.scala-lang:scala-library:2.10.7'\n"
+ "}\n";
File buildFile = folder.newFile("build.gradle");
Files.write(buildFile.toPath(), buildScript.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE);

File javaSourceDir = folder.newFolder("src", "main", "java");
File scalaSourceDir = folder.newFolder("src", "main", "scala");
File to = new File(javaSourceDir, "Foo.java");
File from = new File("src/test/java/com/github/spotbugs/Foo.java");
Files.copy(from.toPath(), to.toPath(), StandardCopyOption.COPY_ATTRIBUTES);
}

private void createProjectGroovy() throws IOException {
String buildScript = "plugins {\n" +
" id 'java'\n" +
" id 'groovy'\n" +
" id 'com.github.spotbugs' version '1.6.1'\n" +
"}\n" +
"version = 1.0\n" +
"repositories {\n" +
" mavenCentral()\n" +
" mavenLocal()\n" +
"}\n" +
"dependencies {\n" +
" compile 'org.codehaus.groovy:groovy-all:2.4.14'\n" +
"}\n";
File buildFile = folder.newFile("build.gradle");
Files.write(buildFile.toPath(), buildScript.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE);

File sourceDir = folder.newFolder("src", "main", "groovy");
File to = new File(sourceDir, "Bar.groovy");
File from = new File("src/test/java/com/github/spotbugs/Bar.groovy");
Files.copy(from.toPath(), to.toPath(), StandardCopyOption.COPY_ATTRIBUTES);
}

private void createProjectGroovyTest() throws IOException {
String buildScript = "plugins {\n" +
" id 'java'\n" +
" id 'groovy'\n" +
" id 'com.github.spotbugs' version '1.6.1'\n" +
"}\n" +
"version = 1.0\n" +
"repositories {\n" +
" mavenCentral()\n" +
" mavenLocal()\n" +
"}\n" +
"dependencies {\n" +
" compile 'org.codehaus.groovy:groovy-all:2.4.14'\n" +
"}\n";
File buildFile = folder.newFile("build.gradle");
Files.write(buildFile.toPath(), buildScript.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE);

File sourceDirJava = folder.newFolder("src", "main", "java");
File sourceDirGroovy = folder.newFolder("src", "main", "groovy");
File sourceDirGroovyTest = folder.newFolder("src", "test", "groovy");
File to = new File(sourceDirJava, "Foo.java");
File from = new File("src/test/java/com/github/spotbugs/Foo.java");

Files.copy(from.toPath(), to.toPath(), StandardCopyOption.COPY_ATTRIBUTES);

to = new File(sourceDirGroovyTest, "FooTest.groovy");
from = new File("src/test/java/com/github/spotbugs/FooTest.groovy");

Files.copy(from.toPath(), to.toPath(), StandardCopyOption.COPY_ATTRIBUTES);
}
}