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

时间:2014-12-30 15:36:11

标签: maven dependencies unpack

我有来自同一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>

4 个答案:

答案 0 :(得分:6)

您可以使用带有flatten选项的复制任务来使用maven antrun插件,如以下主题中所述:Maven : copy files without subdirectory structure

最佳

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您可以使用 Maven Dep. plugin "Rewriting target path and file name" 中描述的文件映射器: 我可以像这样使用ist:

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