0

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

5
  • 3
    What task does it fail on? Jul 10, 2013 at 18:23
  • 1
    Can you please be more specific about the failures? What task do you run? What happens? Jul 10, 2013 at 19:11
  • I'm also not clear on what you mean by "classes are duplicated". Jul 10, 2013 at 20:06
  • one of the classes is duplicated MzSystemModel.class and MzSystemModel$1.class then there is MzSystemModel$State.class but this one is for the enum right?
    – vallllll
    Jul 10, 2013 at 20:11
  • 1
    MzSystemModel$1 isn't a duplicate, it's an anonymous inner class (such as a new Runnable() { public void run() { ... } }) Jul 10, 2013 at 22:37

2 Answers 2

3

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.

4
  • thanks for your help David W the only problem is I get error: build.xml:33: path doesn't support the "depends" attribute
    – vallllll
    Jul 10, 2013 at 19:56
  • if I remove the "depends" attribute then one of the classes in jar file is duplicated and then I dont get error on build but when I sue this file in another project it fails
    – vallllll
    Jul 10, 2013 at 20:04
  • @vallllll - Whoops, that's not a target! This <classpath> will be executed before all targets, and if the lib directory isn't there with the required jars, this will fail. We need to put this either into the compile-api target, or make it a target that compile-api depends upon.
    – David W.
    Jul 10, 2013 at 20:45
  • @vallllll I updated my build.xml. The lib.classpath is in its own target. The problem is that you are setting your classpath outside of all targets which means setting the classpath is called first. If there is no lib directory the build will fail either because lib.classpath is empty or because the lib directory doesn't exist and the classpath fails to get created. You have to execute this after -pre-build-api target is called. Alternatives is to place this at the end of -pre-build-api target or in the beginning of compile-api` target.
    – David W.
    Jul 10, 2013 at 20:51
0

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.

3
  • thanks I added that I added this to all my delete scripts but still did not fix the whole problem.
    – vallllll
    Jul 10, 2013 at 17:28
  • Not everyone has gone to Maven. We use Ant. Jul 10, 2013 at 17:48
  • I support both ANT and Maven builds. Would you believe 80% of Java builds (certainly in Enterprises) still use ANT? Part of this is inertia, part of this is legacy and finally there's plain old fear of something new :-) Jul 12, 2013 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.