[英]How do I set the Java library path from a Java task?
Is it possible to specify a library path in a java task? 是否可以在java任务中指定库路径? Like the equivalent of:
像相当于:
java -Djava.library.path=somedir Whatever
<propertyset>
and <syspropertyset>
should be what you are looking for <propertyset>
和<syspropertyset>
应该是您正在寻找的
See also this thread for 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 propertyset
can be used on demand; 然后在你的java ant任务中使用它们:这个
propertyset
可以按需使用; when passed down to a new process, all current ant properties that match the given prefixes are passed down: 传递给新进程时,传递与给定前缀匹配的所有当前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.path
property! 我完全错过了你试图传递
java.library.path
属性的事实!
As mentioned in this thread : 正如这个帖子中提到的:
if you try to set its value outside of the java task, Ant ignores it.
如果你试图在java任务之外设置它的值,Ant会忽略它。 So I put all properties except for that one in my syspropertyset and it works as expected.
所以我把除了那个之外的所有属性放在我的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) (尽管你可以尝试将“
fork
”属性设置为true,如果它不起作用)
(Note: you cannot modify its value though ) (注意:你不能修改它的值 )
For JUnit ant task set your java.library.path
in section <junit>
对于JUnit ant任务,在
<junit>
部分设置java.library.path
<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 ant
manual, page JUnit , section <sysproperty>
for more details. 有关详细信息,请参见
ant
手册,页面JUnit , <sysproperty>
部分。
The rest of this answer are details for beginners. 这个答案的其余部分是初学者的细节。
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() {
// ...
}
}
java.library.path
in your ant script java.library.path
<?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>
ant
option -v
to check java.library.path
ant
选项-v
检查java.library.path
Search a line like [junit] '-Djava.library.path=
within your ant
output to check the presence and value of java.library.path
. 在你的
ant
输出中搜索[junit] '-Djava.library.path=
之类的行来检查java.library.path
的存在和值。 The expression [...]
represent text that has been removed for clarity. 表达式
[...]
表示为清楚起见而删除的文本。
> 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.
[...]
I've managed to get it to work using the environment variable ANT_OPTS
. 我已经设法使用环境变量
ANT_OPTS
使其工作。 I would like to see this done from a task if it's possible. 如果有可能,我希望从任务中看到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.