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

Add FindBugs failOnError setting #16

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ findbugsIncludeFilters := {
* *Accepts:* `true` and `false`
* *Default:* `false`

### `findbugsFailOnError`
* *Description:* Whether the build should be failed if there are any reported bug instances.
* *Accepts:* `true` and `false`
* *Default:* `false`

### `findbugsIncludeFilters`
* *Description:* Optional filter file XML content defining which bug instances to include in the static analysis.
* *Accepts:* `None` and `Option[Node]`
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name := "findbugs4sbt"

organization := "de.johoop"

version := "1.4.0"
version := "1.4.1-SNAPSHOT"

scalaVersion := "2.10.3"

Expand Down
3 changes: 3 additions & 0 deletions project/scripted.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
libraryDependencies <+= (sbtVersion) { sv =>
"org.scala-sbt" % "scripted-plugin" % sv
}
7 changes: 7 additions & 0 deletions scripted.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ScriptedPlugin.scriptedSettings

scriptedLaunchOpts := { scriptedLaunchOpts.value ++
Seq("-Xmx1024M", "-XX:MaxPermSize=256M", "-Dplugin.version=" + version.value)
}

scriptedBufferLog := false
2 changes: 1 addition & 1 deletion src/main/scala/de/johoop/findbugs4sbt/CommandLine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private[findbugs4sbt] trait CommandLine extends Plugin with Filters with Setting
def findbugsCallArguments = paths.analyzedPath map (_.getPath)

def findbugsCallOptions = {
if (paths.reportPath.isDefined && ! misc.reportType.isDefined)
if (paths.reportPath.isDefined && misc.reportType.isEmpty)
sys.error("If a report path is defined, a report type is required!")

val auxClasspath = paths.auxPath ++ (findbugsClasspath.files filter (_.getName startsWith "jsr305"))
Expand Down
33 changes: 27 additions & 6 deletions src/main/scala/de/johoop/findbugs4sbt/FindBugs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,40 @@
package de.johoop.findbugs4sbt

import sbt._
import Keys._
import sbt.Keys._

object FindBugs extends Plugin
object FindBugs extends Plugin
with Settings with CommandLine with CommandLineExecutor {

override def findbugsTask(findbugsClasspath: Classpath, compileClasspath: Classpath,
paths: PathSettings, filters: FilterSettings, misc: MiscSettings, javaHome: Option[File],
override def findbugsTask(findbugsClasspath: Classpath, compileClasspath: Classpath,
paths: PathSettings, filters: FilterSettings, misc: MiscSettings, javaHome: Option[File],
streams: TaskStreams): Unit = {

val log = streams.log

IO.withTemporaryDirectory { filterPath =>
val cmd = commandLine(findbugsClasspath, compileClasspath, paths, filters, filterPath, misc, streams)
streams.log.debug("FindBugs command line to execute: \"%s\"" format (cmd mkString " "))
executeCommandLine(cmd, javaHome, streams.log)
log.debug("FindBugs command line to execute: \"%s\"" format (cmd mkString " "))
executeCommandLine(cmd, javaHome, log)
}

paths.reportPath foreach(f => {
val warnings = {
val resultFile = paths.reportPath.get
val results = scala.xml.XML.loadFile(resultFile)
results \\ "BugCollection" \\ "BugInstance"
}

if (warnings.nonEmpty) {
val message = s"FindBugs found ${warnings.size} issues"
if (misc.failOnError) {
sys.error(message)
} else {
log.info(message)
}
} else {
log.info("No issues from findbugs")
}
})
}
}
16 changes: 10 additions & 6 deletions src/main/scala/de/johoop/findbugs4sbt/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private[findbugs4sbt] case class MiscSettings(
reportType: Option[ReportType], priority: Priority,
onlyAnalyze: Option[Seq[String]], maxMemory: Int,
analyzeNestedArchives: Boolean, sortReportByClassNames: Boolean,
effort: Effort)
effort: Effort, failOnError: Boolean)

private[findbugs4sbt] trait Settings extends Plugin {

Expand Down Expand Up @@ -71,6 +71,9 @@ private[findbugs4sbt] trait Settings extends Plugin {
/** Whether the reported bug instances should be sorted by class name or not. Defaults to <code>false</code>.*/
val findbugsSortReportByClassNames = SettingKey[Boolean]("findbugs-sort-report-by-class-names")

/** Whether to fail the build if errors are found. Defaults to <code>false</code>.*/
val findbugsFailOnError = SettingKey[Boolean]("findbugs-fail-on-error")

/** Optional filter file XML content defining which bug instances to include in the static analysis.
* <code>None</code> by default. */
val findbugsIncludeFilters = TaskKey[Option[Node]]("findbugs-include-filter")
Expand All @@ -79,7 +82,7 @@ private[findbugs4sbt] trait Settings extends Plugin {
* <code>None</code> by default. */
val findbugsExcludeFilters = TaskKey[Option[Node]]("findbugs-exclude-filter")

protected def findbugsTask(findbugsClasspath: Classpath, compileClasspath: Classpath,
protected def findbugsTask(findbugsClasspath: Classpath, compileClasspath: Classpath,
paths: PathSettings, filters: FilterSettings, misc: MiscSettings, javaHome: Option[File],
streams: TaskStreams): Unit

Expand All @@ -91,14 +94,14 @@ private[findbugs4sbt] trait Settings extends Plugin {
"com.google.code.findbugs" % "findbugs" % "3.0.0" % "findbugs->default",
"com.google.code.findbugs" % "jsr305" % "3.0.0" % "findbugs->default"
),

findbugs <<= (findbugsClasspath, managedClasspath in Compile,
findbugsPathSettings, findbugsFilterSettings, findbugsMiscSettings, javaHome, streams) map findbugsTask,

findbugsPathSettings <<= (findbugsReportPath, findbugsAnalyzedPath, findbugsAuxiliaryPath) map PathSettings dependsOn (compile in Compile),
findbugsFilterSettings <<= (findbugsIncludeFilters, findbugsExcludeFilters) map FilterSettings,
findbugsMiscSettings <<= (findbugsReportType, findbugsPriority, findbugsOnlyAnalyze, findbugsMaxMemory,
findbugsAnalyzeNestedArchives, findbugsSortReportByClassNames, findbugsEffort) map MiscSettings,
findbugsMiscSettings <<= (findbugsReportType, findbugsPriority, findbugsOnlyAnalyze, findbugsMaxMemory,
findbugsAnalyzeNestedArchives, findbugsSortReportByClassNames, findbugsEffort, findbugsFailOnError) map MiscSettings,

findbugsClasspath := Classpaths managedJars (findbugsConfig, classpathTypes value, update value),

Expand All @@ -109,6 +112,7 @@ private[findbugs4sbt] trait Settings extends Plugin {
findbugsMaxMemory := 1024,
findbugsAnalyzeNestedArchives := true,
findbugsSortReportByClassNames := false,
findbugsFailOnError := false,
findbugsAnalyzedPath := Seq(classDirectory in Compile value),
findbugsAuxiliaryPath := (dependencyClasspath in Compile).value.files,
findbugsOnlyAnalyze := None,
Expand Down
13 changes: 13 additions & 0 deletions src/sbt-test/findbugs/findbugs-fail/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import de.johoop.findbugs4sbt.FindBugs

name := "findbugs-fail"

organization := "de.johoop"

version := "1.4.1-SNAPSHOT"

FindBugs.findbugsSettings

FindBugs.findbugsFailOnError := true

FindBugs.findbugsReportPath := Some(target.value / "findbugs" / "report.xml")
7 changes: 7 additions & 0 deletions src/sbt-test/findbugs/findbugs-fail/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
val pluginVersion = System.getProperty("plugin.version")
if(pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("de.johoop" % "findbugs4sbt" % pluginVersion)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.johoop.findbugs4sbt;

import java.util.List;

public class TestClass {

private List<String> strings;
private int n;

public TestClass(int n) {
this.n = n;
}

public static void main(String[] args) {
System.out.println("Hello, world!");
}

public List<String> getStrings() {
return strings;
}

public void setN(int n) {
n = n;
}

}
3 changes: 3 additions & 0 deletions src/sbt-test/findbugs/findbugs-fail/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Check the build fails when running findbugs
-> compile:findbugs
$ exists target/findbugs/report.xml
11 changes: 11 additions & 0 deletions src/sbt-test/findbugs/findbugs/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import de.johoop.findbugs4sbt.FindBugs

name := "findbugs"

organization := "de.johoop"

version := "1.4.1-SNAPSHOT"

FindBugs.findbugsSettings

FindBugs.findbugsReportPath := Some(target.value / "findbugs" / "my-findbugs-report.xml")
7 changes: 7 additions & 0 deletions src/sbt-test/findbugs/findbugs/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
val pluginVersion = System.getProperty("plugin.version")
if(pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("de.johoop" % "findbugs4sbt" % pluginVersion)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.johoop.findbugs4sbt;

import java.util.List;

public class TestClass {

private List<String> strings;
private int n;

public TestClass(int n) {
this.n = n;
}

public static void main(String[] args) {
System.out.println("Hello, world!");
}

public List<String> getStrings() {
return strings;
}

public void setN(int n) {
n = n;
}

}
3 changes: 3 additions & 0 deletions src/sbt-test/findbugs/findbugs/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Check that the findbugs report is created
> compile:findbugs
$ exists target/findbugs/my-findbugs-report.xml