如何设置Groovy Antbuilder的build.xml?

时间:2010-05-05 15:09:23

标签: ant groovy maven

我想使用GMaven(Maven插件在POM中内联执行Groovy)执行build.xml(Ant构建文件)。因为我不得不使用maven-antrun-plugin多次执行构建文件。构建文件正在发展并从合作伙伴项目中获取,几乎没有变化。这就是为什么我不想将逻辑放入另一个环境(groovy代码或Maven配置)。

我从xml文件中获取了一个属性列表(环境和机器名称,数量和名称可能有很大不同,我不介意这些不能放入模块中),并希望为每台机器执行ant构建。我在javadocs中找到了executeTarget方法,但没有在如何设置构建文件的位置。我怎么能这样做 - 这就够了吗?

我的看法如下:

<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>

2 个答案:

答案 0 :(得分:2)

解决方案是使用org.apache.tools.ant中的Project和ProjectHelper类(省略我的任务中的其他细节):

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');

这里缺少的是日志记录。我用了

antProject.addBuildListener(antListener)

添加一个自定义的侦听器,就像你找到它here - 那是我在一段时间后得到解决方案的地方......

答案 1 :(得分:0)

您可以直接使用maven-antrun-plugin

如果你因为循环而这样做,也许每台机器可以有一个模块,并在父pom中使用<pluginManagement>部分,以避免重复maven-antrun-plugin配置。