Maven打包配置文件

11 篇文章 0 订阅

通常我们会有多个系统环境,对应有不同的配置文件,通常在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> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven POM (Project Object Model) 文件是 Maven 项目的核心配置文件,其中包含了项目中所有的依赖、插件、构建配置等信息。在 Maven 打包过程中,POM 文件的正确配置非常重要。以下是一个基本的 Maven POM 文件配置示例: ```xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 项目基本信息 --> <groupId>com.example</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <!-- 依赖声明 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <!-- 插件声明 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.example.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project> ``` 在上述 POM 文件中,`<groupId>`、`<artifactId>` 和 `<version>` 标签定义了项目的基本信息。`<dependencies>` 标签用于声明项目所依赖的其他库,`<build>` 标签下的 `<plugins>` 标签用于声明项目构建时所需要的插件。 在打包过程中,可以使用以下命令: ``` mvn package ``` 该命令会在当前项目根目录下生成一个名为 `my-app-1.0-SNAPSHOT.jar` 的可执行 JAR 文件,其中包含了项目的所有依赖和代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值