Search code examples
javamavenbuildversionpom.xml

substring a variable in pom.xml


Is it possible (and how) to substring a variable in the pom.xml, or the properties that uses this variable?

My scenario:

I have a swing application that shows a kind of version in its footer. This version is read from a properties file. The properties file only have a reference for a maven variable, as:

version=${git.branch}

In my pom.xml, a have a plugin that looks for the branch name, and write it in the "git.branch" variable.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>pl.project13.maven</groupId>
            <artifactId>git-commit-id-plugin</artifactId>
            <version>2.2.2</version>
            <configuration>
                <dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
                <injectAllReactorProjects>true</injectAllReactorProjects>
            </configuration>
            <executions>
                <execution>
                    <id>get-the-git-infos</id>
                    <goals>
                        <goal>revision</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/version.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/version.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>

But now we are using a prefix for the branch names, and this prefix shouldn't be deployed with the application version.

I had branches like:

v123
v124
v125

Now I have branches like:

b_04_v123
b_04_v124

And i want maven to get only the value "v124" of the string "b_04_v123", the prefix is fixed, aways like "b_NN_".

NOTE: No, it's not possible to change the branch names, as other departments uses them with scripts and automation.


Solution

  • I solved it inside the java code, I already had a code reading the properties file, just added the substring there. It isn't a beautiful maven solution, but worked. I could have done this since the beginning. But, if someone could tell how can I substring a variable in my pom.xml it would be great, although I don't need it anymore (or with the same urgency) I'm still curious.