vallllll
vallllll

Reputation: 2761

Ant build file issue

Hi I have a problem with ant build.xml and it works but sometimes not the first time or works on some computers and others not

so here it is:

<project name="My-java-api" default="dist-api" basedir=".">

<description>
    Java API Buildfile
</description>

<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist"  location="dist"/>
<property name="libs"  location="libs"/>

<!--if we don't remove folders, when we call compile-api
and classes have already been built, it doesn't build again-->

<target name="-init-api" depends="clean"
        description="Create folders libs and build">
    <mkdir dir="${build}"/>
    <mkdir dir="${libs}"/>
    <mkdir dir="${dist}"/>
</target>

 <!-- Here I call another buildfile of submodule located at the tree indicated I am 
 sure the other buildfile works perfect -->

<target name="-pre-build-api" depends="-init-api"
        description="Create jelly jar and copy it to libs folder">
    <ant dir="../Libraries/jelly/core/"
         antfile="build.xml"
         target="standalone-jar"/>
    <copy todir="${libs}">
        <fileset
                dir="../Libraries/jelly/core/dist"
                includes="jelly-standalone*.jar" />
    </copy>
</target>

 <!--so now I create this classpath to use for making jar-->

<path id="lib.classpath">
    <fileset dir="${libs}" includes="**/*.jar"/>
</path>

 <!--I compile source code including the jar that I have just copied to libs-->
<target name="compile-api" depends="-pre-build-api" >
    <javac srcdir="${src}"
           includeantruntime="false"
           destdir="${build}"
           classpathref="lib.classpath">
    </javac>
</target>
<!-- here i make jar with the classes and using the jar from external project,
I want just one jar for everything  -->

<target name="dist-api" depends="compile-api" >

    <jar jarfile="${dist}/name-java-api-0.1.jar" basedir="${build}" >
        <zipgroupfileset dir="${libs}" includes="**/*.jar" />
    </jar>
</target>

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

EDIT: failonerror="false" added to all the delete lines

I am not used to ant and I have been trying and trying and it just doesn't quite work. I wish I could just use command line but can't. Strangely most of the time if run it 2 times it works but the first time really weird staff happen: either doesn't compile, either classes are duplicated. What could be the reason? thanks a lot

Upvotes: 0

Views: 2809

Answers (2)

David W.
David W.

Reputation: 107030

Can you explain why it might fail? A quick look, and I see that your compile-api task is dependent upon your lib.classpath task, but it isn't included in compile-api's dependences. That can cause a build script to work sometimes and not others.

Also, lib.classpath is dependent upon the lib directory being created, so it should also be dependent upon -init-api.

And, you should never ever have anything depend upon clean. Ant builds are setup, so they don't have to execute unnecessary steps. For example, if you change just one source file, only that source file gets recompiled. You break the whole idea of a build script forcing a clean each time a build is done.


Looking a bit closer at your build.xml, I realize there are some other issues.

Here's a build.xml with corrected dependencies.

<project name="My-java-api" default="dist-api" basedir=".">

    <description>
        Java API Buildfile
    </description>

    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="dist"  location="dist"/>
    <property name="libs"  location="libs"/>

    <!--if we don't remove folders, when we call compile-api -->
    <!-- and classes have already been built, it doesn't build again-->

    <target name="-init-api"
        description="Create folders libs and build">
        <mkdir dir="${build}"/>
        <mkdir dir="${libs}"/>
        <mkdir dir="${dist}"/>
    </target>

    <!-- Here I call another buildfile of submodule located at the tree indicated I am -->
    <!--sure the other buildfile works perfect -->

    <target name="-pre-build-api" depends="-init-api"
        description="Create jelly jar and copy it to libs folder">
        <ant dir="../Libraries/jelly/core/"
            antfile="build.xml"
            target="standalone-jar"/>
        <copy todir="${libs}">
            <fileset
                dir="../Libraries/jelly/core/dist"
                includes="jelly-standalone*.jar" />
        </copy>
    </target>

    <!--so now I create this classpath to use for making jar-->

    <target name="lib.classpath"
        depends="-pre-build-api">
        <path id="lib.classpath"
            depends="-pre-build-api">
            <fileset dir="${libs}" includes="**/*.jar"/>
        </path>
    </target>

    <!--I compile source code including the jar that I have just copied to libs-->
    <target name="compile-api"
        depends="lib.classpath" >
        <javac srcdir="${src}"
            includeantruntime="false"
            destdir="${build}"
            classpathref="lib.classpath">
        </javac>
    </target>
    <!-- here i make jar with the classes and using the jar from external project,
    I want just one jar for everything  -->

    <target name="dist-api"
        depends="compile-api" >

        <jar jarfile="${dist}/name-java-api-0.1.jar" basedir="${build}" >
            <zipgroupfileset dir="${libs}" includes="**/*.jar" />
        </jar>
    </target>

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

NOTE: dist-api depends upon compile-api which depends upon lib.classpath which depends upon pre-build-api which _depends upon -init.api. There is nothing dependent upon clean.

Upvotes: 3

Ian Fairman
Ian Fairman

Reputation: 635

I think it's going to complain if the directories are not there, as would be the case the first time you run it. You might want to add a failonerror="false" attribute to the delete tasks.

For future reference, ant is not really used that much anymore for building Java - most people have gone over to maven.

Upvotes: 0

Related Questions