我想在我的程序中进行条件编译。我知道,如果您声明一个 public static final boolean ,编译器将忽略未遍历的分支。是否可以在构建程序之前让 ant 目标更改变量?
例如,如果我有:
最终公共静态布尔窗口= false;
我想要两个 ant 目标:Windows 和 Mac。
我想要命令
<代码>
蚂蚁窗
将布尔值更改为 true,同时
<代码>
蚂蚁麦克
使变量保持原样。
谢谢。
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?
For example if I have:
final public static boolean windows = false;
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.
Thanks.
发布评论
评论(5)
您可以让 Ant 修改属性文件,然后您可以在应用程序中轻松读取该文件:
new Properties(new FileInputStream("filename" / new File(filename))
) ,并使用以下方式读取属性:
Boolean isWindows = new Boolean(properties.getProperty("windows"))
或:
String os =properties.getProperty (“操作系统”)
。您可以使用 Ant
PropertyFile
任务来执行此操作:http:// /ant.apache.org/manual/Tasks/propertyfile.html。编辑:如果您绝对必须使用 Ant 编辑源代码文件,这里有一个使用另一个任务的替代方法:
-- 替换代码根据需要作为您自己的。请参阅http://ant.apache.org/manual/Tasks/replaceregexp.html 了解详情。
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")
.You can use the Ant
PropertyFile
task for doing this: http://ant.apache.org/manual/Tasks/propertyfile.html.Edit: here's an alternative using another task if you absolutely must edit the source code file using Ant:
<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 确实有一个任务来替换文件中的文本。例如:
You should read the other answers carefully and see if there is a better solution for you. However, Ant does have a task to replace text in files. For example:
<replace file="${src}/MyFile.java" token="boolean windows=true" value="boolean windows=false"/>
跳过 ant 和属性文件等,Java 已经做到了这一点!
使用类似 System.getProperty("os.name"); 的东西
Skip ant and property files etc, Java already does this!
Use something like System.getProperty("os.name");
属性和替换任务应该可以满足您的需要。
我同意寻找不同的方法是个好主意。
但是,如果由于某种原因内置任务无法帮助您
如果你需要什么,为 ant 编写自定义任务非常容易。
请参阅 http://ant.apache.org/manual/develop.html
The properties and the replace tasks should get you what you need.
I concur that finding a different approach is a good idea.
However if for some reason the built in tasks will not get you
what you need, it is pretty easy to write a custom task for ant.
See http://ant.apache.org/manual/develop.html
当从 Ant 调用 Java 主程序时,您还可以提供命令值作为参数。
例如。
ant -f build.xml "YouranttaskName" -Doperatingsys="windows"
在 build.xml 内部
java -main 方法中,此参数值将以相同的顺序可用。ie args[0] 包含“Windows ”。
您可以通过考虑默认操作系统值是什么来编写逻辑,因为用户可能不会提供命令行参数,然后相应地设置“
boolean flag”
参数。You can also provide command-lind value as an argument while calling Java-main program from Ant.
For eg.
ant -f build.xml "YouranttaskName" -Doperatingsys="windows"
Inside build.xml
Inside java -main method this argument-value will be available in same order .i.e. args[0] contains "Windows".
You can write your logic by considering that what is your default OS value, as user may not provide commandline argument and then set '
boolean flag'
parameter accordingly.