Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 422
res Avatar asked May 09 '18 14:05

res


People also ask

What is ${} in Pom?

build. directory . It is the value inside the <project><build><directory> element of the POM. You can get a description of all those values by looking at the Maven model. For project.

How do I view environment variables in Pom?

To refer to environment variables from the pom. xml, we can use the ${env. VARIABLE_NAME} syntax. We should remember to pass the Java version information via environment variables.

What is effective POM in STS?

Effective POM combines all the default settings from the super POM file and the configuration defined in our application POM. Maven uses default values for configuration elements when they are not overridden in the application pom.xml.

What is POM in xml?

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects.


2 Answers

you can use org.codehaus.groovy.maven plugin:

   <plugin>
        <groupId>org.codehaus.groovy.maven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <executions>
          <execution>
            <phase>generate-sources</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <source>                  
System.setProperty("version2","${version}".replace('b_04_', ''))
              </source>
            </configuration>
          </execution>
        </executions>
      </plugin>

and now you can use version2 :

version=${version2}

this is an exemple in GitHub

like image 148
Azzabi Haythem Avatar answered Sep 21 '22 21:09

Azzabi Haythem


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.

like image 30
res Avatar answered Sep 17 '22 21:09

res