如何从 Java 任务设置 Java 库路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1159333/
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
How do I set the Java library path from a Java task?
提问by Geo
Is it possible to specify a library path in a java task? Like the equivalent of:
是否可以在 java 任务中指定库路径?相当于:
java -Djava.library.path=somedir Whatever
回答by VonC
<propertyset>and <syspropertyset>should be what you are looking for
<propertyset>并且<syspropertyset>应该是您正在寻找的
See also this threadfor instance.
例如,另请参见此线程。
You can set them one by one within your java ant task:
您可以在您的 java ant 任务中一一设置它们:
<sysproperty key="test.classes.dir"
value="${build.classes.dir}"/>
tedious... or you can pass them down as a block of Ant properties:
乏味...或者您可以将它们作为 Ant 属性块传递:
<syspropertyset>
<propertyref prefix="test."/>
</syspropertyset>
You can reference external system properties:
您可以引用外部系统属性:
<propertyset id="proxy.settings">
<propertyref prefix="http."/>
<propertyref prefix="https."/>
<propertyref prefix="socks."/>
</propertyset>
and then use them within your java ant task: This propertysetcan be used on demand; when passed down to a new process, all current ant properties that match the given prefixes are passed down:
然后在你的 java ant 任务中使用它们:这propertyset可以按需使用;当传递给一个新进程时,所有匹配给定前缀的当前 ant 属性都会被传递:
<java>
<!--copy all proxy settings from the running JVM-->
<syspropertyset refid="proxy.settings"/>
...
</java>
I completely missed the fact you were trying to pass java.library.pathproperty!
As mentioned in this thread:
我完全错过了你试图传递java.library.path财产的事实!
如本主题所述:
if you try to set its value outside of the java task, Ant ignores it. So I put all properties except for that one in my syspropertyset and it works as expected.
如果您尝试在 java 任务之外设置它的值,Ant 会忽略它。所以我把除了那个属性之外的所有属性都放在我的 syspropertyset 中,它按预期工作。
meaning:
意义:
<property name="java.library.path" location="${dist}"/>
<propertyset id="java.props">
<propertyref name="java.library.path"/>
</propertyset>
<target name="debug">
<java>
<syspropertyset refid="java.props"/>
</java>
</target>
will not work, but the following should:
将不起作用,但以下内容应该:
<target name="debug">
<java>
<sysproperty key="java.library.path" path="${dist}"/>
</java>
</target>
(although you might try that with the "fork" attribute set to true if it does not work)
(Note: you cannot modify its value though)
回答by olibre
For JUnit ant taskset your java.library.pathin section <junit>
对于JUnit ant 任务,请设置您的java.library.pathin 部分<junit>
<target name="test" depends="build-test">
<junit printsummary="yes" fork="true">
<sysproperty key="java.library.path"
path="path/where/your/library/is/located"/>
<!-- ... -->
</junit>
</target>
See antmanual, page JUnit, section <sysproperty>for more details.
有关更多详细信息,请参阅ant手册页 JUnit部分<sysproperty>。
The rest of this answer are details for beginners.
这个答案的其余部分是初学者的详细信息。
1. Load your library in your junitfixture
1. 在您的junit装置中加载您的库
public class MyFeatureTest {
@Before
public void load_library_xxxxx() {
System.loadLibrary("library_name_without_extension");
}
@Test
public void on_that_case_my_feature_does_this() {
// ...
}
}
2. Set the java.library.pathin your antscript
2.java.library.path在你的ant脚本中设置
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="build" name="xxxxxx">
<!-- ... -->
<property name="lib_dir" value="path/where/your/library/is/located"/>
<!-- ... -->
<target name="test" depends="build-test">
<mkdir dir="${test_report_dir}" />
<junit printsummary="yes" fork="true">
<sysproperty key="java.library.path" path="${lib_dir}"/>
<classpath>
<pathelement location="${antlr}" />
<!-- ... -->
</classpath>
<formatter type="xml" />
<formatter type="plain" />
<batchtest todir="${test_report_dir}">
<fileset dir="${test_src_dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
</target>
</project>
3. Use antoption -vto check java.library.path
3.使用ant选项-v检查java.library.path
Search a line like [junit] '-Djava.library.path=within your antoutput to check the presence and value of java.library.path. The expression [...]represent text that has been removed for clarity.
[junit] '-Djava.library.path=在ant输出中搜索类似的行以检查 的存在和值java.library.path。该表达式[...]表示为清楚起见已被删除的文本。
> ant test -v
[...]
test:
[mkdir] Skipping /home/user/my/dir/report because it already exists.
[junit] Implicitly adding /usr/share/ant/lib/junit.jar:[...] to CLASSPATH
[junit] Executing '/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java' with arguments:
[junit] '-Djava.library.path=/home/user/my/project/path/where/your/library/is/located'
[junit] '-classpath'
[junit] '/home/user/my/project/external/antlr.jar:[...]'
[junit] 'org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner'
[junit] 'com.example.myproject.myfeature.MyFeatureTest'
[junit] 'skipNonTests=false'
[junit] 'filtertrace=true'
[junit] 'haltOnError=false'
[junit] 'haltOnFailure=false'
[junit] 'formatter=org.apache.tools.ant.taskdefs.optional.junit.SummaryJUnitResultFormatter'
[junit] 'showoutput=false'
[junit] 'outputtoformatters=true'
[junit] 'logfailedtests=true'
[junit] 'threadid=0'
[junit] 'logtestlistenerevents=false'
[junit] 'formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,/home/user/my/dir/report/TEST-com.example.myproject.myfeature.MyFeatureTest.xml'
[junit] 'formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter,/home/user/my/dir/report/TEST-com.example.myproject.myfeature.MyFeatureTest.txt'
[junit] 'crashfile=/home/user/my/project/junitvmwatcher4952613017772370651.properties'
[junit] 'propsfile=/home/user/my/project/junit3999929381398716397.properties'
[junit]
[junit] The ' characters around the executable and arguments are
[junit] not part of the command.
[...]
回答by Geo
I've managed to get it to work using the environment variable ANT_OPTS. I would like to see this done from a task if it's possible.
我已经设法使用环境变量让它工作ANT_OPTS。如果可能的话,我希望看到这是从任务中完成的。

