ANT: How to modify java.library.path in a buildfile

旧街凉风 提交于 2019-12-19 18:53:59

问题


The java.library.path property appears to be read-only. For example when you run ant on the following buildfile

<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.path hasn't been changed, but some.other.property was set correctly.

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.

Note: I would like this to work so that I can specify the location of native libraries used in a unit test.


回答1:


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 -Dsome.other.property=commandlinedefinedpath

the output will no longer show

[echo] some.other.property=test1




回答2:


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.




回答3:


I think this is not possible, mainly because the JVM has already started by the time this value is modified.

You can however try to start a new process with the correct env variables ( see exec or 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.




回答4:


If you really want to change a property, you can do this in a Java task or in a scripting language.

Here is an example using 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.



来源:https://stackoverflow.com/questions/422848/ant-how-to-modify-java-library-path-in-a-buildfile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!