9

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}.swf
  • a.swf
  • b.swf
  • ...
  • z.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>

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

Olivier J.
  • 3,115
  • 11
  • 48
  • 71

3 Answers3

16

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>

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>

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>

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.

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.

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

David W.
  • 105,218
  • 39
  • 216
  • 337
  • hmm thank you for this link, I think the `regexpselect` will save me. With maven there is a `copy-resources` goal but I believe we can not rename the files, just copy them. As suggested by Stevo Slavić, there is maven-assembly-plugin but I did not use it yet. – Olivier J. Apr 18 '13 at 21:10
  • @OlivierJ. I didn't really seeing you change the name of the files in your example. Just copying a bunch of files from one directory to another. By the way, did you mean the `regexp` mapper? – David W. Apr 18 '13 at 21:23
  • just check the first copy line, I change tofile".../foo.swf" – Olivier J. Apr 18 '13 at 21:27
4

Ok I finally used this plugin to do this :

<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>
Olivier J.
  • 3,115
  • 11
  • 48
  • 71
1

Use maven-assembly-plugin with assembly descriptor of "dir" "formats/format", a "fileSets/fileSet" which will copy majority of swf's excluding the special swf, and a "files/file" which will copy that one swf to a file with different name.

Stevo Slavić
  • 2,298
  • 2
  • 24
  • 32
  • Hi. I saw on Google that I can use this plugin but I decided to use Ant plugin to do this because I never use this plugin ^^. +1 anyway, I will study this plugin as soon as possible – Olivier J. Apr 18 '13 at 21:11
  • Whenever I see antrun in pom.xml it's good indication that something wrong is being done. – Stevo Slavić Apr 19 '13 at 12:56