Jar creation for java stand alone application using ant

Introduction to the ant:
  • Ant means another neat tool.
  • It is a Java-based build tool from Apache Software Foundation.
  • Ant file is written in XML file .when run the ant its search for the build.xml file.
  • It is used to automate the build and deployment process.
  • It basically compiles the code, package the code deploying the binaries into the server.
Ant Flow

Problem with IDE'S:
  1. For example, the client asked a create a jar for sending an email of user information to one particular person . in this scenario you need to a create sample java standalone application and create a sample java class for send email.
  2. Send an email using the Gmail SMTP you need to add the external libraries activation.jar and java-mail-1.4.4.jar after adding the jars and build the project you will ger jar file in dist folder in-case if you are using net-beans. 
  3. By default net-beans not adding the external libraries into the final component(jar), once you extract the jar file.
For this type of problem, you need to add some ant snippet for includes the external libraries into the final jar file located on the dist folder.

Here is the ant file, you need to add the file in build.xml before the </project> tag on your project.
 <target name="-post-jar">

     <property name="store.jar.name" value="Javamail"/>

     <property name="store.dir" value="dist"/>

     <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>

     <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>

     <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">

         <zipgroupfileset dir="dist" includes="*.jar"/>

         <zipgroupfileset dir="dist/lib" includes="*.jar"/>

         <manifest>

             <attribute name="Main-Class" value="${main.class}"/>

         </manifest>

     </jar>

     <zip destfile="${store.jar}">

         <zipfileset src="${store.dir}/temp_final.jar"

         excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>

     </zip>

     <delete file="${store.dir}/temp_final.jar"/>

 </target>

Here is the Demo for Creating the jar file with external libraries using ant



SUBSCRIBE TO OUR NEWSLETTER

I’m the Founder of quickdevops.com. I am a Professional Blogger, Application developer, YouTuber. I’ve been blogging since 2015.I spend a lot of time learning new techniques and actively help other people learn web development through a variety of help groups and writing web development tutorials for my website and blog about advancements in web design and development.Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie or planning a trip to someplace I've never been before.

0 Response to "Jar creation for java stand alone application using ant"

Post a Comment