【Maven】1、maven的構建週期

一、問題

1.1、環境
電腦環境:Windows 7;
開發工具:Eclipse 4.6.3;
數據庫環境:Oracle 11g;
JDK環境: Jdk1.8;
Maven環境:Maven3.5.0;

1.2、問題
我們再項目管理,jar包管理的時候,經常會使用到maven工具,那麼,如何使用maven工具,有哪些注意點呢?

二、解答

2.1、基本知識;

2.1.1、構建生命週期的3種方式:default, clean and site.
default:負責項目的部署;
clean:負責項目的清除;
site:處理項目網站文檔的創建
(PS:我覺得起的這3個名字,根本不能見名知意。。。)

2.1.2、創建生命週期的階段;
上述3中方式中,完成defalut需要下面7個階段:

  1. validate - 校驗項目是否正確,並且必須的信息是否存在;
  2. compile - 編譯項目的資源代碼;
  3. test - 使用合適的單元測試框架,測試編譯的資源代碼,測試時,不要求代碼必須打包或部署;
  4. package - 將編譯後的代碼打包成指定格式,例如JAR,WAR等等;
  5. verify - 運行並檢查集成測試的結果,來保證質量;
  6. install - 將打包好的包放到本地的倉庫中,並在本地記錄依賴dependency的地址;
  7. deploy - 將最終的包複製到遠程倉庫中,讓其他人也能下載;

我將會用製作汽車來打比方;

  1. validate - 汽車零件是否齊全
  2. compile - 將零件組裝
  3. test - 輪胎組測試輪胎,發動機組測試發動機,車架組測試車架等
  4. package - 汽車的各個部分分別測試完畢後,開始組裝汽車
  5. verify - 開始整車測試;
  6. install - 測試完的汽車放入本工廠的倉庫,並記錄汽車的編號;
  7. deploy - 將汽車發送給全國經銷商;(這裏比喻有點有點不一樣的是,本地也會保存一份);

2.2、pom.xml
我們先來看一個最簡單的pom.xml文件;pom是porject object managerment的縮寫,在pom.xml裏,你可以定義項目的名稱,版本,需要繼承的其他項目的jar包,你自己依賴的jar包;本項目需要的一些插件,配置發佈地址等等;你可以在這個基礎上去增加或修改自己的pom.xml文件;

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <artifactId>dubbo-sdk</artifactId>

    <!--需要繼承的父類的依賴(非必須寫,也可以不繼承)-->
    <parent>
        <artifactId>MyNoteBook</artifactId>
        <groupId>com.springboot</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>



    <!--定義本POM.xml中要使用的變量,屬性-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <jdk.version>1.8</jdk.version>
        <downloadSources>false</downloadSources>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <!--exclusions會排除指定的jar包-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>jcl-over-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


        <!--這裏加了一個使用範圍,只有在測試時纔會編譯使用-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <!--導入插件-->
    <build>
        <plugins>

            <!--maven的打包插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>log4j2.xml</exclude>
                        <exclude>application.properties</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <!--mybatis的逆向生成工具-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.3</version>
                <configuration>
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                </configuration>
            </plugin>

            <!--maven的編譯插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                    <forkMode>once</forkMode>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>

            <!--maven的複製插件,將需要用的jar包複製到指定目錄下-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--指定測試資源-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>additional-resources</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
                            <resources>
                                <resources>
                                    <resource>
                                        <directory>${project.basedir}/src/test/resources</directory>
                                    </resource>
                                </resources>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>nexus-osc</id>
            <name>MyNoteBook Repository</name>
            <url>127.0.0.1</url>
            <layout>defalut</layout>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

三、總結

歡迎關注我的
CSDN博客: https://blog.csdn.net/River_Continent
微信公衆號:幕橋社區
在這裏插入圖片描述
* 知乎:張牧野, https://www.zhihu.com/people/zhang-mu-ye-37-76/activities
* 簡書: https://www.jianshu.com/u/02c0096cbfd3

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