Maven 插件maven-dependency-plugin

复制依赖的jar包到指定的文件夹里,特别是将maven仓库中不存在的jar文件打入到本工程的jar包中
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

背景:

  1.需要某个特殊的 jar包,但是有不能直接通过maven依赖获取,或者说在其他环境的maven仓库内不存在,那么如何将我们所需要的jar包打入我们的生产jar包中。

  2.某个jar包内部包含的文件是我们所需要的,或者是我们希望将它提取出来放入指定的位置 ,那么除了复制粘贴,如何通过maven插件实现呢 

maven-dependency-plugin插件

  dependency  插件我们最常用到的是 dependency:copy  dependency:copy-dependencies  及dependency:unpack    dependency:unpack-dependencies 这四个,如果要实现上述的两种场景,我们需要的 是 第一个和第三个。

dependency:copytakes a list of artifacts defined in the plugin configuration section and copies them to a specified location, renaming them or stripping the version if desired. This goal can resolve the artifacts from remote repositories if they don't exist in either the local repository or the reactor.(将一系列在此插件内列出的artifacts ,将他们copy到一个特殊的地方,重命名或者去除其版本信息。这个可以解决远程仓库存在但是本地仓库不存在的依赖问题)。

  其依赖配置如下:

复制代码

            <!--dependency plugin test start-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>junit</groupId>
                                    <artifactId>junit</artifactId>
                                    <version>4.11</version>
                                    <outputDirectory>${project.build.directory}/lib/lib1</outputDirectory>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>org.slf4j</groupId>
                                    <artifactId>slf4j-log4j12</artifactId>
                                    <version>1.7.7</version>
                                    <outputDirectory>${project.build.directory}/lib/lib1</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

复制代码

我们将两个指定的jar包junit slf4j-log4j12 分别输出到${project.build.directory}/lib/lib1 目录下  即${project.basedir}/target/lib 目录下。

但是这样达到我们的 目的没有呢,这是没有的 我们希望将这些指定的  输出的文件打包到我们的war包中以方便在任何环境内都能正常运行。

大家是否还记得上篇博客:http://www.cnblogs.com/lianshan/p/7348093.html、

利用maven-assembly-plugin加载不同环境所需的配置文件的assembly.xml 文件定义的,fileSet定义的,我们可以将指定目录下的 文件输出到我们的war包结构中,好比这样

        <fileSet>
            <directory>${project.build.directory}/lib/lib1</directory>
            <outputDirectory>lib</outputDirectory>
            <directoryMode>0744</directoryMode>
            <fileMode>0644</fileMode>
        </fileSet>

这样我们就将copy  出来的 文件放到了war包中,完美的解决了我们的需求,很实用于一些现实场景。

  

 那么问题来了,如果我们想将某个jar包的一部分提取出来我们应该怎么做呢,我们是不是因该打开jar包呢 。

 dependency:unpack : like copy but unpacks. 这是官网的 解释,懂了吧 。

打个比方,dubbo这个jar包封装了启动命令脚本,现在我们要上生产了,我们希望生产环境利用这个启动命令进行项目的启动,那么在jar包就肯定不行了,那么我们怎么将这些提取出来呢,当然只能打开了啊

 搜我们通过这样的配置:

复制代码

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.alibaba</groupId>
                                    <artifactId>dubbo</artifactId>
                                    <outputDirectory>${project.build.directory}/dubbo</outputDirectory>
                                    <includes>META-INF/assembly/**</includes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

复制代码

我们将assembly 下的 文件输出到target/dubbo目录下。

 是不是这样就达到我们的效果了,成功将这个文件从jar包中抽取出来了,至于怎么放入我们的包中,那也就是和上面一样了 ,我们利用assembly配置将我们希望的目录下的 文件封装到war包中了。到这里,我们是不是就知道这个maven插件可以做些什么了。

  其实具体的配置在官网上都能找到答案,maven插件的使用,官网都提供了详细的教程,及具体的示例,具体的详细配置可参考apache官网:http://maven.apache.org/components/plugins/maven-dependency-plugin/plugin-info.html

本文的目的,是清楚的告诉你一个插件 能做什么样的事,我们要做什么样的事,需要什么样的插件,大家各司其职,协同工作。

我们才能在我们要做这件事的时候找到它,使用它,而不至于完全没有方向。

  

互联网发展到今天,大多问题都有了良好的解决方案,前人的智慧是无穷的,同时,不是所有人都适合创造,那么至少可以将他人创造的,用精、用好,何尝不是一种发扬。

猜你喜欢

转载自blog.csdn.net/hemeinvyiqiluoben/article/details/81605395