如何从Java任务设置Java库路径?

是否可以在java任务中指定库路径? 就像相当于:

  java -Djava.library.path = somedir无论如何 

应该是您正在寻找的

例如,请参见此线程 。


您可以在java ant任务中逐个设置它们:

  

乏味……或者你可以将它们作为一组Ant属性传递下去:

    

您可以引用外部系统属性:

      

然后在你的java ant任务中使用它们:这个propertyset可以按需使用; 传递给新进程时,传递与给定前缀匹配的所有当前ant属性:

    ...  

我完全错过了你试图传递java.library.path属性的事实!
正如这个post中提到的:

如果你试图在java任务之外设置它的值,Ant会忽略它。 所以我把除了那个之外的所有属性放在我的syspropertyset中,它按预期工作。

含义:

          

不起作用,但以下应该:

      

(尽管你可以尝试将“ fork ”属性设置为true,如果它不起作用)
(注意:你不能修改它的值 )

对于JUnit ant任务,部分设置java.library.path

       

有关详细信息,请参见ant手册,页面JUnit , 部分。


这个答案的其余部分是初学者的细节。

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.在ant脚本中设置java.library.path

                        

3.使用ant选项-v检查java.library.path

在你的ant输出中搜索[junit] '-Djava.library.path=之类的行来检查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. [...] 

我已经设法使用环境变量ANT_OPTS使其工作。 如果有可能的话,我希望从任务中看到这一点。