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:
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
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:
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.
Package the app-assets.zip
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