版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cdnight/article/details/81219795
前言
在打通微服务模块的构建+自动化部署以后,怎么能少了java web的构建及自动化部署?
还是那一句,这篇文章需要参考之前的文章,如果不看的话估计是没办法解决下去的。
前提
假定已经有一个正常的web项目,已经可以用maven正常构建,此为前提。
备注,maven的构建是根据pom文件来的,pom文件是很重要,这里贴出pom文件,其中用到了war等等插件,也用了根据不同环境来复制文件的插件:
<?xml version="1.0" encoding="UTF-8"?>
<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>net.w2p</groupId>
<artifactId>MicroWeb</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<!--设置配置环境profile begin -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<package.environment>dev</package.environment>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<package.environment>test</package.environment>
</properties>
</profile>
<profile>
<id>product</id>
<properties>
<package.environment>product</package.environment>
</properties>
</profile>
</profiles>
<!--设置配置环境profile bend -->
<!--好了,是不是经常发现pom文件一加一点东西idea的编译环境就会自动变化1.5版本的jdk??加上这个强行指定编译版本就没问题了。-->
<build>
<!--名字统一一下-->
<finalName>${project.artifactId}</finalName>
<!-- 主资源目录 -->
<resources>
<resource>
<!-- 设置主资源目录 -->
<directory>src/main/resources</directory>
<!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,是否对主资源目录开启资源过滤 -->
<filtering>true</filtering>
<!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,只处理如下配置中包含的资源类型 -->
<!-- 包括文件 -->
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,不处理如下配置中包含的资源类型(剔除下如下配置中包含的资源类型)-->
<!-- 排除文件,这些文件打包的时候应该被排除 -->
<excludes>
<exclude>profiles/dev/*.properties</exclude>
<exclude>profiles/test/*.properties</exclude>
<exclude>profiles/product/*.properties</exclude>
<exclude>conf/*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!--打包替换-->
<!--打包文件替换end-->
<!--war 打包 begin -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<!--
Exclude JCL and LOG4J since all logging should go through SLF4J.
Note that we're excluding log4j-<version>.jar but keeping
log4j-over-slf4j-<version>.jar
-->
<webXml>web\WEB-INF\web.xml</webXml>
<!--指定jsp、js、css的路劲 -->
<warSourceDirectory>web</warSourceDirectory>
<packagingExcludes>
<!--WEB-INF/lib/commons-logging-*.jar,-->
<!--%regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]-->
</packagingExcludes>
<webResources>
<resource>
<directory>src/main/resources/profiles/${package.environment}</directory>
<targetPath>WEB-INF/classes/conf</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<!--war 打包 end -->
</plugins>
</build>
<dependencies>
<!--基础类库-->
<dependency>
<!--Group net.funfunle-->
<!--Name baselib-->
<groupId>net.funfunle</groupId>
<artifactId>baselib</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!--app项目对应api项目 begin -->
<dependency>
<groupId>net.w2p</groupId>
<artifactId>MicroBaseApi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--app项目对应api项目 end-->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Servlet web -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--七牛 begin -->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.2.0, 7.2.99]</version>
</dependency>
<!--七牛 end -->
<!--apache commons 类库 begin -->
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!--apache commons 类库 end -->
</dependencies>
</project>
准备前
请明确相关参数。
1、在需要部署web网站的机器上必须存在该文件。
/root/deploy-war.sh
可执行,文件模板如下:【文件内容是固定您的】
#!/bin/bash
echo "==========================="
echo "脚本说明:"
echo "当前脚本为自动化java-web的war程序,可用于:"
echo "1、java web 程序部署,具体过程为,自动关停tomcat,自动复制解压缩war文件到指定目录,自动重启tomcat"
echo "==========================="
echo "==========================="
echo "需求参数说明:"
echo "参数-f、构建文件路径,譬如:/data/packages/test/MicroWeb/MicroWeb-Build-0.0.2.war"
echo "参数-t、解压的目标目录路径,譬如:/data/web-root/test/MicroWeb/"
echo '参数-s、tomcat所在目录,譬如:/usr/local/tomcat'
echo "==========================="
echo "==========================="
echo "正在执行:$0"
echo "==========================="
echo "==========================="
echo "脚本执行情况:"
echo "用户输入参数个数:$#"
echo "输入参数列表:$@"
echo "==========================="
###参数设定
#从外界传进来的参数获取工程产出物的具体路径
build_war_file=""
target_path=""
tomcat_home="";
jar_file_name=""
#获取参数:
while getopts :f:t:s:j: OPTION;do
case $OPTION in
f)
build_war_file=$OPTARG
;;
t)
target_path=$OPTARG
;;
s)
tomcat_home=$OPTARG
;;
j)
jar_file_name=$OPTARG
;;
?)
;;
esac
done
echo "build_war_file is:$build_war_file"
####判断产出物是否存在。
if [ -e $build_war_file ]
then
echo "工程打包文件存在,继续执行"
else
echo "工程打包文件不存在,无法继续执行!"
exit 1
fi
if [ -f $build_war_file ]
then
echo ""
else
echo "您指定的产出物为目录,系统无法继续执行!"
exit 1
fi
###检查输出目录是否存在,不存在则新建目录。
if [ ! $target_path ]
then
echo "输出目录为空,系统无法继续执行!"
exit 1
fi
if [ ! -e $target_path ]
then
mkdir -p $target_path
echo "${target_path}不存在,新建成功"
else
if [ -f $target_path ]
then
echo "$target_path 为文件,系统无法处理!"
exit 1
fi
fi
has_tomcat=0
####检查tomcat 目录是否存在。
if [ -z "$tomcat_home" ]
then
echo "tomcat 目录(-s)参数为空系统不执行tomcat相关操作!"
else
has_tomcat=1
fi
##假设有tomcat目录,那么就判断一下。
if [ $has_tomcat -eq 1 ]
then
if [ ! -e $tomcat_home ]
then
echo "${tomcat_home}不存在,系统暂停"
exit
else
if [ -f $tomcat_home ]
then
echo "$tomcat_home 为文件,系统无法处理!"
exit 1
fi
fi
fi
#######有tomcat的话,请先关停服务。
if [ $has_tomcat -eq 1 ]
then
echo "....稍等,正在关停tomcat"
sh "${tomcat_home}/bin/shutdown.sh"
fi
####部署开始。
###删除之前所有文件。
for FILE in $(ls $target_path)
do
echo "删除:$FILE"
rm -rf "${target_path}/${FILE}"
done
###解压缩:
unzip $build_war_file -d $target_path
#######有系统服务的话,部署完了就启动系统服务吧。
if [ $has_tomcat -eq 1 ]
then
echo "....正在重启服务器..."
sh "${tomcat_home}/bin/startup.sh"
fi
配置步骤0、tomcat配置
运行项目要有web服务器啊,没有的话就参考这一篇来配置。
配置步骤1、自动脚本部署
1、vim /root/deploy-war.sh
2、然后复制下面的脚本内容,保存。
3、给予运行权限。
chmod +x /root/deploy-war.sh
#!/bin/bash
echo "==========================="
echo "脚本说明:"
echo "当前脚本为自动化java-web的war程序,可用于:"
echo "1、java web 程序部署,具体过程为,自动关停tomcat,自动复制解压缩war文件到指定目录,自动重启tomcat"
echo "==========================="
echo "==========================="
echo "需求参数说明:"
echo "参数-f、构建文件路径,譬如:/data/packages/test/MicroWeb/MicroWeb-Build-0.0.2.war"
echo "参数-t、解压的目标目录路径,譬如:/data/web-root/test/MicroWeb/"
echo '参数-s、tomcat所在目录,譬如:/usr/local/tomcat'
echo "==========================="
echo "==========================="
echo "正在执行:$0"
echo "==========================="
echo "==========================="
echo "脚本执行情况:"
echo "用户输入参数个数:$#"
echo "输入参数列表:$@"
echo "==========================="
###参数设定
#从外界传进来的参数获取工程产出物的具体路径
build_war_file=""
target_path=""
tomcat_home="";
jar_file_name=""
#获取参数:
while getopts :f:t:s:j: OPTION;do
case $OPTION in
f)
build_war_file=$OPTARG
;;
t)
target_path=$OPTARG
;;
s)
tomcat_home=$OPTARG
;;
j)
jar_file_name=$OPTARG
;;
?)
;;
esac
done
echo "build_war_file is:$build_war_file"
####判断产出物是否存在。
if [ -e $build_war_file ]
then
echo "工程打包文件存在,继续执行"
else
echo "工程打包文件不存在,无法继续执行!"
exit 1
fi
if [ -f $build_war_file ]
then
echo ""
else
echo "您指定的产出物为目录,系统无法继续执行!"
exit 1
fi
###检查输出目录是否存在,不存在则新建目录。
if [ ! $target_path ]
then
echo "输出目录为空,系统无法继续执行!"
exit 1
fi
if [ ! -e $target_path ]
then
mkdir -p $target_path
echo "${target_path}不存在,新建成功"
else
if [ -f $target_path ]
then
echo "$target_path 为文件,系统无法处理!"
exit 1
fi
fi
has_tomcat=0
####检查tomcat 目录是否存在。
if [ -z "$tomcat_home" ]
then
echo "tomcat 目录(-s)参数为空系统不执行tomcat相关操作!"
else
has_tomcat=1
fi
##假设有tomcat目录,那么就判断一下。
if [ $has_tomcat -eq 1 ]
then
if [ ! -e $tomcat_home ]
then
echo "${tomcat_home}不存在,系统暂停"
exit
else
if [ -f $tomcat_home ]
then
echo "$tomcat_home 为文件,系统无法处理!"
exit 1
fi
fi
fi
#######有tomcat的话,请先关停服务。
if [ $has_tomcat -eq 1 ]
then
echo "....稍等,正在关停tomcat"
sh "${tomcat_home}/bin/shutdown.sh"
fi
####部署开始。
###删除之前所有文件。
for FILE in $(ls $target_path)
do
echo "删除:$FILE"
rm -rf "${target_path}/${FILE}"
done
###解压缩:
unzip $build_war_file -d $target_path
#######有系统服务的话,部署完了就启动系统服务吧。
if [ $has_tomcat -eq 1 ]
then
echo "....正在重启服务器..."
sh "${tomcat_home}/bin/startup.sh"
fi
配置步骤2、jenkins配置
在部署jenkins的同时,请在目标机器上面顺便新建必要的目录。
必要的目录及解释:
##jenkins成功构建后的war文件,请根据实际情况进行填写。
/var/lib/jenkins/workspace/MicroWeb/MicroWeb/target/MicroWeb.war
##将war远程复制到目标机器的目标路径。其中war的名字跟随版本而变。
/data/packages/test/MicroWeb/MicroWeb-Build-${REVISION}.war
##目标机器上面的网站目录。
/data/web-root/test/MicroWeb/
##目标机器的tomcat目录
/usr/local/tomcat
首先,远程登录到目标机器,新建对应的目录:
mkdir -p /data/packages/test/MicroWeb/
mkdir -p /data/web-root/test/MicroWeb/
然后在jenkins上添加一个pipeline任务,叫MicroWeb,
脚本如下:
#!/usr/bin/env groovy
pipeline{
agent any
environment {
REVISION = "0.0.${env.BUILD_ID}"
}
options{
disableConcurrentBuilds()
skipDefaultCheckout()
timeout(time: 1, unit: 'HOURS')
timestamps()
}
parameters{
choice(name: 'build_env', choices: 'dev\ntest\nproduct\n', description: '请选择构建的环境')
}
stages{
stage ('Initialize') {
steps {
script {
currentBuild.displayName = "${REVISION}"
}
sh '''
echo "任务初始化..."
echo "构建版本revision:${REVISION}"
'''
sh '''
echo "项目检出...."
'''
checkout([$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [[credentialsId: 'e522721e-4a9a-467c-b154-acb803d8afb0',
depthOption: 'infinity',
ignoreExternalsOption: true,
remote: 'svn://127.0.0.1:3690/MicroWeb']],
workspaceUpdater: [$class: 'UpdateUpdater']])
}
}
stage ('Build') {
steps {
echo "您选择了:${params.build_env}"
echo '构建阶段....'
echo '构建打包MicroWeb项目...'
sh "mvn clean package -e -f MicroWeb/pom.xml -P ${params.build_env}"
}
}
stage ('Deploy') {
steps {
echo '发布阶段....'
script
{
if (params.build_env == 'test') {
echo '=====》》》测试环境进行远程发布。'
sh '''
sudo su -s /bin/bash jenkins
scp /var/lib/jenkins/workspace/MicroWeb/MicroWeb/target/MicroWeb.war root@目标机器ip:/data/packages/test/MicroWeb/MicroWeb-Build-${REVISION}.war
'''
echo "远程发布包含文件的复制及解压缩,假设您尚未配置好服务器上面的目录环境以及系统服务,请手动ssh登录进行配置。"
echo "1、要手动先解压缩网站项目的,请运行:/root/deploy-war.sh -f /data/packages/test/MicroWeb/MicroWeb-Build-${REVISION}.war -t /data/web-root/test/MicroWeb/ "
echo "2、要自动部署且自动关停重启服务器的,请运行:/root/deploy-war.sh -f /data/packages/test/MicroWeb/MicroWeb-Build-${REVISION}.war -t /data/web-root/test/MicroWeb/ -s /usr/local/tomcat"
sh '''
ssh -t -p 22 root@目标机器ip "/root/deploy-war.sh -f /data/packages/test/MicroWeb/MicroWeb-Build-${REVISION}.war -t /data/web-root/test/MicroWeb/ -s /usr/local/tomcat"
'''
}
else if(params.build_env=='product'){
echo '=====》》》生产环境进行远程发布。'
if(1==1){
echo "抱歉,生产环境尚未搭建完毕,不支持自动构建,测试环境可以一试。"
}
else{
}
}
else {
echo '=====》》》 默认(开发)环境进行远程发布。'
if(1==1){
echo "抱歉,dev环境不应在部署到线上,不支持自动构建,测试环境可以一试。"
}
else{
}
}
}
}
}
}
post {
always {
echo '构建结束...'
}
success {
echo '恭喜您,构建成功!!!'
}
failure {
echo '抱歉,构建失败!!!'
}
unstable {
echo '该任务已经被标记为不稳定任务....'
}
changed {
echo ''
}
}
}
然后,build这个任务。
可以看到:
好了,构建及部署完成了,然后,我们要配置一下tomcat,添加一个不同端口的网站项目,再在控制台开放端口。
tomcat添加网站项目
查看一下解压缩的网站目录
好了,
/data/web-root/test/MicroWeb/
这个路径就是网站的根目录了。
打开tomcat的server.xml
vim /usr/local/tomcat/conf/server.xml
看到:
看到service节点没有?
我们再添加一个新的service节点来添加网站,
假设添加的是这个:
端口,网站目录都要自行配置的。。。。
<!--添加micro-web网站-->
<Service name="micro-web">
<!--保证service的name属性值不同-->
<Connector connectionTimeout="20000" port="8500" protocol="HTTP/1.1" redirectPort="8443"/>
<!--修改port为你想要使用的端口,注意不能被占用-->
<!--<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>-->
<Engine defaultHost="localhost" name="micro-web">
<!--保证service中Engine节点的name值不同-->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host appBase="webapps_second" autoDeploy="true" name="localhost" unpackWARs="true"> <!--在Tomcat的根目录下新建一个webapps_second文件夹存放war包-->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t %r %s %b" prefix="localhost_access_log" suffix=".txt"/>
<Context path="" docBase="/data/web-root/test/MicroWeb" debug="0" reloadable="false" />
<!--这句话可以使该端口下项目中的某个页面为访问域名的首个页面,docBase为项目名-->
</Host>
</Engine>
</Service>
<!--添加micro-web网站 end-->
然后重启服务器,阿里云安全规则放开8500的访问权限,
然后访问一下。
经过几天的调试终于运行起来了,感动。。。
界面也出来了。。。
结论
成。。。功。。。