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

时间:2009-07-21 13:51:29

标签: java ant

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

java -Djava.library.path=somedir Whatever

3 个答案:

答案 0 :(得分:15)

<propertyset> and <syspropertyset>应该是你要找的东西

例如,另请参阅this thread


您可以在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财产的事实! 如this thread中所述:

  

如果您尝试在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,如果它不起作用) (注意:你cannot modify its value though

答案 1 :(得分:1)

对于 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 manual, page 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.
    [...]

答案 2 :(得分:0)

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

相关问题