【问题标题】:Maven and Ant plugin to copy resources intelligentlyMaven和Ant插件智能复制资源
【发布时间】:2013-04-19 16:27:55
【问题描述】:

使用org.apache.maven.plugins:maven-antrun-plugin 复制资源并重命名它们很容易,但有没有办法通过通配符或其他机制智能地做到这一点,以符合个人规则?

例如,如果我有这些文件:

  • ${project.artifactId}-${project.version}.swf
  • a.swf
  • b.swf
  • ...
  • z.swf

我想复制目录中的所有这些文件,并仅将 ${project.artifactId}-${project.version}.swf 重命名为 foo.swf。我知道我可以使用:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
       <executions>
          <execution>
             <id>copy-swf-files</id>
             <phase>compile</phase>
                <goals>
                   <goal>run</goal>
                </goals>
                <configuration>
                   <target name="copy swf files to web project">
                       <copy file="${project.build.directory}/${project.artifactId}-${project.version}.swf" tofile="${swf.output.location}/foo.swf" />
                       <copy file="${project.build.directory}/a.swf" tofile="${swf.output.location}/a.swf" />
                       <copy file="${project.build.directory}/z.swf" tofile="${swf.output.location}/z.swf" />
                    </target>
                 </configuration>                       
              </execution>
           </executions>
     </plugin>

这是可行的,但是否有其他方便的方法可以做到这一点,因为如果我要复制 1000 个文件,那将非常无聊...谢谢

【问题讨论】:

    标签: maven ant


    【解决方案1】:

    你知道蚂蚁吗? Maven Ant 插件所做的所有事情就是使用您列出的任务调用 Ant。您可以执行任何 Ant 任务,包括&lt;taskdef&gt; 等。

    你正在做的事情可以用文件集来完成:

     <copy todir="${swf.output.location}">
        <fileset dir="${project.build.directory}">
            <include name="*.swf"/>
        </fileset>
    </copy>
    

    这会将直接位于${project.build.directory} 下的所有*.swf 文件(并且没有子目录)复制到${swf.output.location} 目录。如果要复制*.swf文件的整个目录树,只需更改&lt;include&gt;即可:

     <copy todir="${swf.output.location}">
        <fileset dir="${project.build.directory}">
            <include name="**/*.swf"/>  <!--NOTE DIFFERENCE HERE-->
        </fileset>
    </copy>
    

    如果您需要修改文件名,可以使用Mappers。最简单的映射器是 flatten 映射器:

     <copy todir="${swf.output.location}">
        <fileset dir="${project.build.directory}">
            <include name="**/*.swf"/>  <!--NOTE DIFFERENCE HERE-->
        </fileset>
        <mapper type="flatten"/>
    </copy>
    

    这将复制整个目录树并将所有文件展平到一个目录中。有些映射器可以匹配 glob、正则表达式甚至脚本。

    &lt;include&gt;selector 而不是映射器,因为它会选择您想要处理的文件。这些也可能非常复杂,您可以根据它们的名称 iva 正则表达式,甚至它们的内容来匹配文件。

    我很惊讶没有允许您执行此操作的 Maven 插件。

    【讨论】:

    • 嗯谢谢你的链接,我想regexpselect 会救我。使用 maven 有一个 copy-resources 目标,但我相信我们不能重命名文件,只需复制它们即可。正如 Stevo Slavić 所建议的,有 maven-assembly-plugin 但我还没有使用它。
    • @OlivierJ。我并没有真正看到您更改示例中的文件名。只需将一堆文件从一个目录复制到另一个目录。顺便问一下,您是指regexp 映射器吗?
    • 只检查第一行,我改成file".../foo.swf"
    【解决方案2】:

    好的,我终于用这个插件来做到这一点:

    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-antrun-plugin</artifactId>
       <version>1.7</version>
       <executions>
          <execution>
          <id>copy-swf-files</id>
          <phase>compile</phase>
          <goals>
             <goal>run</goal>
          </goals>
          <configuration>
             <target name="copy swf files to web project">      
                <copy todir="${swf.output.location}">
                    <fileset dir="${project.build.directory}" includes="*.swf">
                        <filename regex="^sim-flex.*"/>
                    </fileset>
                <mapper type="regexp" from=".*" to="sim.swf"/>
                </copy>
                <copy todir="${swf.output.location}" >
                    <fileset dir="${project.build.directory}" includes="*.swf">
                        <not>
                            <filename regex="^sim-flex.*"/>
                        </not>                                  
                    </fileset>
                </copy>
             </target>
          </configuration>                       
       </execution> 
     </executions>
    </plugin>
    

    【讨论】:

    • 这个插件可以复制文件到远程服务器吗?
    【解决方案3】:

    使用带有“dir”“formats/format”的程序集描述符的 maven-assembly-plugin,一个“fileSets/fileSet”,它将复制除特殊 swf 之外的大部分 swf,以及一个将复制该文件的“files/file”一个 swf 到一个具有不同名称的文件。

    【讨论】:

    • 嗨。我在 Google 上看到我可以使用这个插件,但我决定使用 Ant 插件来做到这一点,因为我从来没有使用过这个插件 ^^。无论如何+1,我会尽快研究这个插件
    • 每当我在 pom.xml 中看到 antrun 就很好地表明正在做错事。
    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多