簡體   English   中英

如何從Java任務設置Java庫路徑?

[英]How do I set the Java library path from a Java task?

是否可以在java任務中指定庫路徑? 像相當於:

java -Djava.library.path=somedir Whatever

<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,如果它不起作用)
(注意:你不能修改它的值

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

有關詳細信息,請參見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

在你的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使其工作。 如果有可能,我希望從任務中看到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM