How to create a JAR using Eclipse and use it in one of your projects

This post shows how to create a jar containing resources for other projects withing eclipse in particular. It will surely work with java projects developed in other IDEs but I specifically mention something extra regarding this situation.

It's assumed that you have at least some idea how Apache Ant. And of course have it installed on your machine.

1. Create a java project.
2. Have your resources (i.e. classes, files...etc) created and built.
3. Add a build.xml file in your project folder containing the following text:
<project name="ipKaizenCore" basedir="." default="clean-build">
    <property name="src.dir"     value="src">
    <property name="classes.dir" value="bin">
    <property name="jar.dir"     value="build">
    <target name="clean">
        <delete dir="${jar.dir}">
    </target>
    <target name="compile">
        <mkdir dir="${classes.dir}">
        <javac srcdir="${src.dir}" destdir="${classes.dir}">
    </target>
    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}">
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
    </target>
    <target name="clean-build" depends="clean,jar">
</project>

Obviously the "projectName" will be the JAR's name. You should be able to figure out how this file should work.

4. Now we need to generate an updated jar file for every change we do withing our project. Here is how to do it. Before we proceed, I specified a bulider name "JarBuilder" then I lost it in one of the images so if you notice that just ignore it, it's my mistake.
  • First we need to configure a builder. It's usually better to have it located at the bottom of the builders queue to package your compiled classes after they are ready. In my situation I had a builder configured to modify my compiled classes so I had to build my jar after this builder is done.
 
  •  Then we point to the location of the build.xml file. It should be located in your project's folder. And specify a builder name if needed.

  • Afterwords we need to specify an Auto Build target. Set a target and it should select the default target name by default which is fine in our case.


  • Then we need to refresh our projects\resources that uses this jar to sense the changes. Switch to the "Refresh" tab and specify your projects that refer to that jar.


To this point we should be done. Update your jar project and your other projects should be able to sense the changes right away.

Comments

Popular posts from this blog

Testing SOAP services using pingdom