Creating a bundle jar with ant
In my target, I have something like this:
<jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="dist" includes="*.jar"/>
<zipgroupfileset dir="dist/lib" includes="*.jar" excludes=""/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
<attribute name="Class-Path" value="${mf.classpath}"/>
</manifest>
</jar>
And here is how I build my classpath:
<path id="build.classpath">
<fileset dir="${basedir}/">
<include name="${lib.dir}/*.jar"/>
</fileset>
</path>
<pathconvert property="mf.classpath" pathsep=" ">
<path refid="build.classpath"/>
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="lib/*.jar"/>
</chainedmapper>
</mapper>
</pathconvert>
mf.classpath is used from the package target posted above.
This part I copied from somewhere else, so I'm not all that familiar with it.
Quick edit.
Javac needs to know about those jars too.
<path id="jars">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="compile">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="jars" debug="on"/>
</target>
How can I add bundled jars to my classpath in an Ant build task?
As mentioned by @greg-449, you will have to add all the required jars as part of classpath.
Something like this :
<classpath>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
This is just an example to include multiple (wildcard entries) jars. You can read more about it at Path-like Structures
This is basically a transitive dependency, ie your dependencies have their own dependencies. That is where Maven can be used, which automatically takes care of such dependencies. More on it here : Maven Transitive Dependencies
java build ant file with external jar files
You'll need to combine all the jars together using zipgroupfileset
:
<target name="jar" depends="compile">
<jar destfile="${jar-dir}/${ant.project.name}.jar" basedir="${classes-dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
<zipgroupfileset dir="${lib-dir}" includes="*.jar"/>
</jar>
</target>
See: Creating a bundle jar with ant
Using Ant to build multiple jars
Take a look at subant
task in ant. You can create ant-file which would call other files to.
<subant target="create_jar1">
<fileset dir="." includes="jar2.xml"/>
</subant>
<subant target="create_jar2">
<fileset dir="." includes="jar1.xml"/>
</subant>
How to build jar-file with ant?
I figured it out.
<path id="classpath">
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="init" depends="clean" description="starts">
<tstamp/>
</target>
<target name="clean" depends="package-to-jar" description="clean up">
<delete dir="${classes}"/>
<delete file="${external-lib}"/>
</target>
<target name="package-to-jar" depends="package-external-lib" description="packing all project into a jar-file">
<jar destfile="${jar}/${ant.project.name}.jar" basedir="${classes}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
<zipfileset src="${external-lib}"/>
</jar>
<delete dir="${classes}"/>
<delete file="${external-lib}"/>
</target>
<target name="package-external-lib" depends="compile" description="packing external libraries into a jar-file">
<jar destfile="${external-lib}">
<zipgroupfileset dir="${lib}">
<include name="**/*.jar"/>
</zipgroupfileset>
</jar>
</target>
<target name="compile" description="compile the source">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" classpathref="classpath"/>
<copy todir="${classes}">
<fileset dir="${src}" excludes="**/*.java"/>
</copy>
</target>
ant create jar with source files
try
<jar destfile="${target.dir}/my-app.jar">
<fileset dir="${target.dir}/classes" />
<fileset dir="${src.dir}" includes="**/*.java"/>
</jar>
Creating a bundle jar for javafx application
Thank you jewesea!
I could not make any of those libraries work with JavaFX. But I found this: https://community.oracle.com/message/10266894
so after <fx:jar>
i unpack and repack all libraries and it works.
<target name="do-deploy-bundle" depends="init-properties, do-deploy-dist">
<property name="tmp.file" value="temp_final.jar"/>
<delete file="${dist.dir}/${app.jar}" />
<delete dir="${bundle-dist.dir}"/>
<mkdir dir="${bundle-dist.dir}"/>
<jar destfile="${bundle-dist.dir}/${tmp.file}" filesetmanifest="skip">
<zipgroupfileset dir="${dist.dir}" includes="*.jar" />
<zipgroupfileset dir="${dist.dir}/libs" includes="*.jar" />
<manifest>
<attribute name="Implementation-Vendor" value="${app.vendor}"/>
<attribute name="Implementation-Title" value="${app.name}"/>
<attribute name="Implementation-Version" value="${app.version}"/>
<!--<attribute name="Main-Class" value="com.javafx.main.Main" />-->
<attribute name="Main-Class" value="com.poterion.texovac.application.Main" />
<attribute name="JavaFX-Version" value="2.2" />
<attribute name="JavaFX-Feature-Proxy" value="None"/>
<!--<attribute name="JavaFX-Application-Class" value="com.poterion.texovac.application.Main" />-->
<attribute name="Created-By" value="JavaFX Packager" />
</manifest>
</jar>
<zip destfile="${dist.dir}/${app.jar}">
<zipfileset src="${bundle-dist.dir}/${tmp.file}" excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA , META-INF/maven/**,META-INF/*.txt" />
</zip>
<delete file="${bundle-dist.dir}/${tmp.file}" />
<delete dir="${bundle-dist.dir}"/>
</target>
Related Topics
Docker: Combine Multiple Images
How to Implement Dynamic Gui in Swing
How to Configure Jackson in Wildfly
Debug a Java Application Without Starting the Jvm with Debug Arguments
Size of a Byte in Memory - Java
Jackson Renames Primitive Boolean Field by Removing 'Is'
Java Swing: How to Change Gui Dynamically
How to Get Information About the Local Variables Using Java Reflection
Jfreechart Line Chart with Text at Each Point
Capturing Image from Webcam in Java
How Do Java Interfaces Simulate Multiple Inheritance
Creating a "Logical Exclusive Or" Operator in Java
What Is Difference Between Crudrepository and JPArepository Interfaces in Spring Data JPA
What Are the Differences Between Arraylist and Vector
How to Display Legend for Pie Chart in Columns