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

如何将maven-changelog-plugin生成的变更集文件打包进JAR?

解决Maven构建时将变更集文件打包进JAR的问题

我用maven-changelog-plugin生成Git diff变更集,运行mvn site或mvn changeset:changeset时,文件会输出到target/site/changelog.html和target/changelog.xml。现在想执行mvn clean install构建JAR时把这两个文件包含进去,但之前用build-helper-maven-plugin的尝试没成功,问题出在:

  • reporting块里的插件只会在site阶段执行,clean install不会触发
  • build-helper的add-source是添加源码文件,不是打包资源文件
  • 基于文件存在性激活的profile,在clean后target目录为空,根本不会生效

正确配置方案

把changelog-plugin绑定到构建生命周期的process-resources阶段,确保在打包前生成文件;再用maven-jar-plugin指定要包含的文件路径,或者把文件复制到classpath目录下。

完整配置如下:

<build>
  <plugins>
    <!-- 1. 在构建过程中生成变更集文件 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-changelog-plugin</artifactId>
      <version>2.3</version>
      <executions>
        <execution>
          <id>generate-changelog</id>
          <phase>process-resources</phase>
          <goals>
            <goal>changelog</goal>
          </goals>
          <configuration>
            <type>range</type>
            <range>30</range>
            <!-- 指定输出路径,可选,默认就是target/site和target -->
            <outputDirectory>${project.build.directory}/site</outputDirectory>
            <xmlOutputDirectory>${project.build.directory}</xmlOutputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>

    <!-- 2. 配置JAR插件,将生成的变更集文件包含进JAR -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.3.0</version>
      <configuration>
        <includes>
          <include>../changelog.xml</include>
          <include>site/changelog.html</include>
        </includes>
      </configuration>
    </plugin>
  </plugins>
</build>

配置说明

  • 把changelog-plugin从reporting移到build的plugins里,绑定到process-resources阶段,这样clean install执行时会自动生成变更集文件
  • maven-jar-plugin的includes配置里,路径是相对于target/classes的,所以../changelog.xml对应target/changelog.xml,site/changelog.html对应target/site/changelog.html
  • 如果想把文件放到JAR的特定目录(比如META-INF/changelog),可以先用maven-resources-plugin把文件复制到target/classes/META-INF/changelog,再打包:
<!-- 可选:复制文件到指定目录再打包 -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.3.1</version>
  <executions>
    <execution>
      <id>copy-changelog-files</id>
      <phase>process-resources</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.outputDirectory}/META-INF/changelog</outputDirectory>
        <resources>
          <resource>
            <directory>${project.build.directory}</directory>
            <includes>
              <include>changelog.xml</include>
            </includes>
          </resource>
          <resource>
            <directory>${project.build.directory}/site</directory>
            <includes>
              <include>changelog.html</include>
            </includes>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>

这样执行mvn clean install时,变更集文件就会被打包进最终的JAR包中。

内容的提问来源于stack exchange,提问作者KJ0797

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 22:25:11