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

时间:2022-09-13 23:56:12

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.

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

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

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

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:

我最后需要将每个js文件复制到同一目录中而不使用它们的层次结构,如下所示:

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:

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

3 个解决方案

#1


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

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

Best

最好

#2


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.

您可以使用 false 来更接近。但我认为unpack-dependencies目标仍然包括它解压缩的工件中的文件的完整路径。

#3


0  

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.

刚遇到类似的问题,(因为我不喜欢在Maven中运行过时的Ant,这个时代)我最终使用了org.apache.portals.jetspeed-2:jetspeed-unpack-maven -plugin(此处为mvnrepository链接)。在其 配置中,您可以指定 true 选项,该选项将根据需要展平目录结构。

#1


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

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

Best

最好

#2


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.

您可以使用 false 来更接近。但我认为unpack-dependencies目标仍然包括它解压缩的工件中的文件的完整路径。

#3


0  

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.

刚遇到类似的问题,(因为我不喜欢在Maven中运行过时的Ant,这个时代)我最终使用了org.apache.portals.jetspeed-2:jetspeed-unpack-maven -plugin(此处为mvnrepository链接)。在其 配置中,您可以指定 true 选项,该选项将根据需要展平目录结构。