仅使用Jenkins的Tomcat WAR部署:
这是部署的场景:
3环境生产,预生产和资格认定
2个战争文件,例如app1和app2
每个环境2个Tomcat服务器,例如Server1和Server2(第1点)
战争文件介绍:
开发人员提供战争文件,例如app1 ## 20171122.war和app2 ## 20171122.war
app1和app2设置文件不是战争的一部分。
每种环境(生产,预生产和资格认证)的设置文件都不相同
部署app1和app2之后,我们将设置文件放在webapps / app1 ## 20171122 / config / Application.settings和webapps / app2 ## 20171122 / config / Application.settings
最后,app1和app2开始使用tomcat管理器
这就是我要对詹金斯做的事情:
使用Mavin部署或任何其他工具:
注意:此处War文件的大小超过450MB
Jenkin Project App1:
我们将把app1 ## 20171122.war文件放在Jenkins服务器上
我们应该能够自动选择要部署的war文件以及war版本
我们应该能够在多个服务器(Server1和Server2)上部署War文件
在webapps / app1 ## 20171122 / config / Application.settings下部署app1的设置文件
启动app1
注意:应用程序尚未准备好从Webapp外部读取配置
简而言之:
(Jenkins服务器:App1 war文件及其设置文件)==(在远程VM上部署)==>(Tomcat服务器)
(Jenkins服务器:自动选择App1 war)==(在远程VM上部署)==>(Tomcat服务器)
(Jenkins服务器:App1设置文件)==(远程VM上的部署设置)==>(Tomcat服务器)
(Jenkins服务器:启动App1命令)==(将命令发送到远程VM)==>(Tomcat服务器)
最佳答案
我创建了以下方法来解决此问题:
Maven:
安装了Maven并按照pom.xml创建
创建了一个小shell脚本来解压war,将与特定环境相关的设置文件放入其中,然后再次创建war文件
詹金斯:
将Jenkins文件系统列表参数插件与“此项目已参数化”选项一起使用。
从“构建选项”中使用了“调用顶级Maven目标”,并使用了以下maven命令
配置文件系统参数时,请使用以下“ WAR-NAME”
部署-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 .