在pom.xml中是否存在引用Maven settings.xml路径的内置变量?
Maven内置变量获取用户settings.xml路径的问题
我需要在pom.xml里通过org.codehaus.mojo.exec-maven-plugin执行另一条Maven命令,要传入当前使用的用户settings.xml路径。现在我用的是手动设置的环境变量${env.MAVEN_SETTINGS_XML},想知道Maven有没有内置变量可以直接引用用户settings的路径?
当前我的pom.xml配置片段:
<project> ... <build> <plugins> ... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>build-my-app</id> <phase>package</phase> <goals> <goal>exec</goal> </goals> <configuration> <environmentVariables> <JAVA_HOME>${java.home}</JAVA_HOME> <M2_HOME>${maven.home}</M2_HOME> </environmentVariables> <executable>${maven.home}/bin/mvn</executable> <arguments> <argument>--settings</argument> <argument>${env.MAVEN_SETTINGS_XML}</argument> <argument>clean</argument> <argument>package</argument> <argument>-f</argument> <argument>${PARENT_PROJECT_ABSOLUTE_PATH}\pom.xml</argument> <argument>-Dhttps.protocols=${https.protocols}</argument> <argument>-X</argument> <argument>-U</argument> </arguments> </configuration> </execution> </executions> </plugin> ... </plugins> </build> ... </project>
构建日志显示当前使用的用户settings路径:
[DEBUG] Reading global settings from F:\Shared\Programs\Apache\Maven\3.2.5\conf\settings.xml [DEBUG] Reading user settings from F:\Shared\Data\Programs\Apache\Maven\my-project-custom-settings.xml
我需要获取日志中第二条的用户settings.xml路径,替换掉配置里的${env.MAVEN_SETTINGS_XML}。
解决方案
Maven本身没有对外暴露直接获取用户settings.xml路径的通用内置变量,但可以直接通过Maven内部对象属性实现需求:
直接把exec-maven-plugin配置里的${env.MAVEN_SETTINGS_XML}替换为${settings.userSettingsFile}即可,修改后的参数片段如下:
<arguments> <argument>--settings</argument> <argument>${settings.userSettingsFile}</argument> <argument>clean</argument> <argument>package</argument> <argument>-f</argument> <argument>${PARENT_PROJECT_ABSOLUTE_PATH}\pom.xml</argument> <argument>-Dhttps.protocols=${https.protocols}</argument> <argument>-X</argument> <argument>-U</argument> </arguments>
这个${settings.userSettingsFile}会直接返回当前Maven会话中加载的用户settings.xml绝对路径,完全匹配你日志里显示的第二条路径,无需额外插件或手动配置环境变量。
内容的提问来源于stack exchange,提问作者PeterH
相关产品推荐
相关产品推荐

