tags:

views:

25

answers:

2

Trying to create a grails ant task that has other environments besides prod for the war task.

i've tried

<target name="war" depends="-init-grails" description="--> Creates a WAR of a Grails application">
        <grails script="War" args="grails.env=${env} ${war.filename}"/>
</target>

and

<target name="war" depends="-init-grails" description="--> Creates a WAR of a Grails application">
        <grails script="War" args="-Dgrails.env=${env} ${war.filename}"/>
</target>

Neither of these two work and always just run the production env. Note that I've also hard coded ${env} so it's not that it's missing

+1  A: 

you might want to look at grails scripting to solve your problem

http://www.grails.org/Command+Line+Scripting

it has wrapped ANT so you can utilize ant tasks inside the scripts and I believe you have access to the environment and lots of other "groovy" stuff

i think you can also do this if you are set on using ant, although I havent tried it with grails scripts

<exec executable="grails"  failonerror="true">
    <arg line='-c war grails.env=${env} ${war.filename}' />
</exec>
Aaron Saunders
I've thought of using exec but it has to stay in ant rather than gant since it's part of a much larger j2ee app. We're trying to war the grails project up and include it in a larger .ear which is working great accept for the environment
dstarh
ended up having to go with exec like you said since the official grails ant task doesn't seem to support the custom env's or anything else but prod from what I can see
dstarh
+1  A: 

I use a macrodef for this:

<property environment='env'/>

<property name='grails.home' value='${env.GRAILS_HOME}' />

<condition property='grails' value='grails.bat'>
   <os family='windows'/>
</condition>
<property name='grails' value='grails' />

<macrodef name='grails'>
   <attribute name='action' />
   <attribute name='environment' default='dev' />
   <element name='preargs' optional='true' />
   <element name='args' optional='true' />
   <sequential>
      <exec executable='${grails}' failonerror='true'>
         <preargs />
         <arg value='@{environment}'/>
         <arg value='@{action}'/>
         <args />
      </exec>
   </sequential>
</macrodef>

You can execute simple commands:

<grails action='clean' />

or more complex ones like your war task:

<grails action='war' environment='${env}'>
   <args>
      <arg value="${war.filename}" />
   </args>
</grails>
Burt Beckwith
Hey burt! This still doesn't let me use the grails built in ant tasks with other environments than prod. If it's a bug it should be reported if not then I'd like to know how to do it. But I do like your macro. Might end up going that route if nothing else works.
dstarh
That's weird - I tested this with the above macrodef, condition and property declarations and these targets and it created a war called test.war using the test env: <target name='-init-grails'> <property name='env' value='test'/> <property name='war.filename' value='test.war'/> </target> <target name="war" depends="-init-grails"> <grails action='war' environment='${env}'> <args> <arg value="${war.filename}" /> </args> </grails> </target>
Burt Beckwith
I know the macrodef will work. I was trying to get the built in antTask that's in the grails bootstrap to work without having to fall back to exec
dstarh
It seems that i might have to put the full path to the executable because eclipse (STS) can't find "grails"
dstarh
only change I had to make was moving from <arg value='@{environment}'/> to <arg value='-Dgrails.env=@{environment}'/> to be able to pass in my custom envrionments
dstarh