Oops I Did It Again
I saw some interesting behavior in Maven 1.x today. It winds up being a bug that's already listed, but it still caused some fun for me today. It turns out that if you call a goal in Maven via the attainGoal tag, all of its prereqs will be evaluated again (and again) regardless of whether or not they've ever been called before. I probably didn't make that very clear, but here's an example. Let's say you have this maven.xml:
<project xmlns:ant="jelly:ant" > <goal name="goal0" prereqs="prereq,subGoal1,subGoal2"> <ant:echo>Running "goal0".</ant:echo> </goal> <goal name="goal1" prereqs="prereq"> <ant:echo>Running "goal1".</ant:echo> <attainGoal name="subGoal1"/> <attainGoal name="subGoal2"/> </goal> <goal name="subGoal1" prereqs="prereq"> <ant:echo>Running "subGoal1".</ant:echo> </goal> <goal name="subGoal2" prereqs="prereq"> <ant:echo>Running "subGoal2".</ant:echo> </goal> <goal name="prereq"> <ant:echo>Running the "prereq" goal.</ant:echo> </goal> <preGoal name="prereq"> <ant:echo>Running the preGoal of "prereq".</ant:echo> </preGoal> </project>
Given that maven.xml, running maven goal0 will generate the following output:
build:start: prereq: [echo] Running the preGoal of "prereq". [echo] Running the "prereq" goal. subGoal1: [echo] Running "subGoal1". subGoal2: [echo] Running "subGoal2". goal0: [echo] Running "goal0". BUILD SUCCESSFUL
That's pretty much what I would expect. Each of the preGoals and their prerequisites are called exactly once. However, running maven goal1 generates this:
build:start: prereq: [echo] Running the preGoal of "prereq". [echo] Running the "prereq" goal. goal1: [echo] Running "goal1". prereq: [echo] Running the preGoal of "prereq". [echo] Running the "prereq" goal. subGoal1: [echo] Running "subGoal1". prereq: [echo] Running the preGoal of "prereq". [echo] Running the "prereq" goal. subGoal2: [echo] Running "subGoal2". BUILD SUCCESSFUL
As you can see, the prerequisite and its preGoal are called multiple times by the overall goal and each of the sub-goals. As I said, there has already been a bug opened about this issue, though I have no idea if or when it will ever get resolved. At this point I don't particularly care because knowing is half the battle and, in my case, allows me to implement a workaround. My real world scenario is that I had a preGoal to a goal from another project that was being called 5 or so times. Since I don't like that, I'll just change my preGoal to goal of my own that calls the goal from another project directly as its last step. Then I'll just use my goal directly. Of course, I hadn't noticed this before because my preGoal was relatively fast. As it continued to grow, this problem started to become evident. The cool thing is that now my build is faster. Anyhow, another minor Maven gotcha to watch out for…
Leave a Reply