19

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>
Rémi Doolaeghe
  • 2,262
  • 3
  • 31
  • 50

4 Answers4

6

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

Community
  • 1
  • 1
  • 1
    I wondered whether there were any solution using the dependency plugin, but it seems this is not the case. I feared having to use assembly plugin, but the antrun one is just the not-so-bad solution. Thanks for your help! – Rémi Doolaeghe Dec 31 '14 at 07:58
2

You can use file mappers described in Maven Dep. plugin "Rewriting target path and file name" : I was able to use ist like so:

                    <execution>
                    <id>include-run-java-sh-for-docker</id>
                    <phase>package</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>io.fabric8</groupId>
                                <artifactId>run-java-sh</artifactId>
                                <version>1.3.8</version>
                                <classifier>sources</classifier>
                                <includes>**/fp-files/*.sh</includes>
                            <outputDirectory>${project.build.directory}/jkube</outputDirectory>
                                <fileMappers>
                                    <org.codehaus.plexus.components.io.filemappers.FlattenFileMapper />
                                </fileMappers>
                            </artifactItem>
                        </artifactItems>
Robert
  • 21
  • 2
1

Just came across a similar problem, and (since I'm not a big fan of running antiquated Ant inside of Maven, this day and age) I ended up using the org.apache.portals.jetspeed-2:jetspeed-unpack-maven-plugin (mvnrepository link here). In its <resource> configuration you can specify a <flat>true</flat> option which will flatten the directory structure as desired.

raner
  • 1,175
  • 1
  • 11
  • 21
0

You may be able to get a bit closer using <useSubDirectoryPerArtifact>false</useSubDirectoryPerArtifact>. But I think that the unpack-dependencies goal is still going to include the full path of the files from the artifacts it unpacks.

pioto
  • 2,472
  • 23
  • 37