Javaソースコードのカバレッジツールを探していたらSonarというJavaソースコードのメトリクス収集ツールに行き当たりました。
これまでソースをWebで見れるようにするツール等は様々出回っていましたが、総合的にメトリクスを収集してくれるツールは、多くは無かったのではないかと思います。
カバレッジのみならず、CheckstyleやPMD等も自動的に実行されとても便利です。どんなツールか感じを掴むとっかかりとしてご覧ください。
※JavaとMavenはインストールされている前提です。
※コマンド等は Windows7 64bit 環境での実行例です。
・まずはSonarのインストール
http://www.sonarsource.org/downloads/ から sonar-3.4.zip をダウンロード、適当なディレクトリに解凍する。
・Sonarを起動する
sonar-3.4/bin/windows-x86-64/StartSonar.bat を実行する。
wrapper | --> Wrapper Started as Console wrapper | Launching a JVM... jvm 1 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org jvm 1 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved. jvm 1 | jvm 1 | 2013-01-01 23:41:22.836:INFO::Logging to org.sonar.application.FilteredLogger@339cefd6 via org.sonar.application.FilteredLogger jvm 1 | 2013-01-01 23:41:22.967:INFO::jetty-6.1.25 jvm 1 | 2013-01-01 23:41:23.502:INFO::NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet jvm 1 | JRuby limited openssl loaded. http://jruby.org/openssl jvm 1 | gem install jruby-openssl for full support. jvm 1 | 2013-01-01 23:41:55.976:INFO::Started SelectChannelConnector@0.0.0.0:9000
http://localhost:9000 にアクセスして下記のような画面が表示されればSonarの起動完了。
・Mavenの設定
特に無し。下記のコマンドを順に実行する。
mvn clean install -DskipTests=true mvn sonar:sonar
実行ログを参考までに載せておきます。
ソースコードは http://d.hatena.ne.jp/mokimokisan/20121227/1356624640 に掲載した 20121228_SpringTest.zip を使いました。
c:\SpringTest>mvn clean install -DskipTests=true [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building SpringTest 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ SpringTest --- [INFO] Deleting c:\SpringTest\target [INFO] [INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ SpringTest --- [WARNING] Using platform encoding (MS932 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ SpringTest --- [WARNING] File encoding has not been set, using platform encoding MS932, i.e. build is platform dependent! [INFO] Compiling 6 source files to c:\SpringTest\target\classes [INFO] [INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ SpringTest --- [WARNING] Using platform encoding (MS932 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ SpringTest --- [WARNING] File encoding has not been set, using platform encoding MS932, i.e. build is platform dependent! [INFO] Compiling 1 source file to c:\SpringTest\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ SpringTest --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.3.1:jar (default-jar) @ SpringTest --- [INFO] Building jar: c:\SpringTest\target\SpringTest-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ SpringTest --- [INFO] Installing c:\SpringTest\target\SpringTest-0.0.1-SNAPSHOT.jar to C:\Users\XXXXXXXXX\.m2\repository\SpringTest\SpringTest\0.0.1-SNAPSHOT\SpringTest-0.0.1-SNAPSHOT.jar [INFO] Installing c:\SpringTest\pom.xml to C:\Users\XXXXXXXXX\.m2\repository\SpringTest\SpringTest\0.0.1-SNAPSHOT\SpringTest-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.017s [INFO] Finished at: Wed Jan 02 00:10:42 JST 2013 [INFO] Final Memory: 15M/110M [INFO] ------------------------------------------------------------------------
c:\SpringTest>mvn sonar:sonar [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building SpringTest 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- sonar-maven-plugin:2.0:sonar (default-cli) @ SpringTest --- [INFO] Sonar version: 3.4 [INFO] [00:13:32.427] Load project settings [INFO] [00:13:32.494] Install plugins [INFO] [00:13:34.169] Apply project exclusions [INFO] [00:13:34.171] Install JDBC driver [WARN] [00:13:34.208] H2 database should be used for evaluation purpose only [INFO] [00:13:34.210] Create JDBC datasource for jdbc:h2:tcp://localhost/sonar [INFO] [00:13:34.354] Initializing Hibernate [INFO] [00:13:37.468] ------------- Analyzing SpringTest [INFO] [00:13:37.474] Load module settings [INFO] [00:13:38.740] Quality profile : [name=Sonar way,language=java] [INFO] [00:13:38.795] Configure maven plugins... [INFO] [00:13:38.956] Compare to previous analysis [INFO] [00:13:39.068] Compare over 5 days (2012-12-28) [INFO] [00:13:39.130] Compare over 30 days (2012-12-03) [INFO] [00:13:39.324] JaCoCo agent (version 0.5.10.201208310627) extracted: C:\Users\XXXXXX~1\AppData\Local\Temp\jacocoagent6536198146564021596.jar [INFO] [00:13:39.326] JVM options: -javaagent:C:\Users\XXXXXX~1\AppData\Local\Temp\jacocoagent6536198146564021596.jar=destfile=target/jacoco.exec,excludes=*_javassist_* [INFO] [00:13:39.365] Initializer FindbugsMavenInitializer... [INFO] [00:13:39.405] Initializer FindbugsMavenInitializer done: 40 ms [INFO] [00:13:39.406] Execute maven plugin maven-surefire-plugin... [INFO] [00:13:39.409] Execute org.apache.maven.plugins:maven-surefire-plugin:2.7.2:test... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building SpringTest 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-surefire-plugin:2.7.2:test (default-cli) @ SpringTest --- [INFO] Surefire report directory: c:\SpringTest\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running sample.di.business.service.ProductServiceTest 1 02, 2013 12:13:40 午前 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners 情報: @TestExecutionListeners is not present for class [class sample.di.business.service.ProductServiceTest]: using defaults. 1 02, 2013 12:13:41 午前 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 情報: Loading XML bean definitions from class path resource [applicationContext.xml] 1 02, 2013 12:13:41 午前 org.springframework.context.support.AbstractApplicationContext prepareRefresh 情報: Refreshing org.springframework.context.support.GenericApplicationContext@1ed6ecc2: display name [org.springframework.context.support.GenericApplicationContext@1ed6ecc2]; startup date [Wed Jan 02 00:13:41 JST 2013]; root of context hierarchy 1 02, 2013 12:13:41 午前 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 情報: Bean factory for application context [org.springframework.context.support.GenericApplicationContext@1ed6ecc2]: org.springframework.beans.factory.support.DefaultListableBeanFactory@7afb34d1 1 02, 2013 12:13:41 午前 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 情報: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7afb34d1: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,ProductService,productDaoImpl]; root of factory hierarchy Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.351 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 1 02, 2013 12:13:41 午前 org.springframework.context.support.AbstractApplicationContext doClose 情報: Closing org.springframework.context.support.GenericApplicationContext@1ed6ecc2: display name [org.springframework.context.support.GenericApplicationContext@1ed6ecc2 ]; startup date [Wed Jan 02 00:13:41 JST 2013]; root of context hierarchy [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12.689s [INFO] Finished at: Wed Jan 02 00:13:42 JST 2013 [INFO] Final Memory: 25M/300M [INFO] ------------------------------------------------------------------------ [INFO] [00:13:42.315] Execute org.apache.maven.plugins:maven-surefire-plugin:2.7.2:test done: 2906 ms [INFO] [00:13:42.317] Execute maven plugin maven-surefire-plugin done: 2911 ms [INFO] [00:13:42.320] Initializer JacocoMavenInitializer... [INFO] [00:13:42.322] Initializer JacocoMavenInitializer done: 2 ms [INFO] [00:13:42.324] Initializer ProjectFileSystemLogger... [INFO] [00:13:42.327] Excluded tests: [**/package-info.java] [INFO] [00:13:42.329] Source directories: [INFO] [00:13:42.331] c:\SpringTest\src\main\java [INFO] [00:13:42.334] Test directories: [INFO] [00:13:42.336] c:\SpringTest\src\test\java [INFO] [00:13:42.339] Initializer ProjectFileSystemLogger done: 15 ms [INFO] [00:13:42.341] Initializer MavenInitializer... [INFO] [00:13:42.348] Java source version: 1.5 [INFO] [00:13:42.350] Java target version: 1.5 [INFO] [00:13:42.352] Source encoding: null [INFO] [00:13:42.354] Initializer MavenInitializer done: 13 ms [INFO] [00:13:42.370] Sensor JavaSourceImporter... [INFO] [00:13:42.577] Sensor JavaSourceImporter done: 207 ms [INFO] [00:13:42.578] Sensor JavaSquidSensor... [INFO] [00:13:42.667] Java AST scan... [INFO] [00:13:42.802] Java AST scan done: 135 ms [INFO] [00:13:42.808] Java bytecode scan... [INFO] [00:13:42.872] Java bytecode scan done: 64 ms [INFO] [00:13:42.892] Package design analysis... [INFO] [00:13:42.919] Package design analysis done: 27 ms [INFO] [00:13:42.938] Sensor JavaSquidSensor done: 360 ms [INFO] [00:13:42.940] Sensor JaCoCoSensor... [INFO] [00:13:42.947] Analysing C:\SpringTest\target\jacoco.exec [INFO] [00:13:43.069] Sensor JaCoCoSensor done: 129 ms [INFO] [00:13:43.070] Sensor CpdSensor... [INFO] [00:13:43.072] SonarEngine is used [INFO] [00:13:43.079] Cross-project analysis disabled [INFO] [00:13:43.142] Sensor CpdSensor done: 72 ms [INFO] [00:13:43.144] Sensor CheckstyleSensor... [INFO] [00:13:43.149] Execute Checkstyle 5.6... [INFO] [00:13:43.183] Checkstyle configuration: c:\SpringTest\target\sonar\checkstyle.xml [INFO] [00:13:43.973] Execute Checkstyle 5.6 done: 824 ms [INFO] [00:13:43.983] Sensor CheckstyleSensor done: 839 ms [INFO] [00:13:43.984] Sensor PmdSensor... [INFO] [00:13:43.990] Execute PMD 4.3... [INFO] [00:13:44.010] Java version: 1.5 [INFO] [00:13:44.053] PMD configuration: c:\SpringTest\target\sonar\pmd.xml [INFO] [00:13:45.642] PMD configuration: c:\SpringTest\target\sonar\pmd-unit-tests.xml [INFO] [00:13:45.647] Execute PMD 4.3 done: 1657 ms [INFO] [00:13:45.700] Sensor PmdSensor done: 1716 ms [INFO] [00:13:45.701] Sensor ProfileSensor... [INFO] [00:13:45.978] Sensor ProfileSensor done: 277 ms [INFO] [00:13:45.979] Sensor ProfileEventsSensor... [INFO] [00:13:46.007] Sensor ProfileEventsSensor done: 28 ms [INFO] [00:13:46.008] Sensor ProjectLinksSensor... [INFO] [00:13:46.016] Sensor ProjectLinksSensor done: 8 ms [INFO] [00:13:46.019] Sensor VersionEventsSensor... [INFO] [00:13:46.030] Sensor VersionEventsSensor done: 11 ms [INFO] [00:13:46.033] Sensor Maven dependencies... [INFO] [00:13:46.229] Sensor Maven dependencies done: 196 ms [INFO] [00:13:46.231] Sensor SurefireSensor... [INFO] [00:13:46.236] parsing c:\SpringTest\target\surefire-reports [INFO] [00:13:46.360] Sensor SurefireSensor done: 129 ms [INFO] [00:13:46.806] Execute decorators... [INFO] [00:13:47.990] ANALYSIS SUCCESSFUL, you can browse http://localhost:9000 [INFO] [00:13:47.994] Executing post-job class org.sonar.plugins.core.batch.IndexProjectPostJob [INFO] [00:13:48.112] Executing post-job class org.sonar.plugins.dbcleaner.ProjectPurgePostJob [INFO] [00:13:48.133] -> Keep one snapshot per day between 2012-12-05 and 2013-01-01 [INFO] [00:13:48.138] -> Keep one snapshot per week between 2012-01-04 and 2012-12-05 [INFO] [00:13:48.140] -> Keep one snapshot per month between 2008-01-09 and 2012-01-04 [INFO] [00:13:48.143] -> Delete data prior to: 2008-01-09 [INFO] [00:13:48.150] -> Clean SpringTest [id=1] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 18.969s [INFO] Finished at: Wed Jan 02 00:13:48 JST 2013 [INFO] Final Memory: 21M/359M [INFO] ------------------------------------------------------------------------ c:\SpringTest>
あらためて http://localhost:9000 にアクセスすると、下記のような画面が表示され、Sonarによってメトリクスが収集されたことが分かります。