在maven上运行Ant任务:复制,删除项目中的文件

时间:2016-07-12 08:49:30

标签: maven ant maven-antrun-plugin

我有一个deploy.ant文件,其中整个构建过程已完成,包括复制文件,执行protobuf编译器等等。现在我正在切换到maven,我有pom.xml个文件应该执行这些任务。我几乎完成了所有工作,除了我留下的deploy.ant脚本。

此脚本具有清理,准备,构建和部署目标的功能。我需要的是清理,准备和仅构建目标的某些部分,因为其他部分由其他pom.xml完成。

所以在我的deploy.ant我有以下代码片段:

<!-- 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}" />

这是我提出的maven部分:

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

但正如您所看到的,我对所有这些路径以及如何将它们集成到maven中非常困惑。我怀疑我分享的代码是正确的,因为maven不知道"${install.dir}"是什么。

所以我的问题是,如何使用这个插件在maven中集成这些任务?它只是将ant片段复制粘贴到插件的标签中吗?如果是这样,它将如何理解蚂蚁属性的含义;路径?

1 个答案:

答案 0 :(得分:3)

您似乎正在尝试将一些文件组合成一个有意义的包,可用作目录。

显然,有一个maven插件:maven-assembly-plugin

为了实现您的目标,您首先必须在您的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>

然后你显然必须创建install.xml程序集文件,符合assembly schema

这样的事情应该可以解决问题

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

嗯,这个程序集还不完整,但我想你会明白这个想法。