如何设置从Java任务的Java库路径?(How do I set the Java library

2019-07-30 12:58发布

是否可以指定一个java任务库路径? 像相当于:

java -Djava.library.path=somedir Whatever

Answer 1:

<propertyset><syspropertyset>应该是你正在寻找

又见这个线程的实例。


你可以将它们设置一个你的java Ant任务中的一个:

<sysproperty key="test.classes.dir" 
             value="${build.classes.dir}"/> 

乏味的......或者你可以传递下来的Ant属性块:

<syspropertyset> 
    <propertyref prefix="test."/> 
</syspropertyset> 

您可以参考外部系统的性能:

<propertyset id="proxy.settings"> 
    <propertyref prefix="http."/> 
    <propertyref prefix="https."/> 
    <propertyref prefix="socks."/> 
</propertyset> 

然后你的java Ant任务中使用它们:此propertyset可按需使用; 当传递到一个新的进程,符合给定的前缀,目前所有ant属性流传下来:

<java>
     <!--copy all proxy settings from the running JVM--> 
     <syspropertyset refid="proxy.settings"/> 
     ...
</java>

我完全错过了你试图通过事实java.library.path属性!
正如提到这个线程 :

如果您尝试设置java任务之外的价值,蚂蚁会忽略它。 所以,我把所有的属性,除了一个在我的syspropertyset,它按预期工作。

含义:

<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>

将无法正常工作,但下面应该:

<target name="debug">
    <java>
        <sysproperty key="java.library.path" path="${dist}"/>
    </java>
</target>

(虽然你可能会尝试用“ fork ”属性设置为true,如果它不工作)
(注意: 不能,虽然修改它的值 )



Answer 2:

针对JUnit Ant任务设置您java.library.path在第<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>

ant手册,页JUnit的 ,部<sysproperty>更多细节。


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

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.设置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>

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

搜寻这样一行[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.
    [...]


Answer 3:

我已经成功地得到它使用环境变量工作ANT_OPTS 。 我想看到这个从任务完成,如果这是可能的。



文章来源: How do I set the Java library path from a Java task?
标签: java ant