mvn项目压缩打包

通常情况下,maven打包结果为jar或war包。
如果需要一并打包配置文件等参数,通过resources配置指定需要打包的文件参数,如下示例:

 <project>
...
<!-- 定义环境 -->
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<properties>
<!-- 定义profileActive属性 -->
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile> <!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile> <!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles> <build>
...
<!-- 资源配置 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>xxx-dev.yml</exclude>
<exclude>xxx-test.yml</exclude>
<exclude>xxx-prod.yml</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>xxx-${profileActive}.yml</include>
</includes>
</resource>
</resources>
...
</build>
</project>

注:通过resources配置打包的文件是一并打包到jar包或war包中的(位于classpath路径下)。

而在生产环境部署项目时,却需要动态修改数据库配置等参数,此时如果仅仅打一个war进行部署,对于配置文件修改非常不方便。
那这里就提出2个问题:
第一,J2EE项目是否可以支持配置文件在war包之外?
第二,如何通过maven将项目文件进行统一打包压缩?

Q1: 在J2EE项目中使用Spring框架时,可以将配置文件写在properties文件中,从外部加载相应配置参数。示例如下:

web.xml添加配置文件路径参数:

<context-param>
<param-name>confDir</param-name>
<param-value>file:${catalina.home}/conf</param-value><!--将配置文件放到tomcat目录下-->
<!-- <param-value>classpath:</param-value> --><!--将配置文件放在项目classpath路径下-->
</context-param>

spring加载配置文件:( 详见:https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ 之7.8 Container Extension Points

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>${confDir}/jdbc.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>

spring数据源配置:

 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${user}" />
<property name="password" value="${password}" />
<property name="initialPoolSize" value="${initialPoolSize}" />
<property name="minPoolSize" value="${minPoolSize}" />
<property name="maxPoolSize" value="${maxPoolSize}" />
<property name="breakAfterAcquireFailure" value="${breakAfterAcquireFailure}" />
<property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" />
<property name="acquireRetryAttempts" value="${acquireRetryAttempts}" />
<property name="acquireIncrement" value="${acquireIncrement}" />
<property name="acquireRetryDelay" value="${acquireRetryDelay}" />
<property name="testConnectionOnCheckout" value="${testConnectionOnCheckout}" />
<property name="testConnectionOnCheckin" value="${testConnectionOnCheckin}" />
</bean>

Q2: 通过maven插件maven-assembly-plugin将项目文件进行统一压缩打包,更专业,详见:http://maven.apache.org/plugins/maven-assembly-plugin/
使用该插件时需要单独定义一个打包规则文件,如:assembly.xml,在该文件中定义一系列需要打包的文件参数。

pom.xml:

 <build>
<plugins>
<!-- 部署打包: 通过maven-assembly插件压缩为tar包进行发布 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<finalName>xxx-${project.version}</finalName>
<!-- 定义压缩包存放位置 -->
<outputDirectory>${basedir}/release</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>verify</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

assembly.xml:

 <assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>distribution</id>
<formats>
<format>tar</format>
</formats>
<fileSets>
<!-- 打包配置文件 -->
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/conf</outputDirectory>
<includes>
<include>jdbc.properties</include>
</includes>
</fileSet>
<!-- 打包sql文件 -->
<fileSet>
<directory>${basedir}</directory>
<outputDirectory>/sql</outputDirectory>
<includes>
<include>*.sql</include>
</includes>
</fileSet>
<!-- 打包INSTALL,change.log -->
<fileSet>
<directory>${basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>INSTALL</include>
<include>change.log</include>
</includes>
</fileSet>
<!-- 打包war文件 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.war</include>
</includes>
</fileSet>
</fileSets>
<!-- 定义压缩包根目录名称 -->
<baseDirectory>xxx-${project.version}</baseDirectory>
</assembly>
上一篇:Centos7.5安装kafka集群


下一篇:CentOS7 安装kafka集群