1. 程式人生 > >Maven 教程(22)— Maven中 plugins 和 pluginManagement

Maven 教程(22)— Maven中 plugins 和 pluginManagement

plugins和pluginManagement的區別概述

pluginspluginManagement 的區別,和我們前面研究過的 dependenciesdependencyManagement 的區別是非常類似的。plugins 下的 plugin 是真實使用的,而 pluginManagement 下的 plugins 下的 plugin 則僅僅是一種宣告,子專案中可以對 pluginManagement 下的 plugin 進行資訊的選擇、繼承、覆蓋等。

pluginManagement使用實戰

假如存在兩個專案,專案A為專案B的父專案,其關係通過pom檔案的關係確定。專案A的父pom檔案片段如下:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <attach
>
true</attach> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution
>
</executions> </plugin> </plugins> </pluginManagement>

如果專案B也想使用該plugin配置,則在專案B的子pom檔案中只需要如下配置:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
    </plugin>
</plugins>

我們可以看到,子pom檔案中,省去了版本、配置細節等資訊,只需要指定groupId和artifactId,其他資訊均從父pom檔案繼承。當然,如果子pom檔案想定製自己的特定內容,可以另行設定,並會覆蓋從父pom檔案繼承到的內容。

需要注意的是,dependenciesdependencyManagement 均是 project 下的直接子元素,但是 pluginspluginManagement 卻是 project 下 build 的直接子元素。