In my last post I decribed the way to build and deploy a war file to Tomcat application server with Ant.
In this post I show how to deploy the build war file to Tomcat application server with Maven.
Create inside the "ant" folder this Maven file:
<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>deringo</groupId>
<artifactId>myApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>myApp</name>
<description>myApp Maven deployment</description>
<properties>
<java.version>1.8</java.version>
<tomcat.version>8.5.33</tomcat.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>${tomcat.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>myApp</warName>
<wtpContextName>myApp</wtpContextName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://myAppServer:7011/manager/text</url>
<username>tomcat</username>
<password>tomcat</password>
<warFile>dist/myApp.war</warFile>
<path>/myApp</path>
<update>true</update>
</configuration>
</plugin>
</plugins>
</build>
</project>
Execute with goal: tomcat7:deploy
Use Properties file
In my ANT file I have used a properties file for tomcat username/password etc. In my Maven script I want also to use this properties file.
Unfortunatly Maven can not handle property files out of the box. But there is a Plugin we can use:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>tomcat.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
This is the same, as writing this directly into pom.xml:
<properties>
<tomcat.username>tomcat</tomcat.username>
<tomcat.password>tomcat</tomcat.password>
</properties>
The complete pom.xml with properties:
<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>deringo</groupId>
<artifactId>myApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>myApp</name>
<description>myApp Maven deployment</description>
<properties>
<java.version>1.8</java.version>
<tomcat.version>8.5.33</tomcat.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>${tomcat.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>tomcat.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>myApp</warName>
<wtpContextName>myApp</wtpContextName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>${tomcat.manager.url}</url>
<username>${tomcat.username}</username>
<password>${tomcat.password}</password>
<warFile>dist/myApp.war</warFile>
<path>${webapp.name}</path>
<update>true</update>
</configuration>
</plugin>
</plugins>
</build>
</project>
Update 2021-03-24
I tried the configuration in a project but got this error:
Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:properties-maven-plugin:1.0-alpha-1:read-project-properties (execution: default, phase: initialize)
I solved the error adding a pluginManagement tag:
<build>
<pluginManagement>
<plugins>
<plugin> ... </plugin>
<plugin> ... </plugin>
....
</plugins>
</pluginManagement>
</build>
But this solution seems more like a workaround when reading the discussion on stackoverflow.
...need to search further...
Use in Jenkins
I have not tried it, but it should be possible to set an environment variable in Jenkins build step to execute with a configurable filename.
For example TOMCAT_PROPERTIES=jenkins-home/secret/tomcat-dev.properties.
And we can use it in Maven script this way:
<configuration>
<files>
<file>${env.TOMCAT_PROPERTIES}</file>
</files>
</configuration>