Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Ant: How to include dependent project and splitting jar into two jars?

2c295d7c-8e39-4f65-9be3-47f203291e68Mar 23 2016 — edited Mar 23 2016

Hello everybody

It is my first time I'm writing here, so I'm hoping that I'm making everything right.

I have a Java application, let's call it A. This application depends on another project B (required project on the build path). The project structure looks as follows:

CommonFolder/Project B

     -- src

     -- lib

     build.xml

CommonFolder/subfolder/Project A

     -- src

     -- lib

     -- config

     build.xml

The build.xml in Project B works well and create a jar file in a dist folder. Now in Project A the build.xml file should first call build.xml from Project B and then it should create the jar file. Somehow this does not work. If I just copy the jar file from Project B into the lib folder of Project A it works but I don't want to do this manually.

My second problem is that the final jar file of project A will be pretty big because there are a lot of libraries in the lib folder. I would like to split the jar file into two files: libraries.jar (also containing the dependency from project B) and application.jar. When application.jar is started it should use libraries.jar (both jars in the same folder). The benefit is that I only have to upload application.jar which is much smaller (if I don't change the libraries).


How can this be done?

I'm looking forward for the answer.

Here are both build.xml files.

ProjectB

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<project default="complete build" name="Create runnable jar file">

    <property name="src.dir" location="src" />

    <property name="lib.dir" location="lib" />

    <property name="build.dir" location="bin" />

    <property name="dist.dir" location="dist" />

    <property name="build.sysclasspath" value="last" />

   

    <path id="project-classpath">

        <fileset dir="${lib.dir}" includes="*.jar" />

    </path>

    <target name="remove release">

        <delete dir="${dist.dir}" />

    </target>

    <target name="clean build">

        <delete dir="${build.dir}" />

    </target>

    <target name="compile" depends="clean build">

        <mkdir dir="${build.dir}" />

        <javac srcdir="${src.dir}" destdir="${build.dir}" debug="on" target="1.8" source="1.8">

            <classpath refid="project-classpath" />

            <compilerarg value="-Xlint:none" />

        </javac>

    </target>

    <target name="build jar" depends="remove release, compile">

        <mkdir dir="${dist.dir}" />

        <jar destfile="${dist.dir}/projectA.jar">

            <fileset dir="${build.dir}" />

            <zipgroupfileset dir="lib" excludes="META-INF/**" includes="*.jar"/>

        </jar>

        <antcall target="clean build" />

    </target>

    <target name="complete build" depends="build jar, clean build" />

</project>

ProjectA

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<project default="complete build" name="Create runnable jar file">

    <property name="src.dir" location="src" />

    <property name="lib.dir" location="lib" />

    <property name="build.dir" location="bin" />

    <property name="dist.dir" location="dist" />

    <property name="docs.dir" location="docs" />

    <property name="config.dir" location="config" />

    <property name="build.sysclasspath" value="last" />

   

    <path id="project-classpath">

        <fileset dir="${lib.dir}" includes="*.jar" />

    </path>

    <target name="remove release">

        <delete dir="${dist.dir}" />

    </target>

    <target name="clean build">

        <delete dir="${build.dir}" />

    </target>

   

    <target name="clean docs">

        <delete dir="${docs.dir}" />

    </target>

   

    <target name="build-deps">

        <ant antfile="../../ProjectB/build.xml" target="complete build"/>

    </target>

    <target name="compile" depends="clean build, build-deps">

        <mkdir dir="${build.dir}" />

        <javac srcdir="${src.dir}" destdir="${build.dir}" debug="on" target="1.8" source="1.8">

            <classpath refid="project-classpath" />

            <compilerarg value="-Xlint:none" />

        </javac>

    </target>

   

    <target name="docs" depends="clean docs, compile">

        <mkdir dir="${docs.dir}" />

        <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">

            <fileset dir="${src.dir}">

                <include name="**" />

            </fileset>

        </javadoc>

    </target>

    <target name="build jar" depends="remove release, compile">

        <mkdir dir="${dist.dir}" />

        <jar destfile="${dist.dir}/ProjectA.jar">

            <manifest>

                <attribute name="Main-Class" value="something.main.Main" />

                <attribute name="Class-Path" value="." />

            </manifest>

            <fileset dir="${build.dir}" />

            <zipfileset dir="config" />

            <zipgroupfileset dir="lib" excludes="META-INF/**" includes="*.jar"/>

        </jar>

        <antcall target="clean build" />

    </target>

    <target name="complete build" depends="docs, build jar, clean build" />

</project>

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2016
Added on Mar 23 2016
0 comments
3,374 views