Sonar uses a Derby or H2 as default database. When running Sonar, it says that these databases may only be used for evaluation. Now, in this article, I will tell you how to change the default database and how to configure Maven to use it.
Installation
First, download Sonar from its website at http://www.sonarqube.org/downloads/. Then, extract the zip file and the we can start configuring. Note that I will demonstrate the configuration with a MySQL database. You can use any other database you prefer.
Configuration
In the sonar folder, go to the conf-folder and open sonar.properties. You will see many commented lines. First comment line 48. That’s the connection String to the default database Sonar uses.
1 2 |
# Comment the following line to deactivate the default embedded database. # sonar.jdbc.url: jdbc:h2:tcp://localhost:9092/sonar |
Then, I will change line 40 and 41. These are the login credentials to my database. For MySQL, it will be:
1 2 |
sonar.jdbc.username: root sonar.jdbc.password: |
After that, we need to uncomment the MySQL connection string. (Note that other database may uncomment more properties).
1 2 3 |
#----- 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 |
When that’s finished, we can start Sonar, but before we do that, make sure your database is running and that the Sonar database is created!. To start sonar, go to the bin-folder, select your operating system folder and click on StartSonar.xxx. For me, it will be windows-x86-32 and StartSonar.bat. After starting up, a command box will appear, create the needed tales in your database and start up. To see if Sonar is up and running, got to http://localhost:9000
Eclipse plugin
Now sonar is up and running, we need to configure Eclipse and Maven to use sonar. First, install the Eclipse plugin for Sonar. You can find it at http://dist.sonar-ide.codehaus.org/eclipse/. After installing the plugin, we need a little configuration in Maven to use the plugin.
Maven configuration
When you run Sonar in Maven without a configuration, Maven will still use the default database. We need to add a profile in the Maven settings. In the .m2 folder, open the settings.xml. Then add the code below. You also need to do that in the settings.xml in your Maven conf folder. You can find other database profiles at http://mojo.codehaus.org/sonar-maven-plugin/examples/use-enterprise-database.html
1 2 3 4 5 6 7 8 9 10 11 12 |
<profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url> <sonar.jdbc.username>root</sonar.jdbc.username> <sonar.jdbc.password></sonar.jdbc.password> <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver> </properties> </profile> |
Update Maven in Eclipse
Now sonar and Maven are configured, we need to update your project in Eclipse. Right click on your project in Eclipse, go to Maven en click on Update Project. Then the Maven settings will become active.
Running Sonar in Eclipse
Now we are ready to run Sonar in Eclipse. Right click on your root project, select run as and click on run configurations. Create a new Maven build and insert into your goal: clean install sonar:sonar. Then click on run and your project will show up in Sonar. Enjoy!
8 comments for “How to change Sonar default database”