Maven如何将依赖项中的特定文件夹复制到target / classes / myfolder

时间:2016-08-26 09:14:02

标签: maven maven-dependency-plugin

我有一个依赖:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>swagger-ui</artifactId>
    <version>2.1.4</version>
</dependency>

内部的swagger-ui-2.1.4.jar有META-INF/resources/webjars/ 我想将webjars /文件夹复制到target/classes/myfolder

我已阅读this question并执行以下操作:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <includeGroupIds>org.webjars</includeGroupIds>
                <includeArtifactIds>swagger-ui</includeArtifactIds>
                <outputDirectory>${project.build.directory}/classes/myfolder</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

但它将swagger-ui-2.1.4.jar复制到outputDirectory,这不是我特别需要的。

问题:有可能的解决方案吗?

1 个答案:

答案 0 :(得分:1)

你很可能不需要copy-dependenciesunpack。查看official example了解更多详情。

以下示例:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>org.webjars</groupId>
                        <artifactId>swagger-ui</artifactId>
                        <version>2.1.4</version>
                        <type>jar</type>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.directory}/classes/myfolder</outputDirectory>
                        <includes>META-INF/resources/**/*.*</includes>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

将在${project.build.directory}/classes/myfolder下复制META-INF/resources的内容,该内容基本上是webjars文件夹。但是,您也可以获得META-INF/resources树结构。

要完全实现您的意图(仅将依赖项的子文件夹内容复制到某个文件夹),您需要使用两个插件:maven-dependency-pluginmaven-resources-plugin,如下所示:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>org.webjars</groupId>
                        <artifactId>swagger-ui</artifactId>
                        <version>2.1.4</version>
                        <type>jar</type>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.directory}/tmp</outputDirectory>
                        <includes>META-INF/resources/**/*.*</includes>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>install</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/classes/myfolder</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.build.directory}/tmp/META-INF/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

maven-dependency-plugin会将我们想要的目录树复制到target\tmp文件夹,然后maven-resources-plugin只会将我们想要的子文件夹复制到最终目录。

请注意,两个插件都将在install阶段执行,但是他们的声明顺序对于获得最终预期结果非常重要。