Search code examples
groovyantpom.xml

how does the ant use in groovy


this is a groovy script file MavenInit.groovy, execute in a pom.xml as follow, the var 'SCRIPTS_GROOVY' is defined in another pom.xml,what confuse me is I can not find where the "DIR_TARGET" definition, and how can use ant direct without ant = new AntBuilder()

SCRIPTS_GROOVY=project.properties['SCRIPTS_GROOVY']
ant.pathconvert(targetos:"unix", property:"DIR_TARGET") {
  path(location:project.build.directory)
}
DIR_TARGET=ant.project.properties['DIR_TARGET']
project.properties.setProperty('DIR_TARGET', "${DIR_TARGET}")
project.properties.setProperty('DIR_LOGS', "${DIR_TARGET}/logs")
ant.mkdir(dir:"${DIR_TARGET}/logs")

the pom.xml excute the MavenInit.groovy as follow

<plugin>
    <groupId>org.codehaus.gmavenplus</groupId>
    <artifactId>gmavenplus-plugin</artifactId>
    <executions>
      <execution>
        <id>set-maven-property</id>
        <phase>initialize</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <scripts>
            <script>file:///${SCRIPTS_GROOVY}/MavenInit.groovy</script>
          </scripts>
       </configuration>
      </execution>
      <execution>
        <id>Windows-package</id>
        <phase>compile</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <scripts>
            <script>file:///${SCRIPTS_GROOVY}/PackageWindows.groovy</script>
          </scripts>
       </configuration>
      </execution>
    </executions>
  </plugin>

how can I find the 'DIR_TARGET' initial defined and change the value


Solution

  • GMavenplus plugin set an ant property into its script

     if (!properties.containsKey("ant")) {
                try {
                    Object antBuilder = invokeConstructor(findConstructor(classWrangler.getClass("groovy.util.AntBuilder")));
                    properties.put("ant", antBuilder);
                }
    

    Also there are other properties like project, log, etc.


    About DIR_TARGET, I think it is the value of project.build.directory with path separator normalized as unix like, from path(location: project.build.directory).

    (I am not an ant expert, but I test on my computer and these two have same value. Just adding the log lines to see

    log.info( project.build.directory)
    DIR_TARGET = ant.project.properties['DIR_TARGET']
    log.info(DIR_TARGET)
    

    I searched the ant doc and find an example from https://ant.apache.org/manual/Tasks/pathconvert.html, which verifies my guess.

      <pathconvert property="prop" dirsep="|">
          <map from="${basedir}/abc/" to=''/>
          <path location="abc/def/ghi"/>
        </pathconvert>