如何在Java中无需编写插件即可编程设置Maven构建属性
解决方案
方案一:无需编写代码(推荐)
直接使用build-helper-maven-plugin直接生成正斜杠格式的basedir属性,完全满足你的需求,无需自定义Java代码:
首先在pom.xml中添加插件配置:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.4.0</version> <executions> <execution> <id>convert-basedir-to-unix-path</id> <phase>initialize</phase> <goals> <goal>regex-property</goal> </goals> <configuration> <name>basedir.unix</name> <value>${basedir}</value> <!-- 替换所有反斜杠为正斜杠,XML中需要双重转义正则的反斜杠 --> <regex>\\</regex> <replacement>/</replacement> <failIfNoMatch>false</failIfNoMatch> </configuration> </execution> </executions> </plugin>
之后你在整个Maven生命周期的所有插件配置、资源过滤中都可以直接使用${basedir.unix}这个属性,值就是正斜杠格式的项目根目录绝对路径。
方案二:通过exec:java编程实现属性定义
如果你有更复杂的属性计算逻辑,确实可以通过exec:java运行自定义代码生成属性,步骤如下:
1. 编写属性处理Java类
编写一个简单的Java类,处理路径并写入临时属性文件:
import java.io.File; import java.io.FileOutputStream; import java.util.Properties; public class MavenPropertyGenerator { public static void main(String[] args) throws Exception { // 获取原生basedir String basedir = System.getProperty("basedir"); // 转换为正斜杠格式 String unixBasedir = basedir.replace('\\', '/'); // 写入临时属性文件 Properties props = new Properties(); props.setProperty("basedir.unix", unixBasedir); // 你可以在这里添加任意自定义属性 try (FileOutputStream fos = new FileOutputStream("target/generated-maven.properties")) { props.store(fos, "Generated maven properties"); } } }
2. 配置exec:java执行类和属性加载
在pom.xml中添加以下配置:
<!-- 执行自定义代码生成属性文件 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>generate-custom-properties</id> <phase>initialize</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>com.yourpackage.MavenPropertyGenerator</mainClass> <systemProperties> <systemProperty> <key>basedir</key> <value>${basedir}</value> </systemProperty> </systemProperties> </configuration> </execution> </executions> </plugin> <!-- 加载生成的属性文件到Maven上下文 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <id>load-custom-properties</id> <phase>initialize</phase> <goals> <goal>load-project-properties</goal> </goals> <configuration> <files> <file>target/generated-maven.properties</file> </files> </configuration> </execution> </executions> </plugin>
应用到WildFly CLI脚本
完成属性定义后,只需要开启Maven资源过滤替换脚本中的变量即可:
- 将你的CLI脚本放在
src/main/cli目录下,修改脚本中的变量为${basedir.unix}:
/subsystem=datasources/data-source=MyDS:add(\ jndi-name=java:jboss/datasources/MyDS,\ driver-name=h2,connection-url="jdbc:h2:file:${basedir.unix}/db;\ TRACE_LEVEL_SYSTEM_OUT=0",user-name=sa,\ validate-on-match=true,background-validation=false,\ driver-class=org.h2.Driver)
- 配置资源过滤,替换脚本变量:
<build> <resources> <resource> <directory>src/main/cli</directory> <filtering>true</filtering> <targetPath>${project.build.directory}/cli</targetPath> </resource> </resources> </build>
之后在wildfly-maven-plugin中直接引用target/cli下已经完成变量替换的脚本即可。
内容的提问来源于stack exchange,提问作者user1050755
相关产品推荐
相关产品推荐

