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


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

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

4322
2017-07-21 13:51


起源



答案:


<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任务之外设置它的值,Ant会忽略它。所以我把除了那个之外的所有属性放在我的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)
(注意:你 虽然不能修改它的价值


15
2017-07-21 13:57



它不起作用。 - Geo
我离开了工作。我会在明天尝试一下,如果它有效,我会标记你的答案。谢谢! - Geo
它对我有用......谢谢 - Romain Hippeau


答案:


<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任务之外设置它的值,Ant会忽略它。所以我把除了那个之外的所有属性放在我的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)
(注意:你 虽然不能修改它的价值


15
2017-07-21 13:57



它不起作用。 - Geo
我离开了工作。我会在明天尝试一下,如果它有效,我会标记你的答案。谢谢! - Geo
它对我有用......谢谢 - Romain Hippeau


对于 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.将您的图书馆加载到您的  

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.
    [...]

1
2017-11-14 13:48





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


0
2017-07-21 14:30



我错过了你想要通过的具体财产:我已经更新了我的答案。 - VonC