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

如何在Maven构建中修改JAR包内的preferences.ini文件?

在Maven构建中修改JAR内preferences.ini文件的方案

方案一:打包前预处理(更符合Maven生命周期规范)

如果preferences.ini是项目源码中的资源文件,最合理的方式是在打包阶段直接替换版本,避免事后修改JAR:

  • 将preferences.ini放在项目的src/main/resources目录下,内容修改为:
# Server version
version.server = ${custom.server.version}
  • 在pom.xml中定义自定义版本属性,并配置maven-resources-plugin开启资源过滤:
<properties>
    <!-- 自定义服务器版本,可根据需要动态调整 -->
    <custom.server.version>1.2.3.4.22.11.13</custom.server.version>
</properties>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <!-- 开启过滤,替换资源文件中的Maven属性 -->
            <filtering>true</filtering>
            <!-- 指定只过滤preferences.ini,避免影响其他资源文件 -->
            <includes>
                <include>preferences.ini</include>
            </includes>
        </resource>
    </resources>

    <!-- 显式配置插件确保过滤逻辑生效 -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.3.1</version>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

执行mvn package后,生成的JAR中preferences.ini的版本会自动替换为指定值。

方案二:修改已打包的JAR文件(针对已生成JAR的场景)

如果必须修改已打包完成的JAR,推荐使用官方的maven-antrun-plugin(这是Maven规范内的扩展方式,并非临时方案),通过Ant任务完成解压、修改、重打包的流程:
在pom.xml中添加如下插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>modify-jar-content</id>
                    <!-- 绑定到package阶段之后,确保JAR已生成 -->
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <!-- 定义临时目录与目标JAR路径 -->
                            <property name="temp.dir" value="${project.build.directory}/jar-temp"/>
                            <property name="jar.file" value="${project.build.directory}/${project.build.finalName}.jar"/>
                            
                            <!-- 创建临时目录 -->
                            <mkdir dir="${temp.dir}"/>
                            <!-- 解压JAR到临时目录 -->
                            <unzip src="${jar.file}" dest="${temp.dir}"/>
                            <!-- 修改preferences.ini中的版本信息,注意token要完全匹配原内容 -->
                            <replace file="${temp.dir}/preferences.ini"
                                     token="version.server = 1.2.3.4"
                                     value="version.server = 1.2.3.4.22.11.13"/>
                            <!-- 删除原JAR -->
                            <delete file="${jar.file}"/>
                            <!-- 重新打包JAR -->
                            <zip destfile="${jar.file}" basedir="${temp.dir}"/>
                            <!-- 清理临时目录 -->
                            <delete dir="${temp.dir}"/>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

注意事项:

  • 替换的token要和原文件中的内容完全匹配(包括空格、换行等格式)
  • 可以将版本值替换为Maven属性(比如${custom.server.version}),实现动态配置
  • 建议使用插件的最新稳定版本,避免兼容性问题

内容的提问来源于stack exchange,提问作者Alexander D.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 10:20:41