Maven學習筆記(二)

Maven學習筆記(二)

1.Maven的生命週期和插件

clean\compile\test \package\install

完整的項目構建過程包括:
清理、編譯、測試、打包、集成測試、驗證、部署
Maven包含三套獨立生命週期,執行某個命令,會依次順序執行前面的命令,但不會處罰另外兩套命令。

1.clean 清理項目

pre-clean 執行清理前的文件啊
clean清理上一次構建生成的所有文件
post-clean 執行清理後的文件。

2.default 構建項目(最核心)

compile test package install

3.site 生成項目站點

pre-site    在生成項目站點前要完成的工作
site        生成項目的站點文檔。
post-site   在生成項目站點後要完成的工作。
site-deploy 發佈生成的站點到服務器上。

maven中的許多功能均是調用插件實現的。

例如:source插件可以將項目源碼進行打包
步驟如下:
- 1.打開pom.xml,向其中加入如下代碼

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
            <!--綁定source到package階段-->
            <executions>
                <execution>
                    <!--指定運行階段-->
                    <phase>package</phase>
                    <!--指定運行目標-->
                    <goal>jar-no-fork</goal>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

之後,進行maven操作時,需要:選中項目右鍵-》run as -》 maven build…
在goals中填寫需要的maven操作。之後,直接敲擊shift+alt+x+m即可直接運行maven命令。

2.pom.xml的結構

<project xmlns="...">
    <!--指定當前pom的版本-->
    <modelVersion>4.0.0</modelVersion>

    <groupId>反寫的公司網址+項目名</groupId>
    <artifactId>項目名+模塊名</artifactId>
    <!--第一個0表示大版本號
        第二個0表示分支版本號
        第三個0表示小版本號
        0.0.1
        snapshot快照
        alpha 內部測試
        beta公測
        Release 穩定
        GA 正式發佈
    -->
    <version></version>
    <!--
        默認是jar
        war zip pom
    -->
    <package></package>
    <!--項目描述名-->
    <name></name>
    <!--項目地址-->
    <url></url>
    <!--開發人員-->
    <developers></developers>
    <!--證書-->
    <licenses></licenses>
    <!--組織名-->
    <organization></organization>


    <!--依賴列表重中之重-->
    <dependencies>
        <dependency>
            <groupId></groupId>
            <artifactId></artifactId>
            <version></version>
            <type></type>
            <!--依賴範圍-->
            <scope>test</scope>
            <!--設置依賴是否可選,有兩個值,true、false。默認是false,子項目是繼承的。爲true,子項目必須選擇改該依賴-->
            <optional></optional>
            <!--排除依賴列表-->
            <exclusions>
                <exclusion>

                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <!--依賴管理模塊,定義在父類模塊,供子模塊繼承使用,定義的依賴並不會在該模塊中使用-->
    <dependencyManagement>
        <dependency>
        </dependency>
    </dependencyManagement>
    <!--爲構建行爲提供相應的支持-->
    <build>
        <plugins>
            <groupId></groupId>
            <artifactId></artifactId>
            <version></version>
        </plugins>
    </build>
    <!--子模塊對父模塊pom的繼承-->
    <parent></parent>
    <!--聚合運行多個的maven項目,使用此標籤方便整體的打包-->
    <modules></modules>

</project>

3.依賴的範圍

編寫項目,應用某一jar包,須將jar包引用到項目的classpath中。
maven中爲我們提供了三種classpath:編譯、測試、運行
就是來控制依賴與三中的classpath的關係。
dependency Scope共有6種值可供選擇
compile、 默認的範圍,比那一測試運行都有效
provided、 在編譯測試和測試有效,例如:jdbc
runtime、 測試運行有效
test、 在測試有效
system、 編譯測試有效,與本機系統相關聯,可以執行差
import、 導入的範圍,只是用子啊dependencyManagement中,表示從其它的pom中導入dependecy的配置。
例如:

<project>
    <modelVersion>4.0.0<modelVersion>
    <groupId>maven</groupId>
    <artifactId>B</artifactId>
    <package>pom</package>
    <name>B</name>
    <version>1.0</version>
    <dependencyManagement>
        <dependencies>
            <dependency>
            <!--將A中依賴導入到B中-->
                <groupId>maven</groupId>
                <artifactId>A</artifactId>
                <package>pom</package>
                <version>1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
        <dependencies>
            <dependency>
                <groupId>test</groupId>
                <artifactId>d</artifactId>
                <package>pom</package>
                <version>1.0</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

通過exclusions可以取消傳遞依賴。

4.依賴衝突

假設:A->B->C
B依賴common-2.0
C依賴common-2.4
則如果,A的pom中只寫了依賴B,那麼,A依賴的common是2.0版本(就近原則依賴)
如果,A的pom寫了以來B,同時有寫了以來C,且先聲明的對C的依賴。則A依賴的common是2.4版本的(先聲明,先依賴)

5.聚合與繼承

聚合,既同時打包整個項目的多個模塊,

../A
../B

繼承,在開發多個模塊時,可能有些模塊共用了同一個依賴,則只需頂一個被繼承者,使其他模塊進行繼承即可。
繼承者pom.xml

<project>
    <modelVersion>4.0.0<modelVersion>
    <groupId>maven</groupId>
    <artifactId>B</artifactId>
    <package>pom</package>
    <name>B</name>
    <version>1.0</version>
<!--參數可以寫入配置標籤中-->
    <properties>
        <d.version>1.0</d.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>test</groupId>
                    <artifactId>d</artifactId>
                    <package>pom</package>
                    <version>${d.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

繼承者的pom.xml添加:

<parent>
    <groupId>maven</groupId>
    <artifactId>B</artifactId>
    <version>1.0</version>
</parent>

此處貼出博主編寫的maven練習源碼供大家參考學習,如有問題,歡迎大家共同討論。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章