2

Background: I have a build process that compiles java code and builds two jars using the compiled classes (each jar a different subset, with some classes appearing in both).
Now, I was required that the users to be able to build each jar separately, but also be able to build them together in parallel (since it cuts the total build time by a lot).
So I came up with a script structured like this:

<project> 
    <target name="compile">
        <sequential>
            <echo message="compile"/>
        </sequential>
    </target>
    <target name="jar1" depends="compile">
        <sequential>
            <echo message="jar1"/>
        </sequential>
    </target>
    <target name="jar2" depends="compile">
        <sequential>
            <echo message="jar2"/>
        </sequential>
    </target>
    <target name="all-jars">
        <parallel>
            <antcall target="jar1"/>
            <antcall target="jar2"/>
        </parallel>
    </target>
</project>

The problem is that, when using the "all-jars" target, the "compile" target gets executed twice. Is there any standard way to prevent it? Better yet, is there an alternative to using antcall?

P.S. I found a similar question here but no answer was given.

Community
  • 1
  • 1
yossiz74
  • 889
  • 1
  • 9
  • 16
  • 1
    You're trying to solve a non-trivial problem. What about interdependencies between jars? (Build of one jar depends on compiled classes of another) These cannot be run in parallel. For more information on how dependency management should work see: http://stackoverflow.com/questions/25114699/how-to-import-properties-and-targets-from-ant-build-file-properly/25116954#25116954 – Mark O'Connor Mar 29 '16 at 09:45
  • 1
    can't you add a dependency to `compile` target in the `all-jars` target ? Since `compile` target is invoking java compiler and that this latter take care of compiling only classes that are not up-to-date, it could prevent to redoing compilation in subsequent calls to `jar1`and `jar2` targets (`compile` target will still be called from `jar1` and `jar2` but will immediately ended since there is no further job to do). – P.A. Cros Mar 30 '16 at 07:24
  • @P.A.Cros Yes, I actually did that shortly after posting this, otherwise you get unpredictable results. That's what I've been using since, and while it works, it still seems "wrong". – yossiz74 Mar 30 '16 at 08:43
  • @MarkO'Connor I realize that the root cause is bad structure of the code and deployment, but I was still hoping there is some way to have ant running each task only once (something like the opening #ifdef statement of virtually any C++ h files) – yossiz74 Mar 30 '16 at 08:45

1 Answers1

4

Simply use unless property in the target "compile", as :

...
<target name="compile" unless="compileDone">
    <property name="compileDone" value="true"/>
    <sequential>
        <echo message="compile"/>
    </sequential>
</target>
...

And your target will be executed only once :o)

Dominic
  • 104
  • 1
  • 4
  • Thanks for the tip. It didn't work for me, since I'm using 'antcall' which re-parses the entire file and resets the properties and dependencies (even when using inheritAll=true). I ended up adding the 'compile' as a dependency to 'all-jars'. – yossiz74 Oct 27 '19 at 09:28