You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven如何部署来自其他依赖的单个.properties配置文件

你之前遇到的两个问题根源是:

  1. assembly的dependencySet默认会将依赖输出到以依赖坐标命名的子目录下,所以properties不会出现在根目录
  2. dir格式的assembly产物默认不会被Maven标记为待部署构件,所以deploy到Artifactory时只会上传pom文件

解决方案

通过调整assembly配置+补充构建辅助插件即可满足需求,具体配置如下:

1. 修改assembly描述文件

新增配置关闭额外根目录生成、指定properties输出到根目录:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  <id>package</id>
  <formats>
    <format>dir</format>
  </formats>
  <!-- 关闭assembly默认生成的带id后缀的根目录 -->
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <includes>
        <include>*:properties</include>
      </includes>
      <!-- 指定properties直接输出到目标根目录,不生成依赖相关子目录 -->
      <outputDirectory>/</outputDirectory>
    </dependencySet>
  </dependencySets>
</assembly>

2. 调整pom.xml插件配置

保留原有assembly插件配置的基础上,新增build-helper-maven-plugin,将生成的properties文件标记为待部署构件,执行deploy时就会自动上传到Artifactory:

<build>
  <plugins>
    <!-- 原有maven-assembly-plugin配置,确保绑定到package阶段 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.4.1</version>
      <configuration>
        <descriptors>
          <descriptor>src/main/assembly/assembly.xml</descriptor>
        </descriptors>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <!-- 新增构建辅助插件,附加properties为部署构件 -->
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>3.3.0</version>
      <executions>
        <execution>
          <id>attach-properties</id>
          <phase>package</phase>
          <goals>
            <goal>attach-artifact</goal>
          </goals>
          <configuration>
            <artifacts>
              <artifact>
                <!-- 替换为实际生成的properties文件路径、名称 -->
                <file>${project.build.directory}/your-config.properties</file>
                <type>properties</type>
              </artifact>
            </artifacts>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

如果需要同时部署多个properties文件,在artifacts节点下新增多个artifact子项即可。

内容的提问来源于stack exchange,提问作者Stéphane GRILLON

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 01:06:03