From 608450673950f967b8b5c6b7932c9a78d2442d87 Mon Sep 17 00:00:00 2001 From: Kengo TODA Date: Thu, 8 Jun 2017 18:34:27 +0800 Subject: [PATCH] Follow latest version automatically (#195) * upgrade Gradle to use WriteProperties task * use resource to follow the latest release automatically * fix test for classes built by Java7 --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- gradlePlugin/.gitignore | 1 + gradlePlugin/build.gradle | 7 ++++++ .../com/github/spotbugs/SpotBugsPlugin.java | 22 +++++++++++++++++-- .../github/spotbugs/SpotBugsPluginTest.java | 9 ++++++++ .../detect/FindUnsatisfiedObligationTest.java | 2 +- spotbugsTestCases/build.gradle | 3 +++ .../src/java/{ => lambdas}/Issue60.java | 2 ++ 9 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 gradlePlugin/.gitignore rename spotbugsTestCases/src/java/{ => lambdas}/Issue60.java (96%) diff --git a/build.gradle b/build.gradle index 6b4ba6b37e8..5d1a3a08a28 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,6 @@ subprojects { } wrapper { - gradleVersion = '3.2.1' + gradleVersion = '3.5' distributionType = Wrapper.DistributionType.ALL } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7a28f365a3e..e95cd755ba0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip diff --git a/gradlePlugin/.gitignore b/gradlePlugin/.gitignore new file mode 100644 index 00000000000..671c88c09a8 --- /dev/null +++ b/gradlePlugin/.gitignore @@ -0,0 +1 @@ +src/main/resources/spotbugs-gradle-plugin.properties diff --git a/gradlePlugin/build.gradle b/gradlePlugin/build.gradle index 6c14c837e1e..4f98c8d22be 100644 --- a/gradlePlugin/build.gradle +++ b/gradlePlugin/build.gradle @@ -38,3 +38,10 @@ pluginBundle { } } } + +task processVersionFile(type: WriteProperties) { + outputFile file('src/main/resources/spotbugs-gradle-plugin.properties') + + property 'spotbugs-version', project(':spotbugs').version +} +tasks.processResources.dependsOn processVersionFile diff --git a/gradlePlugin/src/main/java/com/github/spotbugs/SpotBugsPlugin.java b/gradlePlugin/src/main/java/com/github/spotbugs/SpotBugsPlugin.java index b14314f7f4f..49e4a2347ca 100644 --- a/gradlePlugin/src/main/java/com/github/spotbugs/SpotBugsPlugin.java +++ b/gradlePlugin/src/main/java/com/github/spotbugs/SpotBugsPlugin.java @@ -1,7 +1,12 @@ package com.github.spotbugs; import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.net.URL; import java.util.Collection; +import java.util.Properties; import java.util.concurrent.Callable; import org.gradle.api.Action; @@ -15,6 +20,8 @@ import org.gradle.api.resources.TextResource; import org.gradle.api.tasks.SourceSet; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.io.Resources; import com.google.common.util.concurrent.Callables; /** @@ -33,7 +40,6 @@ */ public class SpotBugsPlugin extends AbstractCodeQualityPlugin { - public static final String DEFAULT_FINDBUGS_VERSION = "3.1.0-RC2"; private FindBugsExtension extension; @Override @@ -61,10 +67,22 @@ private void configureFindBugsConfigurations() { @Override protected CodeQualityExtension createExtension() { extension = project.getExtensions().create("spotbugs", FindBugsExtension.class, project); - extension.setToolVersion(DEFAULT_FINDBUGS_VERSION); + extension.setToolVersion(loadToolVersion()); return extension; } + @VisibleForTesting + String loadToolVersion() { + URL url = Resources.getResource("spotbugs-gradle-plugin.properties"); + try (InputStream input = url.openStream()) { + Properties prop = new Properties(); + prop.load(input); + return prop.getProperty("spotbugs-version"); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + @Override protected void configureTaskDefaults(SpotBugsTask task, String baseName) { task.setPluginClasspath(project.getConfigurations().getAt("findbugsPlugins")); diff --git a/gradlePlugin/src/test/java/com/github/spotbugs/SpotBugsPluginTest.java b/gradlePlugin/src/test/java/com/github/spotbugs/SpotBugsPluginTest.java index 7e75df672b8..4315e6d4b79 100644 --- a/gradlePlugin/src/test/java/com/github/spotbugs/SpotBugsPluginTest.java +++ b/gradlePlugin/src/test/java/com/github/spotbugs/SpotBugsPluginTest.java @@ -1,5 +1,8 @@ package com.github.spotbugs; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; + import java.io.File; import java.util.Arrays; @@ -40,4 +43,10 @@ public void TestFindBugsTasksExist() throws Exception{ assertTrue(result.getOutput().contains("findbugsMain")); assertTrue(result.getOutput().contains("findbugsTest")); } + + @Test + public void testLoadToolVersion() { + SpotBugsPlugin plugin = new SpotBugsPlugin(); + assertThat(plugin.loadToolVersion(), is(notNullValue())); + } } diff --git a/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/FindUnsatisfiedObligationTest.java b/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/FindUnsatisfiedObligationTest.java index 8acb3d64d87..a8571b2a8fa 100644 --- a/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/FindUnsatisfiedObligationTest.java +++ b/spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/FindUnsatisfiedObligationTest.java @@ -22,7 +22,7 @@ public class FindUnsatisfiedObligationTest { */ @Test public void testIssue60() { - BugCollection bugCollection = spotbugs.performAnalysis(Paths.get("../spotbugsTestCases/build/classes/main/Issue60.class")); + BugCollection bugCollection = spotbugs.performAnalysis(Paths.get("../spotbugsTestCases/build/classes/main/lambdas/Issue60.class")); assertThat(bugCollection, is(emptyIterable())); } } diff --git a/spotbugsTestCases/build.gradle b/spotbugsTestCases/build.gradle index 9c36478bc7f..002198f269b 100644 --- a/spotbugsTestCases/build.gradle +++ b/spotbugsTestCases/build.gradle @@ -32,9 +32,12 @@ tasks.withType(JavaCompile).all { task classesJava7(type:JavaCompile) { exclude '**/*jdk8.java' + exclude '**/lambdas/**' sourceCompatibility = '1.7' targetCompatibility = '1.7' destinationDir = file("$buildDir/classes-jdk7") + classpath = sourceSets.main.compileClasspath + source = sourceSets.main.java } tasks['build'].dependsOn classesJava7 diff --git a/spotbugsTestCases/src/java/Issue60.java b/spotbugsTestCases/src/java/lambdas/Issue60.java similarity index 96% rename from spotbugsTestCases/src/java/Issue60.java rename to spotbugsTestCases/src/java/lambdas/Issue60.java index b79f1b8f447..0033b1f8c7f 100644 --- a/spotbugsTestCases/src/java/Issue60.java +++ b/spotbugsTestCases/src/java/lambdas/Issue60.java @@ -1,3 +1,5 @@ +package lambdas; + import java.io.IOException; import java.io.InputStream; import java.net.URL;