Configuring the new code calculation for your project
By default, your project applies the baseline for new code (global-level new code definition). This means it consistently uses the baseline for new code. Consequently, any modifications to the baseline will automatically be applied to your project. You can apply a specific new code definition for your project instead.
By default, any branch in your project applies the project’s new code definition. You can apply a specific new code option instead.
For more information, see About new code.
Setting a specific new code definition for your project or branch
As a project administrator, you can set the new code definition for your project or branch in the UI (except the Specific analysis option) or using the Web API, at creation time or anytime later as explained below.
For more compliance with the Clean as You Code methodology, the Specific analysis option can only be set using the Web API, as it would require frequent user action to be kept up to date.
You can configure the Reference branch option through an analysis parameter on the CI/CD host. See Additional setup and recommendations > If using Reference branch option below.
In the UI
To set a specific new code definition for your project or branch in the UI:
1. Retrieve your project.
2. Go to Project Settings > New Code.
3. In Choose the baseline for new code for this project, select Define a specific setting for this project and then the option you want to apply to your project.

4. To apply a specific setting to a branch:
- Navigate to the Set a specific setting for a branch section at the bottom of the page.
- In the Actions column of the branch, select the pen icon. The New Code for <branch> dialog opens.
- Select the new code option you want to apply to the branch.
- Select Save.

Via the Web API
Use the api/new_code_periods/set endpoint by specifying:
- To set a specific definition for your project: the project
- To set a specific definition for a branch: the project and branch.
Additional setup and recommendations
Make sure to follow the recommendations about the code checkout and SCM integration.
If using any option except Reference branch
When using any new code option other than Reference branch, we recommend completing your merges using the fast-forward option without a merge commit; examples include GitHub’s squash and merge or rebase and merge options. That way, blame for merged commits will always have a more recent commit date.
If using Previous version option
The current version of a project is determined in different ways depending on the build system:
- If the analysis is done using the SonarScanner for Maven, then SonarQube Server reads the version from the
pom.xml
file. - If the analysis is done with the SonarScanner for Gradle then SonarQube Server reads the version from the
build.gradle
file. - In all other cases, you must explicitly specify the version by setting the analysis parameter
sonar.projectVersion
.
If using Reference branch option
You can use the sonar.newCode.referenceBranch
property to apply the Reference branch option to the analysis of a branch, overriding the global and project-level new code definition set in the UI. This property specifies the reference branch value.
This setting is particularly useful during the first analysis when the branch to be analyzed does not exist yet in SonarQube (i.e. you cannot set a new code definition for this branch in SonarQube).
Related pages
Was this page helpful?