Search code examples
mavenantmaven-antrun-plugin

Running Ant task on maven: copy, delete files within the projects


I had a deploy.ant file in which the whole build process was done, including copying files, executing the protobuf compiler and all that. Now that I am switching to maven, I have pom.xml files which are supposed to do these tasks. I am done almost all, except the deploy.ant script which I am left with.

This script has cleaning, preparing, building and deploying targets. What I need from here is cleaning, preparing and only some part of the building target, because the other parts are done by the other pom.xmls.

So in my deploy.ant I have the following code snippets:

<!-- just copying sh and ant files to release folder. -->
    <target name="clean">
        <delete dir="${install.dir}" />
    </target>

    <target name="prepare">
        <mkdir dir="${install.dir}" />
        <copy file="${template.dir}/run.all.ant" todir="${install.dir}" />
        <copy file="${template.dir}/run.all.sh" todir="${install.dir}" />
        <copy file="${template.dir}/kill.all.sh" todir="${install.dir}" />
        <copy file="${template.dir}/packLogs.sh" todir="${install.dir}" />
    </target>
    .
    .
    .
    .
<!-- copy template files and configurations -->
        <if>
            <available file="${component.project.dir}/conf/config.properties" />
            <then>
                <copy file="${component.project.dir}/conf/config.properties" todir="${component.release.dir}/conf" />
            </then>
            <else>
                <echo message="${component.project.dir} does not provide a bundle config - using default from release bundle" />
                <copy file="${template.dir}/config.properties" todir="${component.release.dir}/conf" />
            </else>
        </if>
        <if>
            <available file="${component.project.dir}/plans" type="dir" />
            <then>
                <copy todir="${component.release.dir}/plans">
                    <fileset dir="${component.project.dir}/plans" includes="**" />
                </copy>
            </then>
        </if> 
         .
         .
          <copy file="../group1/someProject.mw/conf/log4j.xml" todir="${component.release.dir}/conf" />
            <copy file="${template.dir}/run.ant" todir="${component.release.dir}" />
            <copy file="${template.dir}/run.sh" todir="${component.release.dir}" />

and this is the maven part I've come up with:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo>Copying .sh and .ant files to release folder</echo>
                    <basename file="${component.project.dir}" property="component.name" />
                    <property name="component.release.dir" value="${install.dir}/${component.name}" />
                    <delete dir="${install.dir}" />
                    <mkdir dir="${install.dir}" />
                    <copy file="${template.dir}/run.all.ant" todir="${install.dir}" />
                    <copy file="${template.dir}/run.all.sh" todir="${install.dir}" />
                    <copy file="${template.dir}/kill.all.sh" todir="${install.dir}" />
                    <copy file="${template.dir}/packLogs.sh" todir="${install.dir}" />
                    <copy file="../group1/someProject.mw/conf/log4j.xml" todir="${component.release.dir}/conf" />
                    <copy file="${template.dir}/run.ant" todir="${component.release.dir}" />
                    <copy file="${template.dir}/run.sh" todir="${component.release.dir}" />
                    </tasks>
                </configuration>
            </execution>
        </executions>
    </plugin>

But as you can see I am quite confused with all these paths and how to integrate them in maven. I suspect the code I shared is correct, as maven would have no idea about what the "${install.dir}" is.

So my question is, how to integrate such tasks in maven using this plugin? Is it simply copy-pasting the ant snippets into the tags of the plugin? If so, how will it ever understand what is meant by the ant properties; paths?


Solution

  • It seems you are trying to assemble some files into a meaningfull package, available as a directory.

    Obviously, there is a maven plugin for that : maven-assembly-plugin.

    To achieve your goal, you will first have to reference that plugin in your pom :

        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.6</version>
          <executions>
            <execution>
              <id>make-install-assembly</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
              <inherited>true</inherited>
              <configuration>
                <descriptors>
                  <descriptor>src/main/assemblies/install.xml</descriptor>
                </descriptors>
                <appendAssemblyId>false</appendAssemblyId>
                <attach>true</attach>
              </configuration>
            </execution>
          </executions>
        </plugin>
    

    Then you'll obviously have to create the install.xml assembly file, conforming to assembly schema.

    Something like that should do the trick

    <assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
        <id>install</id>
        <formats>
            <format>dir</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
    
        <fileSets>
            <fileSet>
                <directory>${template.dir}</directory>
                <outputDirectory>${install.dir}</outputDirectory>
                <lineEnding>unix</lineEnding>
                <filtered>true</filtered>
                <includes>
                    <include>run.all.ant</include>
                    <include>run.all.sh</include>
                </includes>
                <excludes>
                    <exclude>META-INF/**</exclude>
                </excludes>
            </fileSet>
        </fileSets>
    </assembly>  
    

    Well, this assembly is not complete, but I guess you'll understand the idea.