Maven: How to unpack precise files from artifacts in the same output directory?

Rémi Doolaeghe picture Rémi Doolaeghe · Dec 30, 2014 · Viewed 17.5k times · Source

I have several artifacts from the same groupId (org.webjars), and I need to unpack them, and then copy all the contained js files into the same directory.

The artifacts archives have a hierarchy (compressed as a jar) as follows:

artifact1
    - resources
        - webjars
            - ...
                - sample-1.js
                - sample-2.js

I need at the end that every js file is copied into the same directory without their hierarchy, as follows:

outputDirectory
    - sample-1.js
    - sample-2.js
    - ...
    - sample-n.js

The result I can reach is the following one:

outputDirectory
    - artifact-1
        - resources
            - webjars
                - ...
                    - sample-1.js
                    - sample-2.js
    - ...
    - artifact-m
        - resources
            - webjars
                - ...
                    - sample-n.js

For this purpose, I used the maven-dependency-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack org.webjars dependencies</id>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
            <includeGroupIds>org.webjars</includeGroupIds>
            <includes>**/*.js</includes>
            <outputDirectory>${project.build.directory}/static</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

Is there a magical option of this plugin to do this, or should I need another plugin to complete the job?

EDIT: Here is the solution I finally used:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>copy org.webjars dependency to jar</id>
            <phase>package</phase>
            <goals><goal>run</goal></goals>
            <configuration>
                <target>
                    <copy todir="${project.build.directory}/classes/static" flatten="true">
                        <fileset dir="${project.build.directory}/static">
                                     <include name="**/*.js"/>
                        </fileset>
                    </copy>
                </target>
            </configuration>
          </execution>
    </executions>
</plugin>
<!-- Force the generation of the jar to be done after the javascript dependencies have been copied.
Note : This is a half solution, as the jar is generated twice: a first time before the javascript get copied, and
another one after. As a result, there is the correct jar, but after two creations... -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>Force the jar-plugin to be called after the javascripts dependencies have been copied to be embedded</id>
            <phase>package</phase>
            <goals><goal>jar</goal></goals>
           </execution>
       </executions>
</plugin>

Answer

Aur&#233;lien Bourdon picture Aurélien Bourdon · Dec 30, 2014

You could use the maven antrun plugin by using the copy task with the flatten option as it is described in the following thread: Maven : copy files without subdirectory structure

Best