Skip to content

Unable to obtain git.branch property with SonarQube build #413

Closed
@NicolaSpreafico

Description

@NicolaSpreafico

Hi,
in my Maven project I have configured SonarQube analysis, and I want to use the name of the current git-branch in the analysis version.

Here is my configuration:

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<sonar.projectVersion>${project.version}-${git.branch}</sonar.projectVersion>
	</properties>

And here is the result
image

The build is triggered using mvn clean verify sonar:sonar

I added an echo output to make sure the property is correctly created, and there is:

[INFO] --- maven-antrun-plugin:1.8:run (echo-properties) @ git-test-sonarqube ---
[INFO] Executing tasks

main:
     [echo] git.branch: feature/my_feature
[INFO] Executed tasks

But when the sonarqube plugin is triggered (after the echo, so I would expect to have the property value), on SonarQube dashboard I will find the property not resolved.

Here is my current plugin configuration

<build>
		<plugins>
			<plugin>
				<groupId>pl.project13.maven</groupId>
				<artifactId>git-commit-id-plugin</artifactId>
				<version>2.2.6</version>
				<configuration>
					<injectAllReactorProjects>true</injectAllReactorProjects>
				</configuration>
				<executions>
					<execution>
						<id>get-the-git-infos</id>
						<goals>
							<goal>revision</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.8</version>
				<executions>
					<execution>
						<id>echo-properties</id>
						<phase>compile</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<target>
								<echo>git.branch: ${git.branch}</echo>
							</target>
						</configuration>
					</execution>
				</executions>
			</plugin>

		</plugins>

		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.sonarsource.scanner.maven</groupId>
					<artifactId>sonar-maven-plugin</artifactId>
					<version>3.6.0.1398</version>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>

I enable full log with this command
mvn clean verify sonar:sonar -Dsonar.analysis.mode=preview -Dsonar.verbose=true -X -Dsonar.log.level=DEBUG > R:\log.txt

And found the section related to properties

[DEBUG] Initialize Maven Ant Tasks
parsing buildfile jar:file:/C:/Users/Nicola/.m2/repository/org/apache/maven/plugins/maven-antrun-plugin/1.8/maven-antrun-plugin-1.8.jar!/org/apache/maven/ant/tasks/antlib.xml with URI = jar:file:/C:/Users/Nicola/.m2/repository/org/apache/maven/plugins/maven-antrun-plugin/1.8/maven-antrun-plugin-1.8.jar!/org/apache/maven/ant/tasks/antlib.xml from a zip file
parsing buildfile jar:file:/C:/Users/Nicola/.m2/repository/org/apache/ant/ant/1.9.4/ant-1.9.4.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/C:/Users/Nicola/.m2/repository/org/apache/ant/ant/1.9.4/ant-1.9.4.jar!/org/apache/tools/ant/antlib.xml from a zip file
Class org.apache.maven.ant.tasks.AttachArtifactTask loaded from parent loader (parentFirst)
 +Datatype attachartifact org.apache.maven.ant.tasks.AttachArtifactTask
Class org.apache.maven.ant.tasks.DependencyFilesetsTask loaded from parent loader (parentFirst)
 +Datatype dependencyfilesets org.apache.maven.ant.tasks.DependencyFilesetsTask
Setting project property: git.build.user.email -> XXXXXX
Setting project property: git.build.host -> XXXXXX
Setting project property: git.dirty -> true
Setting project property: git.remote.origin.url -> Unknown
Setting project property: git.closest.tag.name -> 
Setting project property: sonar.login -> XXXXXX
Setting project property: project.build.sourceEncoding -> UTF-8
Setting project property: sonar.host.url -> XXXXXX
Setting project property: git.total.commit.count -> 1
Setting project property: git.commit.id.describe-short -> be3e227-dirty
Setting project property: git.commit.user.email -> XXXXXX
Setting project property: git.commit.time -> 2019-04-24T08:29:19+0200
Setting project property: git.commit.message.full -> first commit
Setting project property: git.build.version -> 1.0.0
Setting project property: git.commit.message.short -> first commit
Setting project property: git.commit.id.abbrev -> be3e227
Setting project property: git.build.user.name -> XXXXXX
Setting project property: git.branch -> feature/my_feature
Setting project property: sonar.projectVersion -> 1.0.0-${git.branch}
Setting project property: git.closest.tag.commit.count -> 
Setting project property: git.commit.id.describe -> be3e227-dirty
Setting project property: git.commit.id -> XXXXXX
Setting project property: git.tags -> 
Setting project property: git.build.time -> 2019-04-24T08:43:05+0200
Setting project property: git.commit.user.name -> XXXXXX
Setting project property: ant.file -> R:\git-test\pom.xml
[DEBUG] Setting properties with prefix: 
Setting project property: project.groupId -> com
Setting project property: project.artifactId -> git-test-sonarqube
Setting project property: project.name -> git-test-sonarqube
Setting project property: project.version -> 1.0.0
Setting project property: project.packaging -> jar
Setting project property: project.build.directory -> R:\git-test\target
Setting project property: project.build.outputDirectory -> R:\git-test\target\classes
Setting project property: project.build.testOutputDirectory -> R:\git-test\target\test-classes
Setting project property: project.build.sourceDirectory -> R:\git-test\src\main\java
Setting project property: project.build.testSourceDirectory -> R:\git-test\src\test\java
Setting project property: localRepository ->       id: local
      url: file:///C:/Users/Nicola/.m2/repository/
   layout: default
snapshots: [enabled => true, update => always]
 releases: [enabled => true, update => always]

Setting project property: settings.localRepository -> C:\Users\Nicola\.m2\repository
Setting project property: maven.project.dependencies.versions -> 
[INFO] Executing tasks

As far I can see, the property are managed, but the sonar version is not resolved:

Setting project property: git.branch -> feature/my_feature
Setting project property: sonar.projectVersion -> 1.0.0-${git.branch}

Do you know similar cases or possible solutions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions