javaxmlvelocityproperties-fileantbuilder

Problems with Ant build.xml configuration to work with external Libraries and Java property files


I have a problem with Ant Build Tool. First, below you can see my project structure:

Project Structure

and the content of my build.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<project name="addonGenerator" default="main" basedir=".">
<property name="projectName" value="addonGenerator"/>
<property name="src.dir" location="src"/>
<property name="build.dir" location="bin"/>
<property name="dist.dir" location="dist"/>

<target name="compile" description="compile the source ">
        <mkdir dir="${build.dir}"/>
        <javac srcdir="${src.dir}" destdir="${build.dir}">
            <classpath>
                <pathelement path="lib/velocity-1.7.jar"/>
                <pathelement path="lib/log4j-1.2.16.jar"/>
            </classpath>
        </javac>
</target>

<target name="dist" description="package, output to JAR">
    <mkdir dir="${dist.dir}"/>
    <jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
        <zipgroupfileset dir="lib" includes="velocity-1.7.jar" />
        <zipgroupfileset dir="lib" includes="log4j-1.2.16.jar" />
        <manifest>
            <attribute name="${projectName}" value="main"/>
            <attribute name="Main-Class" value="main.java.AddonGenerator"/>
        </manifest>
    </jar>
</target>

<target name="clean" description="clean up">
    <delete dir="${build.dir}"/>
    <delete dir="${dist.dir}"/>
</target>

    <target name="main"  depends="clean, compile, dist"/>
</project>

I don't know how setup the Ant build.xml to build and run my project with external libraries and the java property file generator.properties


Solution

  • To include your generator.properties file in the .jar file, add your resources directory when building the .jar:

    <jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
        <fileset dir="src/main/java/resources"/>
    

    Since you are currently building a “fat jar” (by directly including the contents of your library .jars in your application .jar), you can run by simply invoking your .jar file. Such a target obviously requires the .jar file to be built, so it makes sense to depend on the "dist" target:

    <target name="run" depends="dist">
        <java jar="${dist.dir}/${projectName}.jar"/>
    </target>
    

    On another note, I don’t think you want to pass src as your source directory, unless your classes actually declare themselves with ‘package main.java;’ (which they shouldn’t). You should pass the actual root of your packages to the javac task:

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

    You should also make the "dist" target depend on "compile", since, well, it depends on having compiled classes available.

    I also would suggest that your default target, "main", avoid calling the "clean" target. You should not clean before every single build; that defeats one of the most useful benefits of Ant, namely the ability to update only the things that need to be updated. You should only clean when you need to, with a command like ant clean compile or simply ant clean.

    Note that once "dist" depends on "compile", and once "main" no longer calls "clean", you can simply remove the "main" target and change your project’s default target to "dist". When you think about it, this makes sense: the default action is to build and package the application.