maven 多模块打包部署 maven多模块工程打包部署的方法步骤
xb_smart 人气:0想了解maven多模块工程打包部署的方法步骤的相关内容吗,xb_smart在本文为您仔细讲解maven 多模块打包部署的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:maven多模块项目部署,maven,多模块打包部署,maven多模块项目打包,下面大家一起来学习吧。
一般maven多模块工程结构如下图,图中分为dao数据层和上层web层(当然还可以有service层),在进行多模块划分的时候,一般将dao层采用jar进行打包,web层进行war打包。在进行war包部署时,发现dao是以jar包形式存在于lib包目录下,如果在部署服务器上需要进行相关配置修改会比较麻烦。因此研究了下用maven进行合并打包的方法:
1.确保dao pom.xml中有以下配置
<resources> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> <resource> <directory>${basedir}/src/main/resources</directory> </resource> </resources>
2.在dao目录下执行:mvn clean install -Dmaven.test.skip=true,以生成jar包
3.在web工程pom.xml添加以下插件配置
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>generate-resources</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>com.test</groupId> <artifactId>dao</artifactId> <version>1.0</version> <type>jar</type> <overWrite>true</overWrite> <outputDirectory>./target/classes</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin>
上述插件表示将dao-1.0.jar解压至web工程的target/classes目录中(即工程编译源码目录)
4.运行web工程,即可发现dao相关配置文件及源码已合并过来。(注意,此时在默认的war包的lib中还会包含dao的jar)
若还需要对war工程进行jar打包部署,则在web工程pom.xml中添加jar打包插件即可
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <id>1</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classesDirectory>./target/classes</classesDirectory> <excludes> <exclude>*.properties</exclude> <exclude>*.xml</exclude> </excludes> <finalName>web</finalName> <outputDirectory>./target/WEB-INF/lib</outputDirectory> </configuration> </execution> </executions> </plugin>
加入后,在web下重新执行mvn install命令,会在target/WEB-INF/lib目录下生成web.jar文件,jar中不包含properties和xml文件(去掉excludes,默认情况下会将所有文件进行打包)。
在上面情况下并没有将配置文件打包,因此需要将配置文件进行拷贝:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <encoding>UTF-8</encoding> <outputDirectory>./target/WEB-INF/classes</outputDirectory> <resources> <resource> <directory>./target/classes</directory> <includes> <include>*.properties</include> </includes> <filtering>true</filtering> </resource> </resources> </configuration> </execution> <!-- 拷贝web.xml文件--> <execution> <id>copy-web-xml</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <encoding>UTF-8</encoding> <outputDirectory>./target/WEB-INF</outputDirectory> <resources> <resource> <directory>./src/main/webapp/WEB-INF</directory> <includes> <include>*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
通过上面配置方式,则可将配置文件拷贝至target/WEB-INF/classes目录下
通过上述配置,对于web工程部署,还缺少所依赖的其他jar包,因此需要将jar包拷贝过来(同时去除依赖的dao):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>./target/WEB-INF/lib</outputDirectory> <excludeGroupIds>com.test</excludeGroupIds> </configuration> </execution> </executions> </plugin>
通过以上步骤可在target目录看到web常规工程部署结构:
加载全部内容