Sonar Tomacat Configuration

Source: Internet
Author: User
Tags continuous integration tools

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 ( 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 "", these parameters can be modified in ${sonar_home}/conf/

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/ 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/ 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 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; lists some common configuration and default values.

If there is an error during startup, refer to my other blog (

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:








<>http://localhost:8080/sonar</> <!--sonar server access address--







Note: on-line reference information, for <> 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,<> 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
  1. <project>
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.codehaus.sonar</groupId>
  6. <artifactid>sonar-maven-plugin</artifactid>
  7. <version>3.5.1</version>
  8. </plugin>
  9.    </plugins>
  10. </Build>
  11. </Project>

F. You can explicitly bind sonar to the MAVEN life cycle, as follows:

[XML]View PlainCopy
  1. <plugin>
  2. <groupId>org.codehaus.sonar</groupId>
  3. <artifactid>sonar-maven-plugin</artifactid>
  4. <version>3.5.1</version>
  5. <executions>
  6. <execution>
  7. <ID>sonar</ID>
  8. <phase>site</phase>
  9. <goals>
  10. <goal>sonar</goal>
  11. </Goals>
  12. </Execution>
  13. </Executions>
  14. </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.

Sonar Tomacat Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.