Maven 多web模块项目配置(多war归并)
Maven 多web模块项目配置(多war合并)
有时候在一个项目中为了清晰划分不同web模块需要配置多个web module 。如:
<modules> <!-- 主web应用 --> <module>cathy-web</module> <module>cathy-biz</module> <module>cathy-common-util</module> <module>cathy-common-intergration</module> <module>cathy-common-facade</module> <module>cathy-common-model</module> <module>cathy-common-dal</module> <module>cathy-core-service</module> <!-- 子web应用 --> <module>cathy-backend</module> </modules>
配置看起来很 简单,在打包的时候需要将这两个web打成一个war包又该如何配置呢?此时就需要使用到maven-war-plugin这个插件,当然这个插件的功能不仅仅是将多个war包合并成一个war.
在 cathy-web 这个module的pom 中的dependencies进行如下配置
<dependency> <groupId>com.david.demo</groupId> <artifactId>cathy-backend</artifactId> <type>war</type> </dependency>
在plugins增加如下配置
<!-- 合并多个war --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <packagingExcludes>WEB-INF/web.xml</packagingExcludes> <failOnMissingWebXml>false</failOnMissingWebXml> <overlays> <overlay> <groupId>com.david.demo</groupId> <artifactId>cathy-backend</artifactId> </overlay> </overlays> </configuration> </plugin>
在主pom中的依赖中增加
<dependency> <groupId>com.david.demo</groupId> <artifactId>cathy-backend</artifactId> <version>0.0.1-SNAPSHOT</version> <type>war</type> <scope>runtime</scope> </dependency>
子web module 的结构是“
在项目的主目录下执行 mvn -Dmaven.test.skip=true后
可以看到子web module中的文件夹已经合并到主web中了,注意如果同名的文件夹和合并的,同时不合并空文件夹
在实际的项目中配置文件在不同的环境中配置是不同的,那么如何根据环境去参数化配置呢?
在主web的pom中进行如下配置
<profiles> <profile> <id>dev</id> <build> <filters> <filter> src/main/resources/META-INF/config-dev.properties </filter> </filters> </build> </profile> <profile> <id>test</id> <build> <filters> <filter> src/main/resources/META-INF/config-test.properties </filter> </filters> </build> </profile> </profiles>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <packagingExcludes>WEB-INF/web.xml</packagingExcludes> <failOnMissingWebXml>false</failOnMissingWebXml> <webResources> <resource> <directory> src/main/resources/config </directory> <filtering>true</filtering> <targetPath>WEB-INF/classes</targetPath> </resource> </webResources> <overlays> <overlay> <groupId>com.david.demo</groupId> <artifactId>cathy-backend</artifactId> </overlay> </overlays> </configuration> </plugin>
config目录中log4j.xml的配置文件中有:
<appender name="DEFAULT-APPENDER" class="com.david.common.log4j.DailyRollingFileAppender"> <param name="file" value="P:/${host_name}/common-default.log" /> <param name="append" value="true" /> <param name="encoding" value="GBK" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%X{loginUserEmail}/%X{loginUserID}/%X{remoteAddr}/%X{clientId} - %X{requestURIWithQueryString}] %-5p %c{2} - %m%n" /> </layout> </appender>
使用 mvn package -P test -Dmaven.test.skip=true 打包的结果是:
<!-- ===== [公共Appender] ===== --> <!-- [公共Appender] 默认 --> <appender name="DEFAULT-APPENDER" class="com.david.common.log4j.DailyRollingFileAppender"> <param name="file" value="P:/test/common-default.log" /> <param name="append" value="true" /> <param name="encoding" value="GBK" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%X{loginUserEmail}/%X{loginUserID}/%X{remoteAddr}/%X{clientId} - %X{requestURIWithQueryString}] %-5p %c{2} - %m%n" /> </layout> </appender>