tags:

views:

102

answers:

2

I want to execute a build.xml (Ant buildfile) from using GMaven (Maven Plugin for inline execution of Groovy in a POM). Since I have to execute the buildfile several times using the maven-antrun-plugin is not an option. The buildfile is evolving and taken from a partner project with little changes. That's why I don't want to put the logic in it into another environment (groovy code or Maven configuration).

I take a list of properties (environment and machine names that can differ widely in number and names, to my mind this cannot be put into modules) from an xml file and want to execute ant builds for each of those machines. I found the executeTarget method in the javadocs but not how to set the location of the buildfile. How can I do that - and is this enough?

What I have looks as follows:

<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
      <execution>
        <id>some ant builds</id>
        <phase>process-sources</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <source>
            def ant = new AntBuilder()
            def machines = new XmlParser().parse(new File(project.build.outputDirectory + '/MachineList.xml'));

            machines.children().each { 

             log.info('Creating machine description for ' + it.Id.text() + ' / ' + it.Environment.text());
                ant.project.setProperty('Environment',it.Environment.text());
                ant.project.setProperty('Machine',it.Id.text());

                // What's missing?                    

                ant.project.executeTarget('Tailoring');

            }

            log.info('Ant has finished.')

          </source>
        </configuration>
      </execution>
    </executions>
  </plugin>
A: 

You could directly use maven-antrun-plugin.

If you did that because of the loop, maybe you can have one module per machine, and use a <pluginManagement> section in the parent pom to avoid duplication of the maven-antrun-plugin configuration.

Julien Nicoulaud
This is not an option the machines and environments differ too much to put them into modules. Thanks for you answer anyway!
Jan
A: 

A solution is using the Project and ProjectHelper Classes from org.apache.tools.ant like this (omitting the other details from my task):

import org.apache.tools.ant.Project
import org.apache.tools.ant.ProjectHelper

def antFile = new File('src/main/ant/myBuildfile.xml')

//create a project with the buildfile
def antProject = new Project()
antProject.init()
ProjectHelper.projectHelper.configureProject(antProject, antFile)

// run it with ant and set the target
antProject.executeTarget('myTarget');

What's missing here is the logging. I used

antProject.addBuildListener(antListener)

to add a customized listener like you find it here - that's from where I got the soluting after some time...

Jan