Maven和Ant插件智能复制资源

编程入门 行业动态 更新时间:2024-10-27 19:21:12
本文介绍了Maven和Ant插件智能复制资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

It's easy to use org.apache.maven.plugins:maven-antrun-plugin to copy resources and rename them but is there a way to do this intelligently with wildcards or other mecanisms to conform personnal rules ?

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

For example if I have those files :

${project.artifactId}-${project.version}.swfa.swfb.swf...z.swf

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

I would like to copy all those files inside directory and rename only ${project.artifactId}-${project.version}.swf to foo.swf. I know I can use :

<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 个文件,那将非常无聊...谢谢

it's work but is there another convenient way to do that because if I have 1000 files to copy, it will be very boring...Thanks

推荐答案

你知道 Ant 吗?Maven Ant 插件所做的就是使用您列出的任务调用 Ant.您可以执行任何 Ant 任务,包括 等.

Do you know Ant? All the Maven Ant plugin is doing is calling Ant with the tasks you list. You can do any Ant task including <taskdef>, etc.

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

What it looks like you're doing can be done with fileset:

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

这会将位于 ${project.build.directory}(没有子目录)下的所有 *.swf 文件复制到 ${swf.output.location} 目录.如果要复制 *.swf 文件的整个目录树,只需更改 :

This will copy all *.swf files located directly under in the ${project.build.directory} (and no subdirectories) to the ${swf.output.location} directory. If you want to copy the entire directory tree of *.swf files, just change the <include>:

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

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

If you need to munge the file names, you can use Mappers. The simplest mapper would be the flatten mapper:

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

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

This will copy the entire directory tree and flatten all the files into a single directory. There are mappers that can match globs, regular expressions, and even scripting.

是一个 选择器 而不是映射器,因为它选择您要操作的文件.这些也可能非常复杂,您可以根据它们的名称 iva 正则表达式,甚至它们的内容来匹配文件.

The <include> is a selector and not a mapper because it selects what files you want to act upon. These too can be quite complex, and you can match file based upon their names iva regular expressions, or even their contents.

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

I'm surprised there isn't a Maven plugin that allows you to do this.

这篇关于Maven和Ant插件智能复制资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-20 12:25:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/983930.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:插件   智能   资源   Maven   Ant

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!