jenkins使用
说明:jenkins是一个能实现项目自动化部署管理的工具
准备工作
下载软件:https://jenkins.io/download/ 下载war包就可以了,在不同的平台上都可以通用
安装git、maven工具
git1.7.1在和Jenkins使用的时候会出现问题,这里建议是用1.7.1以上的 由于centos6自带的git就是1.7.1的,所以我是编译安装的,安装步骤如下:
1)安装依赖工具
<code>yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel</code>
2)下载源代码,我用的是2.7.4 https://mirrors.edge.kernel.org/pub/software/scm/git/
<code>cd git-2.7.4 make prefix=/usr/local/git all make prefix=/usr/local/git install vim /etc/profile // 在末尾新开一行填写下面的代码 export PATH=$PATH:/usr/local/git/bin // :wq保存退出,然后执行下面的命令,让其生效 source /etc/profile // 查看git版本 git --version</code>
maven安装 http://maven.apache.org/download.cgi 我下载最新版3.5.3 将文件放在/usr/local/下 配置环境变量:
<code>MAVEN_HOME=/usr/local/apache-maven-3.5.3 export MAVEN_HOME export PATH=${MAVEN_HOME}/bin</code>
运行jenkins并创建项目
启动:在服务器上运行: nohup java -jar jenkins.war &
控制台会打印出初始化的用户名和密码 至此就在浏览器打开页面,输入账号就可以登录到管理界面,jenkins会自动扫描服务器上的安装的maven、git服务
新建一个项目:
选择项目源码位置,这里使用gitlab来管理源码,下面是登陆到gitlab的账号
因为我使用的是maven项目,填写maven构建的执行命令,我这里是打包成一个war包
添加构建后的操作,比如上传到war包到服务器,然后备份原来的项目,重启tomcat,这里面需要配合shell脚本来完成
shell脚本,如下:基本的逻辑是先将原来的项目压缩,移动到备份目录,然后将修改后的项目上传到服务器对应的目录,并重启tomcat
<code>WORK_DIR=`pwd` TOMCAT_DIR=$WORK_DIR/tomcat-dynamic-api NOW_TIME=`date +%Y%m%d%H%M%S` tar -jcvf $TOMCAT_DIR/webapps/dynamicAPI.tar.$NOW_TIME $TOMCAT_DIR/webapps/dynamicAPI mv $TOMCAT_DIR/webapps/dynamicAPI.tar.$NOW_TIME $TOMCAT_DIR/backup/ mv $TOMCAT_DIR/backup/dynamicAPI.war $TOMCAT_DIR/webapps/ rm -rf $TOMCAT_DIR/webapps/dynamicAPI kill -9 `ps aux | grep tomcat-dynamic-api | grep -v grep | awk '{print $2}'` && $TOMCAT_DIR/bin/startup.sh</code>
maven配置
<profiles> <profile> <id>local</id> <properties> <env>local</env> </properties> </profile> <profile> <id>product156</id> <!-- <activation> <activeByDefault>true</activeByDefault> </activation> --> <properties> <env>product_156</env> </properties> </profile> <profile> <id>product169</id> <properties> <env>product_169</env> </properties> </profile> <profile> <id>backup148</id> <properties> <env>backup_148</env> </properties> <build> <resources></resources> </build> </profile> <profile> <id>test157</id> <properties> <env>test_157</env> </properties> </profile> <profile> <id>test169</id> <properties> <env>test_169</env> </properties> </profile> </profiles> <!-- 配置文件 --> <build> <finalName>dynamicAPI</finalName> <extensions> <extension> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-ssh</artifactId> <version>2.8</version> </extension> </extensions> <resources> <resource> <directory>src/main/resources/${env}</directory> </resource> </resources> <testSourceDirectory>src/test/java</testSourceDirectory> <testResources> <testResource> <directory>src/test/resources</directory> </testResource> </testResources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webResources> <resource> <directory>WebContent</directory> </resource> <resource> <directory>libs/</directory> <targetPath>WEB-INF/lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </webResources> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>utf-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> <configuration> <skipTests>true</skipTests> <junitArtifactName>junit:junit</junitArtifactName> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <excludes> <exclude>product_156/**</exclude> <exclude>product_169/**</exclude> <exclude>test_157/**</exclude> <exclude>*.properties</exclude> <exclude>*.xml</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.1</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins> </build>