pankaj ghadge
pankaj ghadge

Reputation: 945

Only Tomcat WAR deployment using Jenkins

Only Tomcat WAR deployment using Jenkins:

Here is the scinario for deployment:

War files scinario:

Note: Application is not yet ready to read configuration from outside of webapps

In short:
(Jenkins Server: App1 war files & their setting files) ==(deploy on remote VM)==>(Tomcat server)

(Jenkins Server: Auto select App1 war) ==(deploy on remote VM)==>(Tomcat server)

(Jenkins Server: App1 Setting file ) ==(deploy setting on remote VM)==>(Tomcat server)

(Jenkins Server: Start App1 command ) ==(Send Command to remote VM)==>(Tomcat server)

Upvotes: 1

Views: 1926

Answers (3)

pankaj ghadge
pankaj ghadge

Reputation: 945

I have created following things to solve this:

Maven:

  1. Installed Maven and created following pom.xml
  2. Created one small shell script to unzip war, put the setting file related to specific environment and again created war file

Jenkins:

  1. Used the Jenkins Filesystem List Parameter Plug-in with "This project is parameterized" option.
  2. Used "Invoke top level Maven targets" from Build Option and used the following maven command
  3. Please use following "WAR-NAME" while configuring File system parameter

    deploy -Papp1 -Papp2 -DwarFile=${WAR-NAME}

Pom.xml:

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.app1.app</groupId>
<artifactId>app1</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
    <warFile>none</warFile>
</properties>
<profiles>
    <profile>
        <id>app1</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                        <execution>
                            <id>regex-property</id>
                            <goals>
                                <goal>regex-property</goal>
                            </goals>
                            <configuration>
                                <name>warFilename</name>
                                <value>${warFile}</value>
                                <regex>.war$</regex>
                                <replacement></replacement>
                                <failIfNoMatch>false</failIfNoMatch>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.6.0</version>
                    <executions>
                        <execution>
                            <id> unzip, put setting file and zip again </id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <commandlineArgs>${warFile}</commandlineArgs>
                        <executable>${basedir}/make_war.sh</executable>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <executions>
                        <execution>
                            <id>deploy-app1-1</id>
                            <goals>
                                <goal>deploy-only</goal>
                            </goals>
                            <phase>deploy</phase>
                            <configuration>
                                <username>admin</username>
                                <password>adminpass</password>
                                <warFile>${basedir}/deploy/${warFile}</warFile>
                                <url>http://localhost:9090/manager/text</url>
                                <server>Tomcat</server>
                                <path>/app1</path>
                                <update>true</update>
                                <ignorePackaging>true</ignorePackaging>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>app2</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <executions>
                        <execution>
                            <id>deploy-app1-2</id>
                            <goals>
                                <goal>deploy-only</goal>
                            </goals>
                            <phase>deploy</phase>
                            <configuration>
                                <username>admin</username>
                                <password>adminpass</password>
                                <warFile>${basedir}/deploy/${warFile}</warFile>
                                <url>http://localhost:8080/manager/text</url>
                                <server>Tomcat</server>
                                <path>/app1</path>
                                <update>true</update>
                                <ignorePackaging>true</ignorePackaging>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

make_war.sh

#!/usr/bin/sh
if [ $# -lt 1 ]; then
  echo 1>&2 "$0: not enough arguments. Please pass the WAR file name !!!"
  exit 2
elif [ $# -gt 1 ]; then
  echo 1>&2 "$0: too many arguments. Please only pass the WAR file name !!!"
  exit 2
fi

war_file_name=$1
file_name=$(basename "$war_file_name")
extension="${war_file_name##*.}"
file_name="${file_name%.*}"

if [ $extension != "war" ]; then
  echo 1>&2 "$0: Invalid arguments. Please pass valid WAR file with version name !!!"
  exit 2
fi

echo "Cleaning up the deploy folder ..."
/usr/bin/rm -rf ./deploy/*
/usr/bin/unzip $war_file_name -d ./deploy/$file_name
/usr/bin/cp -f Application.settings ./deploy/$file_name/config/Application.settings
/usr/local/java/latest/bin/jar -cvf ./deploy/$war_file_name -C ./deploy/$file_name .

Upvotes: 1

saurabh14292
saurabh14292

Reputation: 1401

Here is what you can do :

  • Configure "target" servers through ssh in jenkins.
  • Take parameters as input; what target, what .war, what .settings file etc
  • Copy .war and .settings file to target server over SCP/SSH through jenkins along with a script which does the following.
  • Explode .war (do unzip)
  • Copy required .settings files inside exploded war
  • Start tomcat

I had built .war using Jenkins as a build job and a downstream job was used for deployment on a tomcat based environment whose details were picked up as a build parameter.

Developer gives war files like app1##20171122.war and app2##20171122.war

You haven't mentioned how developer "gives" you these war files. If they are "built", I suggest create a job to build these war using ant/maven etc so that locations where these wars reside is already known. If they are to be provided, you can upload them to any centralized location (FTP server) or over some artifactory such as JFrog/Nexu and download directly on tomcat server.

Upvotes: 0

Jarvis Cochrane
Jarvis Cochrane

Reputation: 101

Are you asking how to get Jenkins to deploy your applications and their configuration to your Tomcat instances?

I think using the Maven deploy plugin might be your best bet to physically deploy the files.

What I've done when I needed to deploy multiple copies/versions of an application to multiple Tomcat servers is to create a new directory wars to contain the .war files, and use Tomcat Context Containers to configure the individual application instances. Like this:

$CATALINA_HOME/conf/Catalina/localhost/app1.xml
$CATALINA_HOME/conf/Catalina/localhost/app2.xml
$CATALINA_HOME/wars/app1-1.0.0.war
$CATALINA_HOME/wars/app1-1.0.1.war
$CATALINA_HOME/wars/app2-1.0.1.war

When you deploy a .war file to ../webapps, Tomcat treats each .war as a separate application. I presume you can provide additional configuration for these apps, but I don't recall how.

But, if you use Context Container files, multiple application instances can use the same .war file. You can keep multiple revisions of the .war file on hand in case you need to roll back to an earlier version, or want to keep running an old version.

Upvotes: 0

Related Questions