Maven:如何从同一输出目录中的工件中解压缩精确文件?

Rém*_*ghe 17 dependencies unpack maven

我有来自同一groupId(org.webjars)的几个工件,我需要解压缩它们,然后将所有包含的js文件复制到同一目录中.

工件存档具有层次结构(压缩为jar),如下所示:

artifact1
    - resources
        - webjars
            - ...
                - sample-1.js
                - sample-2.js
Run Code Online (Sandbox Code Playgroud)

我最后需要将每个js文件复制到没有层次结构的同一目录中,如下所示:

outputDirectory
    - sample-1.js
    - sample-2.js
    - ...
    - sample-n.js
Run Code Online (Sandbox Code Playgroud)

我能达到的结果如下:

outputDirectory
    - artifact-1
        - resources
            - webjars
                - ...
                    - sample-1.js
                    - sample-2.js
    - ...
    - artifact-m
        - resources
            - webjars
                - ...
                    - sample-n.js
Run Code Online (Sandbox Code Playgroud)

为此,我使用了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>
Run Code Online (Sandbox Code Playgroud)

这个插件有一个神奇的选择吗,或者我需要另一个插件来完成这项工作?

编辑:这是我最终使用的解决方案:

<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>
Run Code Online (Sandbox Code Playgroud)

小智 6

您可以使用带有flatten选项的复制任务来使用maven antrun插件,如以下线程中所述:Maven:复制没有子目录结构的文件

最好