Maven的繼承以及import做用域

Maven的pom文件中可繼承的元素包括:java

  groupId:項目ID,項目座標核心元素mysql

  version:項目版本redis

  description:描述信息sql

  organization:組織信息apache

  inceptionYear:創始年份mybatis

  url:項目URL地址maven

  developers:開發者信息post

  distributionManagement:項目部署配置ui

  issueManagement:項目缺陷跟蹤系統信息url

  ciManagement:項目持續集成系統信息

  scm:項目版本控制系統信息

  mailingLists:郵件列表

  properties:自定義屬性

  dependencies:依賴配置

  dependencyManagement:依賴配置管理

  repositories:倉庫配置

  build:構建信息,包括源碼目錄,輸出目錄,插件配置,插件管理配置

  reporting:項目的報告輸出目錄配置,報告插件配置。

 

大部分配置都是說明性質的信息配置,這裏主要介紹依賴配置,依賴管理配置,插件配置,插件管理配置。示例以下:

  父項目配置文件:

<!-- 父項目自己除了一個pom.xml文件,不包含其它內容 -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cbm.stu</groupId>
    <artifactId>maven-study-parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>    <!-- 父項目的packing必須爲pom -->

    <name>maven-study-parent</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- dependencies標籤中的依賴會被子項目徹底繼承:即父項目中有的依賴都會出如今子項目中 -->
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>
    </dependencies>
    
    <!-- 依賴管理中聲明的依賴並不會被子項目直接繼承,在子項目中須要聲明,可是隻須要聲明groupId和artifactId -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.40</version>
            </dependency>
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>42.0.0</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <!-- 插件管理,可供子項目繼承 -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

子項目配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- parent標籤聲明繼承的父項目信息 -->
    <parent>
        <groupId>com.cbm.stu</groupId>
        <artifactId>maven-study-parent</artifactId>
        <version>1.0.0</version>
        <!-- 父項目的pom.xml相對於當前項目的位置 -->
        <relativePath>../maven-study-parent/pom.xml</relativePath>
    </parent>

    <!-- groupId能夠繼承,也能夠覆蓋,一般不須要覆蓋 -->
    <groupId>com.cbm.stud</groupId>
    <!-- 不可繼承,子項目座標的關鍵屬性 -->
    <artifactId>maven-study-account</artifactId>
    <version>1.0.2</version>
    <packaging>jar</packaging>

    <name>maven-study-account</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- 父項目中dependencies標籤聲明的依賴被直接繼承,例如mybatis包 -->
    <dependencies>
        <!-- 繼承自父項目的依賴,只需指明groupId和artifactId -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- 繼承自父項目的插件依賴,此處省略也可繼承 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

 使用import屬性實現依賴的多繼承:

父項目a:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cbm.stu</groupId>
    <artifactId>maven-parent-a</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <name>maven-parent-a</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

父項目b:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cbm.stu</groupId>
    <artifactId>maven-parent-b</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <name>maven-parent-b</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.1</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.40</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

 

繼承 a 和 b 的子項目:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cbm.stu</groupId>
    <artifactId>maven-study-mail</artifactId>
    <version>2.1</version>
    <packaging>jar</packaging>

    <name>maven-study-mail</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 此處繼承了a 和 b 兩個項目,type爲pom,scope 爲 import -->
            <dependency>
                <groupId>com.cbm.stu</groupId>
                <artifactId>maven-parent-a</artifactId>
                <version>1.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.cbm.stu</groupId>
                <artifactId>maven-parent-b</artifactId>
                <version>1.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- 從繼承的父項目中繼承依賴 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

----------------------

The end!

相關文章
相關標籤/搜索