Maven打包配置文件

本文介绍如何使用Maven将不同环境的配置文件打包为zip或tar.gz格式,涉及maven-assembly-plugin和maven-resources-plugin的配置方法。

通常我们会有多个系统环境,对应有不同的配置文件,通常在maven中我们打包只有jar, pom, maven-plugins, war and ear. 为了可以同时打包不同环境的配置文件,我们选择maven-assembly-plugin,把指定路径下的各种环境配置打包成zip和tar.gz包。
文件夹结构如下:

// mave standard folders
src/main/java
....
//config folder and files for different env
conf/dev (properties and log4j xml files)
conf/local
conf/uat
conf/prod
//config assembly files for different env
src/main/assembly/config/dev.xml
src/main/assembly/config/local.xml
src/main/assembly/config/uat.xml
src/main/assembly/config/prod.xml

maven pom实现:

<properties>
   <!-- -->
   <assemble.directory>${basedir}/conf</assemble.directory>
</properties>
<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>package-config</id>
                        <!-- 绑定到package生命周期阶段上 -->
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptorSourceDirectory> <!--描述文件夹路径-->                                <descriptor>${basedir}/src/main/assembly/config</descriptorSourceDirectory>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

assembly 配置文件实现(dev为例)

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>config-dev</id>
    <formats>
        <format>zip</format>
        <format>tar.gz</format>
    </formats>
     <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${assemble.directory}/dev</directory>
            <outputDirectory>/${project.version}</outputDirectory>
            <lineEnding>unix</lineEnding>
            <fileMode>0644</fileMode>
            <directoryMode>0755</directoryMode>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
    </fileSets>
 </assembly>

如果需要对配置文件做处理,那么还需要使用maven-release-plugin对src/conf/下面的文件夹内容做处理,比如拷贝conf到target对应的folder,这样assembly的diretory也要跟着修改。

<plugin> 
  <groupId>org.apache.maven.plugins</groupId> 
  <artifactId>maven-resources-plugin</artifactId> 
  <version>2.5</version> 
  <executions> 
   <execution> 
     <id>dev-config</id> 
     <goals> 
         <goal>copy-resources</goal> 
     </goals>   
     <configuration> 
              <outputDirectory>${project.build.directory}/deploy/config-dev</outputDirectory> 
              <resources>          
                <resource> 
                  <directory>${basedir}/conf/dev</directory> 
                  <filtering>true</filtering>
                  <includes>
                     <include>*.properties</include>
                     <include>*.xml</include>
                  </include>
                </resource> 
              </resources>              
            </configuration>  
   </execution> 
  </executions> 
</plugin> 
在使用Maven打包项目时,如果发现配置文件如`application.properties`或其他资源文件未包含在最终的JAR包中,通常是由于Maven默认的资源过滤配置或项目结构设置不当所致。 Maven默认会将`src/main/resources`目录下的资源文件包含在构建输出中,并打包进JAR文件。如果`application.properties`文件未被正确包含,应检查其是否放置在`src/main/resources`目录下。Maven的约定优于配置原则意味着只要按照默认结构组织项目,通常无需额外配置即可完成资源的正确打包[^1]。 如果确实需要自定义资源目录,可以在`pom.xml`中通过`<resources>`标签指定额外的资源目录。例如: ```xml <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> <resource> <directory>config</directory> <targetPath>${project.build.outputDirectory}</targetPath> <includes> <include>*.properties</include> </includes> </resource> </resources> </build> ``` 此配置确保`config`目录下的`.properties`文件也会被打包进JAR中。此外,还可以使用`spring.config.location`参数指定外部配置文件的位置,这样服务启动后可以读取外部的配置文件[^3]。 如果在使用Spring Boot时遇到`application.properties`文件加载不上,可能是因为`pom.xml`中的`<packaging>`元素被错误地设置为`pom`而非`jar`,这会导致Maven无法正确地将资源文件打包进JAR中[^2]。 对于需要排除某些配置文件的情况,可以利用Maven的插件机制来实现。例如,使用`maven-war-plugin`插件的`<packagingExcludes>`功能来排除不需要的资源文件[^4]。 ### Maven资源过滤配置 Maven提供了资源过滤的功能,允许在构建过程中替换资源文件中的变量。为了启用资源过滤,需要在`pom.xml`中的`<build>`部分设置`<filtering>true</filtering>`。例如: ```xml <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> ``` 启用资源过滤后,Maven会在构建过程中替换资源文件中的`${}`占位符为实际值。 ### 使用Maven打包资源文件 使用Maven打包资源文件时,可以通过执行`mvn package`命令在`target`目录下生成JAR文件。解压该JAR文件后,可以看到`application.properties`文件位于`META-INF`目录下[^5]。 ```bash mvn package ``` 生成的JAR文件会包含所有必要的资源文件,包括`application.properties`等。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值