Jenkins+maven+svn构建项目,及分区部署war包到tomcat
一.Jenkins+maven+svn构建项目与部署到tomcat上。
1.在官网下载Jenkins的war包,https://jenkins.io/download/
2.将war包扔到tomcat上编译运行
3.浏览器输入localhost:8080/jenkins/进入主页。
4.新建任务,构建maven项目,如果没有构建maven项目选项,点击系统管理-管理插件安装Maven Integration plugin插件
5.点击下面的ok按钮以后会跳到配置页面,因为使用的是从svn获取项目,所以在源码管理中选择Subversion 并在Repository URL填写上你的svn中的项目的地址
6.可定时打包部署
7.然后填写pom.xml文件位置,和maven命令。
8.下载插件Deploy to container Plugin,填写配置
apache-tomcat-7.0.70\conf\tomcat-users.xml可设置权限,账户密码
如:
<role rolename="tomcat"/>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<role rolename="manager"/>
<user username="tomcat" password="tomcat" roles="tomcat,manager-script,manager-gui,manager"/>
9.保存回到主页面,完成一键打包发布
会先在.jenkins\workspace进行编译打包,然后部署到tomcat上。
二.分区打包。
基本思路是配置pom.xml,使用profile标签,在打包时输入mvn package -P地区名来激活不同的文件属性,来更改打包后配置文件里的内容以及排除不需要打包的文件。
例如一个项目中要分山东和上海两个地区打包,项目目录为:
其中city.properties的配置内容是
1.Pom文件配置内容:
<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>com</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<profiles>
<profile>
<id>shanghai</id>
<properties>
<!-- 激活cityName属性为shanghai -->
<cityName>shanghai</cityName>
<!-- 写地区为上海时要排除的文件路径 -->
<delFileName>shandong1.html,WEB-INF/shandong2.html,WEB-INF/classes/sandong.xml,WEB-INF/classes/contoller/ShandongController.class</delFileName>
</properties>
</profile>
<profile>
<id>shandong</id>
<properties>
<!-- 激活cityName属性为shandong -->
<cityName>shandong</cityName>
<!-- 写地区为山东时要排除的文件路径 -->
<delFileName>shanghai1.html,WEB-INF/shanghai2.html,WEB-INF/classes/shanghai.xml,WEB-INF/classes/contoller/ShanghaiController.class</delFileName>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<!-- warSourceExcludes是在编译周期进行完成后从src/main/webapp目录复制文件时忽略 -->
<!--<warSourceExcludes>hi.html</warSourceExcludes> -->
<!-- packagingExcludes是在复制webapp目录完成后打包时忽略target/ 文件夹的文件 -->
<packagingExcludes>${delFileName}</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.在Jekins中创建两个地区的项目
3.
在pom下写不同的命令
然后经可以在tomcat的webapp下看到只有shandong文件的包了
属性文件也改成了指定的值
三.在对fxbd项目打包部署时遇到的一些bug。
1.由于项目不符合maven规范,需要在pom文件指定路径,否则打包时会少文件。
配置为:
<build>
<sourceDirectory>src/</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/java/resources</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webXml>${basedir}/webapp/WEB-INF/web.xml</webXml>
<warSourceDirectory>webapp</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
2.程序包xxx不存在,找不到符号
使用mvn命令打包时发现程序包xxx不存在,找不到符号。发现是因为maven编译时只加载maven库中的jar包,而手动添加到lib下的包maven无法自动加载。
解决办法:
在pom.xml文件中添加配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${basedir}/webapp/WEB-INF/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
3.报错Error assembling WAR:webxml attribute is required
maven的web项目默认的webroot是在src\main\webapp。如果在此目录下找不到web.xml就抛出以上的异常。
解决办法:需要在pom.xml中增加配置,如下:
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
4.maven打包时 junit.framework不存在
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
去掉test