0

What is the best approach to have my maven package-deploy project write to a properties file inside of a WAR file?

I currently have three separate maven projects that create their own packages:

a.war, b.zip, and c.tar.gz

Inside of the WAR file (a.war), there is a properties file that contains the following:

buildDate=2018-01-25 16:11:49 PST
aUiNumber=2.1.0-SNAPSHOT.5
buildNumber=2.1.0-SNAPSHOT.${deploy.number}

The file is located here (inside of the WAR file):

WEB-INF/classes/a-version.properties

On a Jenkins server, I have a job that uses maven to do the following:

  1. Pull the latest a.war, b.zip, c.tar.gz from nexus
  2. Package these into app-assets.zip
  3. Deploy app-assets.zip

I would like to have this maven job populate the ${deploy.number} in the a-version.properties file with the Jenkins job number. What is the best approach for this? Is there a way to do it without unpacking the WAR file?

I attempted this by adding the a.war/WEB-INF/classes to the <directory> section of the war file. As expected, the build did not fail; however, it did not populate the variable as well:

mvn -U -f ./PackageDeployPom.xml resources:resources -Ddeploy.number=${BUILD_NUMBER}
[INFO] skip non existing resourceDirectory /home/jenkins/app-assets/apache-tomcat-8.0.41/webapps/a.war/WEB-INF/classes
Josafoot
  • 119
  • 1
  • 5
  • What is the purpose of using Jenkins job number if Jenkins only downloads file from Nexus? In this case war file with `buildNumber=2.1.0-SNAPSHOT.1` and `buildNumber=2.1.0-SNAPSHOT.2` will still be the same, isn't it? It makes more sense to generate that value during packaging of war because in this case files with different value of that property might be different. – Ivan Jan 26 '18 at 17:57
  • @Ivan That is true and, believe me, I brought this up as well. It is more for documentation and troubleshooting purposes. The aUiNumber is is populated during the packaging of the war file. In your scenario, while the deploy.number will be different, the aUiNumber will not. The idea of using the jenkins job number is to see which package is currently deployed, not the war, zip or tar file. – Josafoot Jan 26 '18 at 18:15
  • have you tried to unpack war, process resources and package it again? – Ivan Jan 26 '18 at 18:40
  • @Ivan I have not tried this yet. Mostly out of hope to contain this deployment process within maven rather than shell scripts. If there is a way to do this in a single maven command, that would be be excellent. (Unpack-> filter resources -> repack (war) -> package super package. – Josafoot Jan 29 '18 at 18:56

1 Answers1

0

Not sure if this is the best way to do this but here is how I did it:

I used the exec-maven-plugin.

The maven project now follows this procedure:

  1. Pull the latest a.war, b.zip and c.tar from nexus (place in to-be-packaged-directory).
  2. Use exec-maven-plugin to call a bash script. This script will:

    i. Copy the a.war file to a temporary workspace directory, unpack it here.

    ii. Populate the variables in temp-workspace/WEB-INF/classes/a-version.properties. (Using sed).

    iii. Use jar to update the file in the war file.

  3. Package the app-assets.zip

  4. Deploy app-assets.zip

Here is what I added to the pom file for the packaging job:

            <plugin>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <groupId>org.codehaus.mojo</groupId>
                <executions>
                    <execution><!-- For Adding deploy Number -->
                        <id>Renaming build artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>bash</executable>
                            <commandlineArgs>scripts/BuildNumber.sh -b ${deploy.number}</commandlineArgs>
                        </configuration>
                  </execution>
                </executions>
            </plugin>

Here is the working portion of the Bash script that gets executed with by the maven plugin:

#Cleaning the TempWorkspace
TempWs=${Workspace}/a-war-temp;
if [[ ! -d ${TempWs} ]];then
    mkdir ${TempWs};
else
    rm -r ${TempWs}/;
    mkdir ${TempWs};
fi
#--- end cleaning temp workspace ---    

#Copying the war file to the temp workspace
cp ${Workspace}/app-assets/${ApacheTomcat}/webapps/a.war ${TempWs}/a-old.war;


#Unpacking the war file, using sed to make changes to variable(s)
cd ${TempWs}
    jar xvf aw-old.war;
    sed -i "s/\${deploy.number}/${BuildNumber}/g" ${TempWs}/WEB-INF/classes/am-version.properties
cd ${Workspace};     #Going back to the Workspace
#--- end populating variable(s) ---


#Updating the war file with the new properties file
jar -uf ${Workspace}/aw-emr/${ApacheTomcat}/webapps/aw.war -C ${TempWs} WEB-INF/classes/am-version.properties  

Maven is run with this command:

mvn -U -B -Ddeploy.number=${BUILD_NUMBER} -f ./App-Asset-deploy.xml clean package

Josafoot
  • 119
  • 1
  • 5