Samuel Torga
Samuel Torga

Reputation: 175

Using maven, how to concat final name of war with project version in a specific format?

The functionality of Parallel Deployment from Tomcat requires that the .war file name be in a specific format.

For example: If we have the version 1 of the project running and we need to hot deploy the version 2, it is needed that the .war name be "project-name##2.war".

We use in our project semantic versioning. So our versions are for example 5.31.6 .

When we deploy a new version through Tomcat manager we build the project using maven, rename the file name to project-name##053106.war and upload it to server. This process is manual and susceptible to errors.

Our version is set in pom.xml like this:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>br.com.group</groupId>
    <artifactId>project-name</artifactId>
    <version>5.31.6</version>
    <packaging>war</packaging>
    <name>Project Name</name>
    <description>Project Description</description>
    ...
</project>

We set the final name in profiles, for example:

<profiles>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <profilelabel>LOCAL</profilelabel>
        </properties>
        <build>
            <finalName>project-name-local</finalName>
        </build>
    </profile>
    <profile>
        <id>production</id>
        <properties>
            <profilelabel>PRODUCTION</profilelabel>
        </properties>
        <build>
            <finalName>project-name</finalName>
        </build>
    </profile>
</profiles>

My question is: With the examples above, how can I build the war file with the final name concatenated with version number without the dots and if possible with each "part" of version "str_pad"ed (5.31.6 becomes 053106)?

Upvotes: 0

Views: 996

Answers (1)

Lesiak
Lesiak

Reputation: 25946

You can use gmaven plugin to mangle project.version in any desired way and assign it to a property (all during initialization phase). Here is an example, it handles versions with -SUFFIX part (to be safe, it handles the case of multiple dashes):

<build>
    <finalName>yourProjectName-${paddedVersion}</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>
                            println('Mangling project version: ' + project.version)
                            String[] versionParts = project.version.split("-")
                            paddedVersion = versionParts[0].split("\\.").collect { it.padLeft(2,"0") }.join()
                            if (versionParts.size() > 1) {
                                paddedVersion += "-" + versionParts[1..-1].join("-")
                            }
                            println('Padded version version: ' + paddedVersion)
                            project.properties["paddedVersion"] = paddedVersion
                        </source>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Upvotes: 2

Related Questions