Ant with Jar Target

Code4Fun :

I need help, to understand the problem of jar packaging with ant. Here my simple code:

  <target name="build" depends="compile,test">
    <jar 
        basedir="${bin}/swing.gui"
        destfile="${mod}/swing.gui.jar">
      <manifest>
        <attribute name="Main-Class" value="swing.main.Main"/>
      </manifest>
    </jar>
  </target>

The little program is modularized and as you can see the module swing.gui should be packaged in a jar file. The problem here, when I try to start the module with:

java --module-path bin/mod --module swing.gui

It does not work. The error message says that there is no MainClass attribute and I should try -m / instead. When I execute this line on the console:

jar --create --file=bin/mod/swing.gui.jar --main-class=swing.main.Main -C bin/src/swing.gui .

It just works! Is that a bug in ant?

VGR :

Not a bug, exactly. Ant is just doing what you told it to do.

The Main-Class manifest attribute is not used by module loaders. The --main-class option of jar has nothing to do with manifests; it sets a binary class attribute on the jar’s module-info.class entry.

So, your Ant build file is doing something entirely different from what your jar command is doing.

Until Ant’s <jar> task has direct support for this, the workaround is to call the JDK’s jar command directly. Something like this:

<condition property="exec-suffix" value=".exe" else="">
    <os family="windows"/>
</condition>

<property name="jar.tool"
          location="${java.home}/bin/jar${exec-suffix}"/>

<exec executable="${jar.tool}" failonerror="true">
    <arg value="-u"/>
    <arg value="-f"/>
    <arg file="${mod}/swing.gui.jar"/>
    <arg value="-e"/>
    <arg value="swing.main.Main"/>
</exec>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=3696&siteId=1
ANT
ANT
jar