首页 > 代码库 > mvn项目压缩打包

mvn项目压缩打包

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

技术分享
 1 <project>
 2     ...
 3     <!-- 定义环境 -->
 4     <profiles>
 5         <!-- 开发环境 -->
 6         <profile>
 7             <id>dev</id>
 8             <properties>
 9                 <!-- 定义profileActive属性 -->
10                 <profileActive>dev</profileActive>
11             </properties>
12             <activation>
13                 <activeByDefault>true</activeByDefault>
14             </activation>
15         </profile>
16 
17         <!-- 测试环境 -->
18         <profile>
19             <id>test</id>
20             <properties>
21                 <profileActive>test</profileActive>
22             </properties>
23         </profile>
24 
25         <!-- 生产环境 -->
26         <profile>
27             <id>prod</id>
28             <properties>
29                 <profileActive>prod</profileActive>
30             </properties>
31         </profile>
32     </profiles>
33     
34     <build>
35         ...
36         <!-- 资源配置 -->
37         <resources>
38             <resource>
39                 <directory>src/main/resources</directory>
40                 <filtering>true</filtering>
41                 <excludes>
42                     <exclude>xxx-dev.yml</exclude>
43                     <exclude>xxx-test.yml</exclude>
44                     <exclude>xxx-prod.yml</exclude>
45                 </excludes>
46             </resource>
47             <resource>
48                 <directory>src/main/resources</directory>
49                 <filtering>true</filtering>
50                 <includes>
51                     <include>xxx-${profileActive}.yml</include>
52                 </includes>
53             </resource>
54         </resources>
55         ...
56     </build>
57 </project>
View Code

注:通过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数据源配置:

技术分享
 1 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
 2     <property name="driverClass" value="${driverClass}" />
 3     <property name="jdbcUrl" value="${jdbcUrl}" />
 4     <property name="user" value="${user}" />
 5     <property name="password" value="${password}" />
 6     <property name="initialPoolSize" value="${initialPoolSize}" />
 7     <property name="minPoolSize" value="${minPoolSize}" />
 8     <property name="maxPoolSize" value="${maxPoolSize}" />
 9     <property name="breakAfterAcquireFailure" value="${breakAfterAcquireFailure}" />
10     <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" />
11     <property name="acquireRetryAttempts" value="${acquireRetryAttempts}" />
12     <property name="acquireIncrement" value="${acquireIncrement}" />
13     <property name="acquireRetryDelay" value="${acquireRetryDelay}" />
14     <property name="testConnectionOnCheckout" value="${testConnectionOnCheckout}" />
15     <property name="testConnectionOnCheckin" value="${testConnectionOnCheckin}" />
16 </bean>
View Code

 

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

pom.xml:

技术分享
 1 <build>
 2     <plugins>
 3         <!-- 部署打包: 通过maven-assembly插件压缩为tar包进行发布 -->
 4         <plugin>
 5             <artifactId>maven-assembly-plugin</artifactId>
 6             <configuration>
 7                 <!-- not append assembly id in release file name -->
 8                 <appendAssemblyId>false</appendAssemblyId>
 9                 <descriptors>
10                     <descriptor>assembly.xml</descriptor>
11                 </descriptors>
12                 <finalName>xxx-${project.version}</finalName>
13                 <!-- 定义压缩包存放位置 -->
14                 <outputDirectory>${basedir}/release</outputDirectory>
15             </configuration>
16             <executions>
17                 <execution>
18                     <id>make-assembly</id>
19                     <phase>verify</phase>
20                     <goals>
21                         <goal>single</goal>
22                     </goals>
23                 </execution>
24             </executions>
25          </plugin>
26     </plugins>
27 </build>
View Code

 assembly.xml:

技术分享
 1 <assembly
 2     xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
 5     <id>distribution</id>
 6     <formats>
 7         <format>tar</format>
 8     </formats>
 9     <fileSets>
10         <!-- 打包配置文件 -->
11         <fileSet>
12             <directory>src/main/resources</directory>
13             <outputDirectory>/conf</outputDirectory>
14             <includes>
15                 <include>jdbc.properties</include>
16             </includes>
17         </fileSet>
18         <!-- 打包sql文件 -->
19         <fileSet>
20             <directory>${basedir}</directory>
21             <outputDirectory>/sql</outputDirectory>
22             <includes>
23                 <include>*.sql</include>
24             </includes>
25         </fileSet>
26         <!-- 打包INSTALL,change.log -->
27         <fileSet>
28             <directory>${basedir}</directory>
29             <outputDirectory>/</outputDirectory>
30             <includes>
31                 <include>INSTALL</include>
32                 <include>change.log</include>
33             </includes>
34         </fileSet>
35         <!-- 打包war文件 -->
36         <fileSet>
37             <directory>${project.build.directory}</directory>
38             <outputDirectory>/</outputDirectory>
39             <includes>
40                 <include>*.war</include>
41             </includes>
42         </fileSet>
43     </fileSets>
44     <!-- 定义压缩包根目录名称 -->
45     <baseDirectory>xxx-${project.version}</baseDirectory>
46 </assembly>
View Code

 

mvn项目压缩打包