Tutorials     About     RSS
Tech and Media Labs

Build Java Modules With Ant

Jakob Jenkov
Last update: 2020-06-24

The Java Platform Module System (JPMS) was included in Java from Java 9. With JPMS came a method for defining something called a Java module. Along with Java modules came a slightly different way to compile, package (in JARs) and link (with jlink) Java toolkits and applications. In this tutorial I will show you how to compile, package and link a modular Java toolkit or application using Ant.

To learn more about the Java Platform Module System, see my tutorial about it:
Java Modules - Java Platform Module System.

Compile Java Module

Here is an Ant target definition that is capable of

This Ant script can compile a Java module using the Java SDK compiler. The Ant script uses the simple Ant task exec to execute the Java compiler as a standard command line executable.

<project name="MyProject" basedir="." default="text">

    <property name="version" value="1.1.1"/>
    <property name="javahome" value="C:\Program Files\Java\jdk-9.0.4\"/>

    <target name="clean">
        <delete dir="out-build"/>
        <mkdir  dir="out-build"/>
        <mkdir  dir="out-build/java"/>
        <mkdir  dir="out-build/java/com.nanosai.memops"/>
    </target>

    <target name="copy-source">
        <copydir src="src/main/java" dest="out-build/java/com.nanosai.memops" />
    </target>


    <target name="compile">
        <exec executable="${javahome}\bin\javac" dir="${basedir}">
            <arg value="-d"/>
            <arg value="out-build/classes"/>
            <arg value="--module-source-path"/>
            <arg value="out-build/java"/>
            <arg value="--module"/>
            <arg value="com.nanosai.memops"/>
        </exec>
    </target>

</project>

Package Java Module

Here is an Ant target definition that is capable of packaging the Java module that was compiled by the script in the previous section. The Ant target creates a modular JAR file with a module descriptor in.

<project name="MyProject" basedir="." default="text">

    <property name="version" value="1.1.1"/>
    <property name="javahome" value="C:\Program Files\Java\jdk-9.0.4\"/>

    <target name="clean">
        <delete dir="out-build"/>
        <mkdir  dir="out-build"/>
        <mkdir  dir="out-build/java"/>
        <mkdir  dir="out-build/java/com.nanosai.memops"/>
    </target>

    <target name="copy-source">
        <copydir src="src/main/java" dest="out-build/java/com.nanosai.memops" />
    </target>


    <target name="compile">
        <exec executable="${javahome}\bin\javac" dir="${basedir}">
            <arg value="-d"/>
            <arg value="out-build/classes"/>
            <arg value="--module-source-path"/>
            <arg value="out-build/java"/>
            <arg value="--module"/>
            <arg value="com.nanosai.memops"/>
        </exec>
    </target>

    <target name="package-jar">
        <exec executable="${javahome}\bin\jar" dir="${basedir}">
            <arg value="-c"/>
            <arg value="--file=out-build/com-nanosai-memops.jar"/>
            <arg value="-C"/>
            <arg value="out-build/classes/com.nanosai.memops"/>
            <arg value="."/>
        </exec>
    </target>

</project>

Jakob Jenkov

Featured Videos

















Sponsored Ads

Maildroppa - Smart Email Marketing Solution
Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next