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

如何在Maven项目中同时以两种方式使用properties-maven-plugin插件?

如何在Maven项目中同时以两种方式使用properties-maven-plugin插件?

嘿,我来给你捋捋怎么搞定这个需求!你要在同一个Maven项目里用properties-maven-plugin干两件事:一是读取带过滤器的本地配置文件,二是读取另一个项目里的属性文件对吧?其实核心思路超简单——给这个插件配置两个独立的执行单元(execution),让它们各自按需求跑逻辑,完全不会互相干扰。

下面我把你没写完的代码补全成完整可参考的配置,你可以照着改:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <executions>
                <!-- 第一个执行单元:处理带过滤器的本地配置文件 -->
                <execution>
                    <id>read-filtered-local-props</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <!-- 替换成你本地带过滤器的属性文件路径 -->
                        <files>
                            <file>src/main/resources/your-filtered-config.properties</file>
                        </files>
                        <!-- 开启过滤功能,指定你的环境过滤器文件 -->
                        <filtering>true</filtering>
                        <filters>
                            <filter>src/main/filters/your-env-filters.properties</filter>
                        </filters>
                    </configuration>
                </execution>

                <!-- 第二个执行单元:读取另一个项目的属性文件 -->
                <execution>
                    <id>read-external-project-props</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <!-- 替换成外部项目属性文件的路径,比如依赖jar包内的文件 -->
                        <!-- 注意:必须先在pom的dependencies里引入这个外部项目哦 -->
                        <files>
                            <file>${project.build.directory}/dependency/com/xxx/external-project/1.0.0/external-project-1.0.0.jar!/META-INF/external-props.properties</file>
                        </files>
                        <!-- 外部项目的属性文件一般不需要过滤,直接关闭即可 -->
                        <filtering>false</filtering>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- 额外提醒:如果你的资源文件也需要过滤,记得配这个插件配合 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.3.1</version>
            <configuration>
                <filtering>true</filtering>
                <filters>
                    <filter>src/main/filters/your-env-filters.properties</filter>
                </filters>
            </configuration>
        </plugin>
    </plugins>
</build>

最后给你划几个必看的重点,避免踩坑:

  • 每个execution一定要设置唯一的id,不然Maven会把它们当成同一个逻辑覆盖掉,这是新手最容易犯的错!
  • 执行阶段要选对:比如initialize适合提前读取属性,process-resources适合处理资源文件;如果有依赖顺序要求(比如外部属性要先读),就调整阶段顺序(比如把外部属性的阶段设为validate)。
  • 读外部项目属性前,必须先在pom.xml的<dependencies>里声明那个外部项目的依赖,不然Maven找不到对应jar包,路径肯定报错。嫌写全路径麻烦的话,也可以用classpath:META-INF/external-props.properties这种方式,前提是外部项目的属性文件在classpath范围内能被找到。

备注:内容来源于stack exchange,提问作者GargaLeNoir

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.13 17:28:09