Recently in learning Sonar, configured for several days, only to build up the environment, for their own learning ability feel ashamed, hurriedly in this record, so-called good memory than bad pen.
1. Sonar Introduction
Sonar is an open source platform for code Quality management that manages the quality of Java source code.
Through the plug-in mechanism, Sonar can integrate different test tools, code analysis tools, and continuous integration tools. These results are re-processed by different plug-ins and quantified to measure the change in code quality, which makes it easy to code quality management for projects of different sizes and types.
Sonar also provides interface support for a large number of continuous integration tools, which makes it easy to use sonar in continuous integration.
In addition, Sonar's plug-in provides support for programming languages other than Java, as well as internationalization and reporting documentation.
2. Install, configure Sonar
The operation of Sonar requires the support of JDK 1.5+, Maven 2.0.9+, and therefore requires the installation of the above two software in the system; Download sonar from http://www.sonarqube.org/downloads/ ( Latest version 3.5.1) zip file, unzip to any directory, complete the installation.
There are two ways to start sonar:
The first type: Directly start the ${sonar_home}/bin of the corresponding system script can be.
Windows environment, start ${sonar_home}/bin/windows-x86-32/startsonar.bat, access in the browser: http://localhost:9000/, the interface is as follows:
Because Sonar comes with a JETTY6 application Server environment, no additional configuration is required to use it.
SONAR default port is "9000", the default context path is "/", the default network interface is "0.0.0.0", these parameters can be modified in ${sonar_home}/conf/sonar.properties.
The default administrator account and password are:admin/admin.
The second type: As a Web project, deploy to an application server such as Tomcat.
The steps are as follows (in Tomcat, for example):
A. Edit conf/sonar.properties revert to standard format (that is, do not modify ports, etc.) and ensure that conf/wrapper.conf is not used when deploying to the application server;
B. Execute the Build-war.bat script under the ${sonar_home}/war directory; Deploy the generated Sonar.war to the application server;
C. Launch tomcat, accessed via http://localhost:8080/sonar .
To avoid memory overflow, increase the size of the memory stack: Set the CATALINA_OPTS environment variable before Tomcat starts:
Catalina_opts= "-xms1024m-xmx1024m-dorg.apache.jasper.runtime.bodycontentimpl.limit_buffer=true-xx:maxpermsize= 256m "
3. Database installation configuration and database connection configuration
Sonar needs a database to store the results, Apache Derby is the database that sonar comes with and is installed by default, and it's good for sonar demos. However, it is recommended to use a better-performing and more powerful database in a real-world project.
Sonar supports the following databases: MySQL 5.x, Oracle 10g XE, Postgresql, MS SQL Server 2005, and more. (for example, MySQL ):
A. Edit the ${sonar_home}/conf/sonar.properties configuration database :
B. Configure the DB driver package.
If you are using an Oracle database, you must manually copy the driver class to the ${sonar_home}/extensions/jdbc-driver/oracle/directory, and the other supported databases provide drivers by default.
After the above steps, restart the sonar service (using Tomat external server, need to re-fight the war package, restart the server), will automatically create a database named Sonar, the account number and the password is sonar, sonar users (sonar default); The sonar service can be accessed through a browser.
note : tested , found if not manually created first Sonar when the user , Sonar cannot be created correctly ( This is related to the user rights of the database itself , default account is not correct , or insufficient permissions , The database cannot be created naturally )! Create a database manually here first (Sonar) and Users (sonar), also gives Sonar User Authorization . The statement is as follows :
CREATE DATABASE sonar CHARACTER SET UTF8 COLLATE utf8_general_ci; CREATE USER ' sonar ' identified by ' sonar '; GRANT all on sonar.* to ' sonar ' @ '% ' of ' identified by ' sonar '; GRANT all on sonar.* to ' sonar ' @ ' localhost ' identified by ' sonar '; FLUSH privileges; |
http://docs.codehaus.org/display/SONAR/Analysis+Parameters lists some common configuration and default values.
If there is an error during startup, refer to my other blog (http://allen-j-will.iteye.com/blog/1897180)
4 , integrated maven
Sonar uses the Maven2 plugin to parse the source code and inject the results into the database. Therefore, you need to set the properties of the database in MAVEN configuration.
To modify the ${maven_home}/conf/settings.xml file or the ~/.m2/settings.xml file, add the following profile:
<profile> <id>sonar</id> <properties> <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url> <sonar.jdbc.driver>com.mysql.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> <!--sonar server access address-- </properties> </profile> <activeProfiles> <activeProfile>nexus</activeProfile> <activeProfile>sonar</activeProfile> </activeProfiles> |
Note: on-line reference information, for <sonar.host.url> properties, only configured to the port, I test in the local, found that always not, this should be related to the startup mode of Sonar;
If you start the sonar service separately, the above configuration should be OK (not tested), and if you deploy sonar to the server, the,<sonar.host.url> property needs to be configured to the context. This is especially easy to miss!!!
Similarly, to avoid memory overflow, it is recommended to increase the size of the memory stack. Set the MAVEN_OPTS environment variable:
Set maven_opts= "-xmx512m-xx:maxpermsize=256m"
5. Using sonar
A. Running the sonar server;
B. Inject the code into sonar through MVN Sonar:sonar and store the results in the database as XML;
C. Through browser access, display analysis results;
D. Continuous operation of the MAVEN build will iterate over the results of the analysis;
E. You can explicitly specify the version of the sonar plug-in as follows:
[XML]View PlainCopy
- <project>
- <build>
- <plugins>
- <plugin>
- <groupId>org.codehaus.sonar</groupId>
- <artifactid>sonar-maven-plugin</artifactid>
- <version>3.5.1</version>
- </plugin>
- </plugins>
- </Build>
- </Project>
F. You can explicitly bind sonar to the MAVEN life cycle, as follows:
[XML]View PlainCopy
- <plugin>
- <groupId>org.codehaus.sonar</groupId>
- <artifactid>sonar-maven-plugin</artifactid>
- <version>3.5.1</version>
- <executions>
- <execution>
- <ID>sonar</ID>
- <phase>site</phase>
- <goals>
- <goal>sonar</goal>
- </Goals>
- </Execution>
- </Executions>
- </plugin>
At this point, when you specify the site declaration period for MAVEN, the Sonar.sonar command is called automatically.
6. with the Hudson Integration
Sonar can also be integrated with the Hudson via plug-ins, triggering sonar at the end of each build.
First, you should add Hudson Sonar Plugin to the Hudson plugin management;
Then, in the system setup, add a sonar installation;
Finally, in the post-build actions of the project, select Sonar.
After the project is built, Sonar's tasks are automatically performed.
However, running sonar tasks in the Hudson integration may require a reference to sonar's documentation to modify the MAVEN configuration accordingly.
This article refers to the following article, here to express our thanks.
http://www.ibm.com/developerworks/cn/java/j-lo-sonar/
http://pengwei841221.iteye.com/blog/940428
http://digitalsonic.iteye.com/blog/695304
Sonar Tomacat Configuration