1.下载:
sonar3.7.4 : http://dist.sonar.codehaus.org/sonar-3.7.4.zip
maven3.0.5 : apache-maven-3.0.5-bin.zip
tomcat 6: http://download.nextag.com/apache/tomcat/tomcat-6/v6.0.39/bin/apache-tomcat-6.0.39-windows-x86.zip
2.添加maven的环境变量M2_HOME,添加Path;在cmd中测试mvn -version(需要jdk及jdk环境变量配置);
3.配置maven的conf\setting.xml: (下面的sonar.host.url 为tomcat6启动的sonar访问地址;若未使用tomcat ,使用默认的http://localhost:9000进行访问)
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>webproxy.ssmb.com</host>
<port>8080</port>
</proxy>
</proxies>
<servers>
</servers>
<mirrors>
<!--<mirror>
<id>ibiblio.org</id>
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
-->
<mirror>
<id>antelink.com</id>
<url>http://maven.antelink.com/content/repositories/central</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!--使用内嵌数据库,若使用其他数据库需配置<sonar.jdbc.url> dbc:h2:tcp://localhost:9092/sonar</sonar.jdbc.url>
<sonar.jdbc.driver> </sonar.jdbc.driver>
-->
<sonar.jdbc.username> sonar</sonar.jdbc.username>
<sonar.jdbc.password> sonar</sonar.jdbc.password>
<sonar.host.url>http://localhost:8080/sonar/</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
4.sonar配置:
4.1.数据库配置:建立sonar数据库,sonar用户和对应权限
4.2:配置conf\sonar.properties(数据库url和driver);下面使用default database
#--------------------------------------------------------
# This file must contain only ISO 8859-1 characters
# see http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Properties.html#load(java.io.InputStream)
#
# To use an environment variable, use the following syntax : ${env:NAME_OF_ENV_VARIABLE}
# For example :
# sonar.jdbc.url: ${env:SONAR_JDBC_URL}
#
#
# See also the file conf/wrapper.conf for JVM advanced settings
#---------------------------------------------------------
#---------------------------------------------------------
# WEB SETTINGS - STANDALONE MODE ONLY
# These settings are ignored when the war file is deployed to a JEE server.
#---------------------------------------------------------
# Listen host/port and context path (for example / or /sonar). Default values are 0.0.0.0:9000/.
#sonar.web.host: 0.0.0.0
#sonar.web.port: 9000
#sonar.web.context: /
# Log HTTP requests. Deactivated by default.
#sonar.web.jettyRequestLogs: ../../logs/jetty-yyyy_mm_dd.request.log
#sonar.web.jetty.threads.min: 5
#sonar.web.jetty.threads.max: 50
#-----------------------------------------------------------------------
# DATABASE
#
# IMPORTANT : the embedded database H2 is used by default.
# It is recommended for tests only. Please use an external database
# for production environment (MySQL, Oracle, Postgresql, SQLServer)
#
#-----------------------------------------------------------------------
#----- Credentials
# Permissions to create tables and indexes must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username: sonar
sonar.jdbc.password: sonar
#----- Embedded database H2
# Note : it does not accept connections from remote hosts, so the
# SonarQube server and the maven plugin must be executed on the same host.
# Comment the following line to deactivate the default embedded database.
sonar.jdbc.url: jdbc:h2:tcp://localhost:9092/sonar
# directory containing H2 database files. By default it's the /data directory in the SonarQube installation.
#sonar.embeddedDatabase.dataDir:
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port: 9092
#----- MySQL 5.x
# Comment the embedded database and uncomment the following line to use MySQL
#sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
#----- Oracle 10g/11g
# To connect to Oracle database :
#
# - It's recommended to use the latest version of the JDBC driver (ojdbc6.jar).
# Download it in http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
# - Copy the driver to the directory extensions/jdbc-driver/oracle/
# - Comment the embedded database and uncomment the following line :
#sonar.jdbc.url: jdbc:oracle:thin:@localhost/XE
# Uncomment the following property if the Oracle account has permissions to access multiple schemas,
# for example sonar schemas with different versions. In that case, use the same property during project analysis
# (-Dsonar.jdbc.schema=<schema>)
# The schema is case-sensitive.
#sonar.jdbc.schema: sonar
#----- PostgreSQL 8.x/9.x
# Comment the embedded database and uncomment the following property to use PostgreSQL
#sonar.jdbc.url: jdbc:postgresql://localhost/sonar
# Uncomment the following property if the PostgreSQL account has permissions to access multiple schemas,
# for example sonar schemas with different versions. In that case, use the same property during project analysis
# (-Dsonar.jdbc.schema=<schema>)
#sonar.jdbc.schema: public
#----- Microsoft SQLServer
# The Jtds open source driver is available in extensions/jdbc-driver/mssql. More details on http://jtds.sourceforge.net
#sonar.jdbc.url: jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
#----- Connection pool settings
sonar.jdbc.maxActive: 20
sonar.jdbc.maxIdle: 5
sonar.jdbc.minIdle: 2
sonar.jdbc.maxWait: 5000
sonar.jdbc.minEvictableIdleTimeMillis: 600000
sonar.jdbc.timeBetweenEvictionRunsMillis: 30000
#---------------------------------------------------------
# UPDATE CENTER
#---------------------------------------------------------
# The Update Center requires an internet connection to request http://update.sonarsource.org
# It is activated by default:
#sonar.updatecenter.activate=true
# HTTP proxy (default none)
#http.proxyHost=
#http.proxyPort=
# NT domain name if NTLM proxy is used
#http.auth.ntlm.domain=
# SOCKS proxy (default none)
#socksProxyHost=
#socksProxyPort=
# proxy authentication. The 2 following properties are used for HTTP and SOCKS proxies.
#http.proxyUser=
#http.proxyPassword=
#---------------------------------------------------------
# NOTIFICATIONS
#---------------------------------------------------------
# Delay (in seconds) between processing of notification queue
sonar.notifications.delay=60
5.建立java工程:
1.目录如下:HelloTest使用junit4;
2.工程中的pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>SonarProject1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SonarProject1</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <sonar.language>java</sonar.language> <sonar.sources>src</sonar.sources> <sonar.tests>test</sonar.tests> <sonar.dynamicAnalysis>true</sonar.dynamicAnalysis> <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <testSourceDirectory>test</testSourceDirectory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>coverage-per-test</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.13</version> <configuration> <properties> <property> <name>listener</name> <value>org.sonar.java.jacoco.JUnitListener</value> </property> </properties> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.codehaus.soanr-plugins.java</groupId> <artifactId>sonar-jacoco-listeners</artifactId> <version>1.2</version> <scope>test</scope> </dependency> </dependencies> </profile> </profiles> </project>
6.启动sonar服务器,两种方式:
1.直接启动:不适用tomcat6 ;需要修改步骤3里面的sonar.host.url,步骤三有写;
2.在配置好sonar后,使用war\bulid-war.bat 进行打包;
将war包放在tomcat的webapp中,启动;
(若报outofMemery exception:在tomcat的bin\catalina.bat中添加
set JAVA_OPTS= %JAVA_OPTS% -server -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256M)
如下
rem Guess CATALINA_HOME if not defined set JAVA_OPTS= %JAVA_OPTS% -server -Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256M set "CURRENT_DIR=%cd%" if not "%CATALINA_HOME%" == "" goto gotHome set "CATALINA_HOME=%CURRENT_DIR%" if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome cd .. set "CATALINA_HOME=%cd%" cd "%CURRENT_DIR%" :gotHome if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome echo The CATALINA_HOME environment variable is not defined correctly echo This environment variable is needed to run this program goto end :okHome
7.分析工程:
1.在cmd命令下,进入到需要分析的工程根目录下,包含pom.xml文件
2.执行 mvn clean install (-X)
3.执行mvn sonar:sonar (-X)
4.打开http://localhost:9000 或者 http://localhost:8080/sonar (tomcat启动sonar)查看项目分析情况
分析成功。。。