Skip to content

Avoid Hardcoding Version in pom.xml and Use Git Version Instead #9

@vchendrix

Description

@vchendrix

The pom.xml file currently has the version hardcoded as 2.0.0. This can lead to issues with maintaining and updating the version across different branches and releases. It would be more efficient to dynamically retrieve the version from the git tags.

Proposed Solution

  • Modify the pom.xml configuration to use a plugin that can fetch the version from git tags.
  • This change will ensure that the version is always in sync with the current git tag, reducing the risk of discrepancies.

Implementation Example

To dynamically retrieve the version from git tags in the pom.xml of the NCEAS/osti-elink repository, follow these steps:

  1. Add the Maven Git Versioning Plugin: Modify the pom.xml file to include the Maven Git Versioning Plugin.
  2. Configure the Plugin: Update the pom.xml file to use the plugin for versioning.

Here’s how you can modify the pom.xml file:

Step 1: Add the Plugin

Insert the following configuration into the <build> section of your pom.xml file:

<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>4.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>revision</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <gitPropertiesFile>${project.build.outputDirectory}/git.properties</gitPropertiesFile>
    </configuration>
</plugin>

Step 2: Update the Version Property

Replace the hardcoded version with a property that pulls from the git commit ID:

<properties>
    <revision>${git.commit.id.describe}</revision>
</properties>
<version>${revision}</version>

Example Updated pom.xml

Here’s how the relevant sections of your pom.xml will look after making these changes:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>edu.ucsb.nceas</groupId>
    <artifactId>osti-elink</artifactId>
    <properties>
        <revision>${git.commit.id.describe}</revision>
    </properties>
    <version>${revision}</version>
    <build>
        <plugins>
            <!-- Other plugins -->
            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>4.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                    <gitPropertiesFile>${project.build.outputDirectory}/git.properties</gitPropertiesFile>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Benefits

  • Easier version management.
  • Reduces the chances of human error when updating the version.

References

Now, the pom.xml will dynamically retrieve the version from the git tags, ensuring it is always in sync with the current git tag.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions