views:

1211

answers:

2

I need to execute some ant commands depending on an environment variable passed in as a parameter to the maven build command.

At the moment I have 3 tasks blocks and only the tasks block with no condition is being executed.

<tasks name="isProdCheck">
  <condition property="isProd">
    <equals arg1="${environment}" arg2="PROD" />
  </condition>
</tasks>

<tasks if="isProd" depends="isProdCheck">
...
</tasks>

<tasks>
... I am the only block executed
</tasks>

What am I doing wrong, is there a better way to do this?

+2  A: 
Pascal Thivent
Apologies, quite new to Maven. I am using Maven 2.0.9 with JDK 1.4.
reckoner
No problem, this makes more sense. I'll update my answer accordingly.
Pascal Thivent
Thanks for your help Pascal. Your solution above looks like exactly what I was trying to acheive. I couldn't get the 'if' statements working before as I was not aware that i needed to include an ant-contrib resource.Essentially what I am trying to achieve is creating a directory structure on an app server which differs depending on whether I am deploying to a development/testing environment or a production environment.I have just had a quick look at build profiles and it sounds like this would be a way better way of doing things. Thanks!
reckoner
You're welcome, glad you found it helpful.
Pascal Thivent
A: 

Resolved this issue by creating multiple named targets with "if" attributes and a condition property in a build.xml file in the project root as follows.

<target name="prod" if="isProd" depends="isProdCheck">
    // do something
</target>

Passed properties of the command line switches I required, and called the ant targets in the build.xml file from the tasks section in the Maven POM as follows:

<tasks>
 <ant antfile="${basedir}/build.xml">
    <property name="environment" value="${environment}"/>        
    <target name="prod"/>
 </ant>          
</tasks>
reckoner
No offense but this is an horrible way to use maven to solve your problem.
Pascal Thivent
why do you need to call a specific target and than check if you called the right target? Can't you just call the right target from maven depending on the environment (e.g. tasked named according to the environment names)?
Peter Schuetze