Deploy SonarQube on Windows Server


All information needed can be found at my post Analyze Java with SonarQube and SonarScanner.

By this, you can deploy SonarQube on a remote server, and only deploy SonarScanner locally. Run SonarScanner and get your analysis remotely!

Step.1 Preparations

On your Windows Server, do exactly what you do locally. For more information, just refer to my old post.

Analyze Java with SonarQube and SonarScanner

Here, I’ll just point out something different.

PostgreSQL Configuration

Server also need restart. :(

When adding new server, password field is required. It is the password of the user, here, it is postgres, the super user.

image-20230107121953414

Notice that, if you entered the wrong password, it will say cannot connect the server, rather than wrong password.

Step.2 Launch SonarQube!

Just do what you did locally, and run StartSonar.bat. It may take quite long on remote server with low performance. It may have a lot of warning, but you can ignore them. If everything goes well, you will get SonarQube fully operational!

image-20230107124614053

And then, you can visit SonarQube remotely! Isn’t it great!

image-20230107125407204

Remember to add firewall rule for port 9000 for your server.

Step.3 Configure SonarQube

3.1 Configure SonarQube

This is done on the remote server.

Well, this remains the same as local configuration.

Add following parameters to the Sonar\SonarQube\conf\sonar.properties.

1
2
3
4
5
6
# Your database
sonar.jdbc.url=jdbc:postgresql://127.0.0.1:5432/SonarQube?currentSchema=public
# Database user name
sonar.jdbc.username=Sonar
# Database password
sonar.jdbc.password=sonar

3.2 Configure SonarScanner

This is done on your local machine.

Well, since SonarScanner is deployed locally, you have to change sonar.host.url to your remote server.

Add following parameters to the Sonar\SonarScanner\conf\sonar-scanner.properties.

1
2
3
4
5
# ----- Default SonarQube server
sonar.host.url=http://xxx.xxx.xxx.xxx:9000

# ----- Default source code encoding
sonar.sourceEncoding=UTF-8

3.3 Configure Java Project

This is done, of course, on your local machine.

Find your Java project, and create sonar-project.properties in the root directory with the following parameters. Remember to replace parameters with your own.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# must be unique in a given SonarQube instance
sonar.projectKey=myscs-4

# this is the name displayed in the SonarQube UI
sonar.projectName=myscs-4
sonar.projectVersion=0.1.0

# Path is relative to the sonar-project.properties file
# Replace "\" by "/" on Windows
# If not set, SonarQube starts looking for source code from the
# directory containing sonar-project.properties file
sonar.sources=src

# Path for classes
sonar.java.binaries=out/production/scs-4

sonar.language=java
sonar.sourceEncoding=UTF-8

# SonarQube website username and password
# or remove sonar.password and replace sonar.login with your token
sonar.login=admin
sonar.password=password

Step.4 Run SonarScanner

Now, you can run sonar-scanner under your Java project folder, and get results on the remote server!

image-20230107131727792

You may get such a warning after a scan. It is because SonarQube failed to connect the PostgreSQL database. Check Sonar\SonarQube\conf\sonar.properties, and see if the parameters match database settings. Pay attention to username and password.

This is it, and you’re all set! See you around! :)