Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven and Ant plugin to copy resources intelligently

Tags:

maven

ant

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

like image 695
Olivier J. Avatar asked Apr 18 '13 20:04

Olivier J.


3 Answers

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.

like image 108
David W. Avatar answered Nov 09 '22 02:11

David W.


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>
like image 26
Olivier J. Avatar answered Nov 09 '22 01:11

Olivier J.


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.

like image 1
Stevo Slavić Avatar answered Nov 09 '22 01:11

Stevo Slavić