【问题标题】:Running Ant task on maven: copy, delete files within the projects在 maven 上运行 Ant 任务:复制、删除项目中的文件
【发布时间】:2016-07-12 08:49:29
【问题描述】:

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

此脚本具有清理、准备、构建和部署目标。从这里我需要的是清洁、准备和只是构建目标的一部分,因为其他部分由其他 pom.xmls 完成。

所以在我的deploy.ant 中,我有以下代码 sn-ps:

<!-- 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 sn-ps 复制粘贴到插件的标签中?如果是这样,它将如何理解蚂蚁属性的含义?路径?

【问题讨论】:

    标签: maven ant maven-antrun-plugin


    【解决方案1】:

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

    显然,有一个 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>  
    

    好吧,这个组装并不完整,但我想你会理解这个想法。

    【讨论】:

    • 构建过程应该通过其他方式完成,我想要的只是将文件复制/粘贴到相关的项目文件夹中。我还需要这个“组装”插件吗?
    • 事实上,它会将您的分发文件夹的制作方式与调用它的那一刻分开。使用 maven 时,我经常发现它比使用 maven-ant-plugin 要好得多。所以,是的,你需要它,只是为了澄清你的构建。
    • 这有一些问题。首先,我需要它只通过install 阶段,这可能吗?另外,这可以处理 if-else 条件吗?
    • 不过我接受这个答案,因为它确实解决了问题。
    • maven 能识别像 ${template.dir} 这样的属性吗?我需要一直在那里写实际组件的名称吗?如果是这样,这并不方便。因为我有大量的组件并且我不能为每个组件复制粘贴相同的代码 sn-p,所以我最终会为此编写数千行 maven 代码,这是不专业的。我需要使用${component.dir}${component.release.dir} 等属性。我应该能够使用for-each 之类的循环。你能确认这个插件是否可行吗?
    猜你喜欢
    • 2011-03-31
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2012-04-12
    相关资源
    最近更新 更多