Maven:如何在相同的输出目录中解压精确的文件?

19

我有来自同一个groupId(org.webjars)的多个构件,我需要将它们解压缩,并将其中包含的所有js文件复制到相同的目录中。

这些构件的归档文件具有以下层次结构(作为jar压缩):

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

我需要在最后将每个JS文件复制到同一个目录中,不保留它们的层次结构,具体操作如下:

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

我能够得到的结果是以下内容:

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

为此,我使用了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>

这个插件有没有类似魔法选项可以完成这个任务,还是我需要另一个插件来完成?

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

<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>

一年过去了,你的回答仍然很有帮助!! - Halayem Anis
两年过去了,你的回答仍然很有帮助!! - Matt
你可以将antrun插件的执行阶段更改为prepare-package。这样,您就不必再次创建jar文件了。 - David
与 https://stackoverflow.com/questions/30425852/maven-unpack-to-disregard-original-folder 相关。 - Kenston Choi
4个回答

6

1
我曾想过是否有使用依赖插件的解决方案,但似乎并非如此。我担心不得不使用汇编插件,但antrun插件只是一个不那么糟糕的解决方案。感谢您的帮助! - Rémi Doolaeghe

2
你可以使用在Maven依赖插件“重写目标路径和文件名”中介绍的文件映射器: 我是这样使用的:
                    <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>

1

我最近遇到了一个类似的问题,而且(因为我不太喜欢在现代环境下运行过时的Ant),我最终使用了org.apache.portals.jetspeed-2:jetspeed-unpack-maven-plugin(mvnrepository链接here)。在其<resource>配置中,您可以指定<flat>true</flat>选项,以按所需方式展平目录结构。


插件可能无法在Maven 3.6中正常工作,正如https://issues.apache.org/jira/browse/JS2-1366中报告的那样。 - Kenston Choi

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接