背景:
1.需要某個特殊的 jar包,但是有不能直接通過maven依賴獲取,或者說在其他環境的maven倉庫內不存在,那么如何將我們所需要的jar包打入我們的生產jar包中。
2.某個jar包內部包含的文件是我們所需要的,或者是我們希望將它提取出來放入指定的位置 ,那么除了復制粘貼,如何通過maven插件實現呢
maven-dependency-plugin插件
dependency 插件我們最常用到的是 dependency:copy dependency:copy-dependencies 及dependency:unpack dependency:unpack-dependencies 這四個,如果要實現上述的兩種場景,我們需要的 是 第一個和第三個。
dependency:copy:takes 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
本文的目的,是清楚的告訴你一個插件 能做什么樣的事,我們要做什么樣的事,需要什么樣的插件,大家各司其職,協同工作。
我們才能在我們要做這件事的時候找到它,使用它,而不至於完全沒有方向。
互聯網發展到今天,大多問題都有了良好的解決方案,前人的智慧是無窮的,同時,不是所有人都適合創造,那么至少可以將他人創造的,用精、用好,何嘗不是一種發揚。