Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 746
Rémi Doolaeghe Avatar asked Dec 30 '14 15:12

Rémi Doolaeghe


Video Answer


2 Answers

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

like image 166
Aurélien Bourdon Avatar answered Oct 15 '22 13:10

Aurélien Bourdon


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>
like image 39
Robert Avatar answered Oct 15 '22 13:10

Robert