【maven多模块工程打包部署】
一般maven多模块工程结构如下图,图中分为dao数据层和上层web层(当然还可以有service层),在进行多模块划分的时候,一般将dao层采用jar进行打包,web层进行war打包。
在进行war包部署时,发现dao是以jar包形式存在于lib包目录下,dao里引用的配置文件也都在自己的jar文件里,
如果部署服务器时,web层会引用不到dao里的配置文件。因此研究了下用maven进行合并打包的方法:
[
](javascript:void(0)😉
project
|--business (核心业务)
|--business-api
|--business-service
|--business-message
|--business-dao
|--business-web
|--common (公共组件、服务、常量)
|--common-component
|--common-component-...
|--common-service
|--common-constants
|--common-...
|--management (管理台)
|--management-...
|--taskserver (定时任务、批处理)
|--msgserver (消息队列)
[
](javascript:void(0)😉
1.确保dao pom.xml中有以下配置
[
](javascript:void(0)😉
<!--配置打包时不过滤非java文件开始 -->
<!--说明,在进行模块化开发打jar包时,maven会将非java文件过滤掉,
xml,properties配置文件等,但是这些文件又是必需的,
使用此配置可以在打包时将不会过滤这些必需的配置文件。
-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.js</include>
<include>**/*.json</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<!-- resource的filtering属性用来表示资源文件中的占位符是否需要被替换,true为需要替换。 -->
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
[
](javascript:void(0)😉
2.在web工程pom.xml添加以下插件配置
[
](javascript:void(0)😉
<build>
<!-- war的项目配置需加上如下内容 start -->
<plugins>
<!-- 下述插件表示将business_service.0.0.1-SNAPSHOT.jar解压至web工程的target/classes目录中(即工程编译源码目录)
子模块jar文件里的配置文件也会相应解压到target/classes目录中
-->
<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.ovit</groupId>
<artifactId>business_service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-dependency-plugin
</artifactId>
<versionRange>
[2.1,)
</versionRange>
<goals>
<goal>unpack</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<!-- war的项目配置需加上如下内容 end -->
</build>
[
](javascript:void(0)😉
3.运行web工程,即可发现dao相关配置文件及源码已合并过来。(注意,此时在默认的war包的lib中还会包含dao的jar)
/build>
[[外链图片转存中...(img-c7dQWKrF-1608728294816)]](javascript:void(0);)
3.运行web工程,即可发现dao相关配置文件及源码已合并过来。(注意,此时在默认的war包的lib中还会包含dao的jar)
本文介绍如何在Maven多模块工程中正确打包并部署,包括配置非Java文件不被过滤,以及通过特定插件将依赖模块的内容解压至目标目录。
1921

被折叠的 条评论
为什么被折叠?



