How to integrate Sonar (SonarQube) with iOS Project?
Step 1: Download and setup SonarQube
- Go to SonarQube website : https://www.sonarqube.org/downloads/
- Select Developer Edition and start Free Trial. [Note you need to purchase full license for full integration and check sonar dashboard]
- Click Download and accept T&C.
4. Unzip downloaded file.
5. Move downloaded file under /Applications/ directory.
6. Rename folder to SonarQube.
Step 2: Download and setup SonarScanner
- Download SonarScanner from: https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/
Select Mac OS X 64 bit and Download Mac OS X specific SonarScanner
2. Unzip downloaded file.
3. Move downloaded file under /Applications/ directory.
4. Rename folder to SonarScanner.
Step 3: Update .bash_profile with new path
1. Open Terminal and run following command.
$ cd ~/
$ vi .bash_profile
2. Above commands will open your bash_profile in vi editor.
3. Use down-arrow key to jump to last line.
4. Use left-right arrows to navigate to last character.
5. Press i to enable insert mode.
6. Copy & paste following lines.
# Sonar Setting
export PATH=$PATH:/Applications/SonarScanner/bin
export PATH=$PATH:/Applications/SonarQube/bin
7. Press esc key and : will appear at bottom-left corner in vi editor. [If : does not appear press w+q+shift+! keys at once and remove any character typed after :]
8. Type wq! keys and press enter to save.
If you face any issues like below —
Type R and press enter multiple times until you see —
If you see the paths are not correct as mentioned earlier, repeat from step 3.
Step 4: Starting up SonarQube
- Open Terminal and run following command to start SonarQube server.
sh /Applications/SonarQube/bin/macosx-universal-64/sonar.sh console
2. Your console will look like below if everything goes well -
Step 5: Open SonarQube Dashboard
1. Go to browser. Open following URL.
http://localhost:9000/about
2. Click on Log in.
3. Use admin as username, admin as password.
Note — For first time setup it will ask you to change your password after step 3.
4. Now you have to create a project in Dashboard —
A). Enter your project display name and click setup.
B). Select locally, if you want to analyse your local code on your machine —
C). Enter any token name to generate token and click continue.
Now SonarQube server setup is done. Now we will integrate this with your XCode project.
Step 6: Integrate with XCode project
- Hope you already have XCode project [If not, create one]. Open XCode project
- Edit project scheme, Select Test and Check Gather code coverage for all target. [Do this if you have test targets]
- Create sonar-project.properties file in your project root directory and copy below content and paste in your file -
# Sonar Server details
sonar.host.url=http://localhost:9000
sonar.login=admin
#use your latest password, if you have changed in Step 5 — Point 3
sonar.password=admin
# must be unique in a given SonarQube instance
sonar.projectKey=test
# — — optional properties — -
# defaults to project key
#sonar.projectName=test
# defaults to ‘not provided’
#sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Defaults to .
sonar.sources=.
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
# Xcode project configuration (.xcodeproj)
# and use the later to specify which project(s) to include in the analysis (comma separated list)
# Specify either xcodeproj or xcodeproj + xcworkspace
sonar.swift.project=test.xcodeproj
sonar.swift.workspace=test.xcworkspace
# Scheme to build your application
sonar.swift.appScheme=test
# Destination Simulator to run surefire
# As string expected in destination argument of xcodebuild command
# Example = sonar.swift.simulator=platform=iOS Simulator,name=iPhone 7,OS=12.0
sonar.swift.simulator=platform=iOS Simulator,name=iPhone 12 Pro Max,OS=14.2
# Exclude directories
# sonar.test.inclusions=**/*Test*/**
# sonar.test.inclusions=*.swift
sonar.exclusions=**/*.xml,Pods/**/*,Reports/**/*
sonar.inclusions=*.swift
# Path to test directories (comment if no test)
# sonar.tests=testTests,testUITests
4. Open Terminal and cd to your project root directory
5. Run following command to start SonarScanner on your project.
sh /Applications/SonarScanner/bin/sonar-scanner
and SonarScanner will perform successful execution like below console —
Since this is your non-licensed SonarQube so your dashboard will automatically refresh as below —
If you have licensed version of SonarQube, Your dashboard will have information like —