ant support for java 9 addmods option

2020-07-13 10:15发布

问题:

I am trying to compile my java project with Java 9. I am using the java.xml.bind package so I need to use the -addmods option for compiling. Ant 1.9.7 does not seem to support this new feature. Does ant support -addmods option for Java 9 compiler?

回答1:

There is no explicit support in any released version of Ant at this point in time. But you should be able to use <jvmarg> for that

<java ....>
    <jvmarg value="--add-modules"/>
    <jvmarg value="module.name.to.add"/>
    <jvmarg ..../>
</java>

If you are asking about <javac> rather than <java>, <compilerarg> can be used instead.

There are quite a few ways that Java 9 manages to break Ant - and 1.9.8 and 1.10.x will contain a lot of fixes for it (there will be new releases soon once the last known issues have been ironed out). Right now there is no explicit support for --add-modules, though, only for modulepath and upgrademodulepath which have been added in Ant 1.9.7.

IMHO - Would be a good enhancement request though.



回答2:

Use at least Ant 1.10.1 and do the following in build.xml:

<condition property="java9">
  <equals arg1="${ant.java.version}" arg2="9"/>
</condition>

<java classname="....." fork="true">
  <classpath>
    <pathelement location="...."/>
  </classpath>
  <jvmarg value="--add-modules" if:set="java9" />
  <jvmarg value="java.xml.bind" if:set="java9" />
  ...
</java>


标签: java ant java-9