可以用ant修改源代码吗? [英] Is it possible to modify source code with ant?
问题描述
我想在我的程序中进行条件编译.我知道如果你声明一个 public static final boolean 编译器将忽略没有遍历的分支.是否可以在构建程序之前让蚂蚁目标更改变量?
I would like to do conditional compilation in a program of mine. I know that if you declare a public static final boolean the compiler will ignore the branch that is not traversed. Is it possible to have an ant target change a variable before building the program?
例如,如果我有:
<代码>final public static boolean windows = false;
我想要两个蚂蚁目标:Windows 和 Mac.我想要命令<代码>蚂蚁窗将布尔值更改为 true,而<代码>蚂蚁麦克保持变量不变.
I would like two ant targets: Windows and Mac.
I would like the command
ant windows
to change the boolean to true, while
ant mac
leaves the variable as is.
谢谢.
推荐答案
你可以让 Ant 修改一个属性文件,然后你可以很容易地在你的应用程序中读取这个文件:new Properties(newFileInputStream("filename"/new File(filename))
),
并使用以下方法读取属性:Boolean isWindows = new Boolean(properties.getProperty("windows"))
或:String os = properties.getProperty(操作系统")
.
You can get Ant to modify a properties file, and then you can read this file in your application pretty easily:new Properties(new FileInputStream("filename" / new File(filename))
),
and read properties using:Boolean isWindows = new Boolean(properties.getProperty("windows"))
or:String os = properties.getProperty("os")
.
您可以使用 Ant PropertyFile
任务来执行此操作:http://ant.apache.org/manual/Tasks/propertyfile.html.
You can use the Ant PropertyFile
task for doing this: http://ant.apache.org/manual/Tasks/propertyfile.html.
如果您绝对必须使用 Ant 编辑源代码文件,这里是使用另一个任务的替代方法:
<replaceregexp file="blah.java" match="public static final boolean WINDOWS = \"(.*)\"" replace="public static final boolean WINDOWS = \"" + ${properties.windows} + "\""/>
-- 根据需要将代码替换为您自己的代码.参见 http://ant.apache.org/manual/Tasks/replaceregexp.html 了解详情.
<replaceregexp file="blah.java" match="public static final boolean WINDOWS = \"(.*)\"" replace="public static final boolean WINDOWS = \"" + ${properties.windows} + "\"" />
-- replace the code as your own as required. See http://ant.apache.org/manual/Tasks/replaceregexp.html for details.
这篇关于可以用ant修改源代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!