I wish to access the build information from inside my java project which uses gradle to build the project. The information I need to access is the build number generated by teamcity, build vcs number etc. These are easily accessed by maven and using the maven-replacer-plugin these can be replaced in a properties file. Firstly, is there an alternative in gradle to achieve the same? And then, how to do it? :)
I tried the approach shared here, but I always get null as the build number.
In maven I placed a properties file (project.properties) in the project to hold key value pairs with keys being those project information like
project.groupId, project.artifactId, project.version, maven.build.timestamp
I wish to access the same build information using gradle. . The pom would look like below:
<!-- Write project version number to file -->
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.3.8</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<!-- replace the token in this file -->
<include>target/classes/project.properties</include>
</includes>
<replacements>
<replacement>
<token>PROJECT_GROUP</token>
<value>${project.groupId}</value>
</replacement>
<replacement>
<token>PROJECT_ARTIFACT</token>
<value>${project.artifactId}</value>
</replacement>
<replacement>
<token>PROJECT_VERSION</token>
<value>${project.version}</value>
</replacement>
<replacement>
<token>PROJECT_BUILD_DATE</token>
<value>${maven.build.timestamp}</value>
</replacement>
<replacement>
<token>BUILD_NUMBER</token>
<value>${build.number}</value>
</replacement>
</replacements>
</configuration>
</plugin>
What the maven replacer plugin does is replace the values in the properties object, that can be used in the java application.
I need to access the same properties using gradle.
Team City configuration in the pom is below:
<ciManagement>
<system>Team City</system>
<url>http://teamcity.mycompany.com/teamcity/project.html?projectId=bt007
</url>
</ciManagement>
In the build.gradle
file, I have a task defined as below:
task getProjectGroup << {
ext.projectGroup = System.getProperty("project.group")
println "Project Group: $projectGroup"
}
I get the following output when executing this task:
:service:getProjectGroup
Project Group: null
BUILD SUCCESSFUL