15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Play Framework 2.2でFindBugsとJaCoCo, PMD, CPDを利用してJenkinsで表示

Last updated at Posted at 2014-04-04

Play FrameworkでFindBugs, JaCoCo, CPDによるチェックをおこなう。
sbtコミュニティリポジトリの追加が必要。

最新版のバージョンなどはリポジトリ内で確認した

project/plugins.sbtにリポジトリを追加する。typesafeのリポジトリに下に一行あけて追記した。

project/plugins.sbt
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

resolvers += "sbt community repository" at "http://dl.bintray.com/sbt/sbt-plugin-releases/"

同じくproject/plugins.sbtにプラグイン追加を追記する。

project/plugins.sbt

libraryDependencies ++= Seq(
  "net.sourceforge.pmd" % "pmd" % "5.1.0"
)

addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.4")

addSbtPlugin("de.johoop" % "findbugs4sbt" % "1.3.0")

addSbtPlugin("de.johoop" % "cpd4sbt" % "1.1.4")

addSbtPlugin("de.johoop" % "sbt-testng-plugin" % "3.0.0")

プロジェクトフォルダ直下にbuild.sbtファイルを作成する。

build.sbt
import de.johoop.jacoco4sbt._
import JacocoPlugin._
import de.johoop.findbugs4sbt.FindBugs._
import de.johoop.cpd4sbt.CopyPasteDetector._

play.Project.playJavaSettings

seq(jacoco.settings : _*)

findbugsSettings

cpdSettings

project/Build.scalaからbuild.sbtへの移行がまだよくわかっていないのでとりあえずBuild.scalaを作ってPMDを実行する。
project/pmd-ruleset.xmlにPMDのルールセットを用意する。

ここを参考にさせてもらった。
https://github.com/ymasory/play-pmd-checkstyle

project/Build.scala
import sbt._
import Keys._
import play.Project._
import de.johoop.jacoco4sbt._
import JacocoPlugin._
import de.johoop.findbugs4sbt._
import de.johoop.findbugs4sbt.FindBugs._
import de.johoop.cpd4sbt.CopyPasteDetector._

object ApplicationBuild extends Build {

  val appName         = "MyProject"
  val appVersion      = "1.0-SNAPSHOT"

  val main = play.Project(appName, appVersion).settings(
    ProjectSettings.all: _*
  )  
}

object ProjectSettings {

  def all: Seq[sbt.Project.Setting[_]] = List(
    PmdSettings.all
  ).flatten

  object PmdSettings {

    val pmd = TaskKey[Unit]("pmd", "run PMD")
    val pmdTask = pmd <<=
      (streams, baseDirectory, sourceDirectory in Compile, target) map {
        (streams, base, src, target) =>
        import net.sourceforge.pmd.PMD.{ main => PmdMain }
        import streams.log

        val args = List(
          "-d",
          src.getAbsolutePath,
          "-f",
          "xml",
          "-R",
          (base / "project" / "pmd-ruleset.xml").getAbsolutePath,
          "-reportfile",
          (target / "scala-2.10/pmd.xml").getAbsolutePath
        )

        log info ("using pmd args " + args)
        trappingExits {
          PmdMain(args.toArray)
        }
      }
      val all = Seq(pmdTask)
  }

  def trappingExits(thunk: => Unit): Unit = {
    val originalSecManager = System.getSecurityManager
    case class NoExitsException() extends SecurityException
    System setSecurityManager new SecurityManager() {
      import java.security.Permission
      override def checkPermission(perm: Permission) {
        if (perm.getName startsWith "exitVM") throw NoExitsException()
      }
    }
    try {
      thunk
    } finally {
      System setSecurityManager originalSecManager
    }
  }
}

実行はplayコマンドのオプション

command
play jacoco:cover findbugs cpd pmd

レポートの出力先は下記の通り。

  • target/scala-2.10/jacoco/
  • target/scala-2.10/cpd/cpd.xml
  • target/scala-2.10/findbuts/report.xml
  • target/scala-2.10/pmd.xml

Jenkinsでこれらのレポートを読み込むには下記の記事の通り。

Jenkins+GradleでJavaのCIのための基本build.gradle設定 (JUnit,PMD,FindBugs,CPD,JaCoCo)
http://qiita.com/mychaelstyle/items/74baa62b7bf2fe81e309

15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?