用dependency插件解決依賴包衝突

偶然翻閱了下 maven dependency 插件的官方文檔,很有收穫,記錄一下。可能有些標題黨的嫌疑,並無具體介紹怎麼解決依賴衝突問題,不過既然你都打印出了依賴樹,衝突關係已然在樹中顯示的清清楚楚了。html

依賴樹

dependency:tree 大概是用的最多的功能,用來排查依賴衝突,沒有指定任何參數執行時打印是全部依賴信息,信息量略大,能夠經過includes參數指定想看哪些依賴,也能夠經過excludes參數指定不想看的。includesexcludes能夠配合使用。舉個例子吧:java

# 只想看依賴樹中包含 groupId 爲 javax.serlet 的枝幹
mvn dependency:tree -Dincludes=javax.servlet
# 不想看依賴樹中包含 groupId 爲 javax.serlet 的枝幹
mvn dependency:tree -Dexcludes=javax.servlet

參數的格式(pattern)定義以下:apache

[groupId]:[artifactId]:[type]:[version]bash

每一個部分(冒號分割的部分)是支持*通配符的,若是要指定多個格式則能夠用,分割,如:maven

mvn dependency:tree -Dincludes=javax.servlet,org.apache.*

默認狀況下 dependency:tree 打印出來的是 maven
解決了衝突後的樹(解決衝突的策略是:就近原則,即離根近的依賴被採納),經過指定 -Dverbose
參數則能夠顯示原始的依賴樹,讓你顯式地看出某個包都在哪些枝幹上出現了。ui

清空被本地倉庫(purge-local-repository)

有時候打包時會遇到一些莫名其妙的問題,百思不得其解,可是清空本地倉庫後問題就解決了(就像重啓電腦通常神奇)。以前都是去本地私服目錄把某個groupId對應的jar包都刪了或者
把全部的都給刪了,不免刪了一些無辜的依賴,dependency
插件提供了一個goal能夠方便的刪除本地目錄下該項目依賴的jar包:插件

mvn dependency:purge-local-repository

複製依賴或某些jar包到指定目錄

使用 dependency 的 copy-depenecis goal 把依賴的jar複製到指定目錄前,在pom文件配置以下:code

<project>
[...]
    <build>
        <plugins>
            <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>/path/to/dest</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
[...]
</project>

更多參數信息參見dependency:copy-dependenciesxml

若是隻想複製極少的幾個jar包到指定目錄的話能夠使用 copy goal:htm

<project>
[...]
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>junit</groupId>
                                    <artifactId>junit</artifactId>
                                    <version>3.8.1</version>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>/path/to</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
[...]
</project>

聽說analyze goal 能夠分析出聲明的依賴中哪些未被使用,未被聲明的包中哪些被依賴了,試了下好像並非很好使。

參考

相關文章
相關標籤/搜索