如何从Java类获取多值并在Ant的build.xml中作为属性使用?
问题描述
需要从Java类中获取多个值,作为Ant任务(build.xml)的属性使用。现有代码如下:
exampleClass.java
String key1 = "my.test.key1"; String value1 = "my.test.value1"; String key2 = "my.test.key2"; String value2 = "my.test.value2"; Properties systemProperties = System.getProperties(); systemProperties.put(key1, value1); systemProperties.put(key2, value2); System.setProperties(systemProperties);
build.xml
<macrodef name="nexDecryptingPwds"> <sequential> <java classname="my.util.exec.exampleClass" fork="true" failonerror="false"> <classpath> <fileset dir="${install.dir}/lib/"> <include name="**/*.jar" /> </fileset> <pathelement location="${basedir}" /> </classpath> </java> </sequential> </macrodef> <echo message="my.test.key1: ${my.test.key1}" /> <echo message="my.test.key2: ${my.test.key2}" />
目前无法获取上述两个键对应的值,已知resultproperty属性一次只能获取一个值,请问该如何解决?
解决方案
核心问题在于使用了fork="true",这会让Java程序在独立进程中运行,它设置的系统属性无法传递回Ant主进程,导致Ant读取不到。以下是三种可行的解决方式:
方式一:Java类输出键值对,Ant解析输出
- 修改
exampleClass.java,将键值对以key=value格式输出到标准输出:
String key1 = "my.test.key1"; String value1 = "my.test.value1"; String key2 = "my.test.key2"; String value2 = "my.test.value2"; // 每行输出一个键值对 System.out.println(key1 + "=" + value1); System.out.println(key2 + "=" + value2);
- 修改build.xml,用
outputproperty捕获Java程序的输出,再通过loadproperties转化为Ant属性:
<macrodef name="nexDecryptingPwds"> <sequential> <java classname="my.util.exec.exampleClass" fork="true" failonerror="false" outputproperty="props.output"> <classpath> <fileset dir="${install.dir}/lib/"> <include name="**/*.jar" /> </fileset> <pathelement location="${basedir}" /> </classpath> </java> <!-- 将输出内容转为Ant属性 --> <loadproperties> <string value="${props.output}" /> </loadproperties> </sequential> </macrodef> <!-- 先调用宏定义生成属性,再输出 --> <nexDecryptingPwds /> <echo message="my.test.key1: ${my.test.key1}" /> <echo message="my.test.key2: ${my.test.key2}" />
方式二:取消进程隔离,让Java类在Ant进程内运行
如果Java类没有依赖冲突或特殊进程隔离需求,直接去掉fork="true",这样Java类设置的系统属性会直接在Ant进程中生效:
<macrodef name="nexDecryptingPwds"> <sequential> <java classname="my.util.exec.exampleClass" fork="false" failonerror="false"> <classpath> <fileset dir="${install.dir}/lib/"> <include name="**/*.jar" /> </fileset> <pathelement location="${basedir}" /> </classpath> </java> </sequential> </macrodef> <nexDecryptingPwds /> <echo message="my.test.key1: ${my.test.key1}" /> <echo message="my.test.key2: ${my.test.key2}" />
方式三:Java类写入属性文件,Ant加载文件
- 修改
exampleClass.java,将键值对写入临时属性文件:
import java.io.FileOutputStream; import java.util.Properties; public class exampleClass { public static void main(String[] args) throws Exception { String key1 = "my.test.key1"; String value1 = "my.test.value1"; String key2 = "my.test.key2"; String value2 = "my.test.value2"; Properties props = new Properties(); props.put(key1, value1); props.put(key2, value2); // 写入临时属性文件 props.store(new FileOutputStream("temp.properties"), "Generated properties"); } }
- 修改build.xml,执行Java任务后加载该属性文件,可选删除临时文件:
<macrodef name="nexDecryptingPwds"> <sequential> <java classname="my.util.exec.exampleClass" fork="true" failonerror="false"> <classpath> <fileset dir="${install.dir}/lib/"> <include name="**/*.jar" /> </fileset> <pathelement location="${basedir}" /> </classpath> </java> <!-- 加载生成的属性文件 --> <loadproperties srcfile="temp.properties" /> <!-- 可选:清理临时文件 --> <delete file="temp.properties" /> </sequential> </macrodef> <nexDecryptingPwds /> <echo message="my.test.key1: ${my.test.key1}" /> <echo message="my.test.key2: ${my.test.key2}" />
内容的提问来源于stack exchange,提问作者Yuki Yasha
相关产品推荐
相关产品推荐

