简体   繁体   中英

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

Is it possible to specify a library path in a java task? Like the equivalent of:

java -Djava.library.path=somedir Whatever

<propertyset> and <syspropertyset> should be what you are looking for

See also this thread for instance.


You can set them one by one within your java ant task:

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

tedious... or you can pass them down as a block of Ant properties:

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

You can reference external system properties:

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

and then use them within your java ant task: This propertyset can be used on demand; when passed down to a new process, all current ant properties that match the given prefixes are passed down:

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

I completely missed the fact you were trying to pass java.library.path property!
As mentioned in this thread :

if you try to set its value outside of the java task, Ant ignores it. So I put all properties except for that one in my syspropertyset and it works as expected.

meaning:

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

will not work, but the following should:

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

(although you might try that with the " fork " attribute set to true if it does not work)
(Note: you cannot modify its value though )

For JUnit ant task set your java.library.path in section <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>

See ant manual, page JUnit , section <sysproperty> for more details.


The rest of this answer are details for beginners.

1. Load your library in your

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. Set the java.library.path in your script

<?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. Use ant option -v to check java.library.path

Search a line like [junit] '-Djava.library.path= within your ant output to check the presence and value of java.library.path . The expression [...] represent text that has been removed for clarity.

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

I've managed to get it to work using the environment variable ANT_OPTS . I would like to see this done from a task if it's possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM