ANT:如何在构建文件中修改 java.library.path
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/422848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ANT: How to modify java.library.path in a buildfile
提问by Ivo Bosticky
The java.library.path property appears to be read-only. For example when you run ant on the following buildfile
java.library.path 属性似乎是只读的。例如,当您在以下构建文件上运行 ant 时
<project name="MyProject" default="showprops" basedir=".">
<property name="java.library.path" value="test"/>
<property name="some.other.property" value="test1"/>
<target name="showprops">
<echo>java.library.path=${java.library.path}</echo>
<echo>some.other.property=${some.other.property}</echo>
</target>
</project>
you get
你得到
> ant -version
Apache Ant version 1.6.5 compiled on June 2 2005
> ant -Djava.library.path=commandlinedefinedpath
Buildfile: build.xml
showprops:
[echo] java.library.path=commandlinedefinedpath
[echo] some.other.property=test1
BUILD SUCCESSFUL
Total time: 0 seconds
The output indicates that the java.library.pathhasn't been changed, but some.other.propertywas set correctly.
输出表明java.library.path没有改变,但 some.other.property设置正确。
I would like to know how to modify the java.library.path within a buildfile. Specifying the java.library.path on the ant command line is not really an easy option, because the library path location is not know at that time.
我想知道如何修改构建文件中的 java.library.path。在 ant 命令行中指定 java.library.path 并不是一个简单的选择,因为当时不知道库路径位置。
Note: I would like this to work so that I can specify the location of native libraries used in a unit test.
注意:我希望这样可以工作,以便我可以指定单元测试中使用的本机库的位置。
回答by Peter Hilton
Ant properties do not work the way you expect: they are immutable, i.e. you cannot change a property's value after you have set it once. If you run
Ant 属性并不像您期望的那样工作:它们是不可变的,即您不能在设置一次属性后更改它的值。如果你跑
ant -Dsome.other.property=commandlinedefinedpath
ant -Dsome.other.property=commandlinedefinedpath
the output will no longer show
输出将不再显示
[echo] some.other.property=test1
[回声] some.other.property=test1
回答by Yoni Roit
I think you can modify it if you use fork=true in your "java" task. You can supply java.library.path as a nested sysproperty tag.
如果您在“java”任务中使用 fork=true,我认为您可以修改它。您可以提供 java.library.path 作为嵌套的 sysproperty 标记。
回答by OscarRyz
I think this is not possible, mainly because the JVM has already started by the time this value is modified.
我认为这是不可能的,主要是因为在修改此值时JVM已经启动。
You can however try to start a new process with the correct env variables ( see exec or ant tasks )
但是,您可以尝试使用正确的 env 变量启动一个新进程(请参阅 exec 或 ant tasks )
I think what you want is to compute the value of the library at runtime and then use it to run the test. By creating a new process you can have that new process to use the right path.
我认为您想要的是在运行时计算库的值,然后使用它来运行测试。通过创建新流程,您可以让该新流程使用正确的路径。
回答by flicken
If you really want to change a property, you can do this in a Java task or in a scripting language.
如果您真的想要更改属性,您可以在 Java 任务或脚本语言中执行此操作。
Here is an example using Groovy:
下面是一个使用 Groovy 的例子:
<?xml version="1.0"?>
<project name="example" default="run">
<taskdef name="groovy"
classname="org.codehaus.groovy.ant.Groovy"
classpath="lib/groovy-all-1.1-rc-1.jar"/>
<target name="run">
<echo>java.library.path = ${java.library.path}</echo>
<groovy>
properties["java.library.path"] = "changed"
</groovy>
<echo>java.library.path = ${java.library.path}</echo>
</target>
</project>
Caution, this violates Ant's "immutable property" property. Use at your own risk.
注意,这违反了 Ant 的“不可变属性”属性。使用风险自负。

