maven原理分析(1)

1 maven的目錄結構(最下面在加一個pom.xml文件)

2 在java包下的com.flowable下創建一個UserService類:

package com.flowable;

public class UserService {
    public String sayHello(String name){
        return "hello"+name;
    }
}

3 創建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">
    <!--超級pom依賴-->
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.test</groupId>
    <artifactId>test-demo</artifactId>
    <!--SNAPSHOT是快照的意思-->
    <version>0.0.1-SNAPSHOT</version>
</project>

4 用cmd命令行到我們的test-demo目錄下

    使用 mvn compiler命令編譯,如果是這樣就說明成功了:

    並且在項目路徑下已經生成了target包:

    4.1 使用mvn package打包,默認是jar包,如需要打成war包需在pom.xml中定義:

        <packaging>war</packaging>

        打包成功後會在target包下出現一個jar包或者war包:

        

    4.2 執行mvn clean會對target目錄進行清空:

        

        會發現target包沒有了

    4.3 mvn test命令,測試不通過是不能打包的.可以使用mvn package -Dmaven.test.skip=true命令跳過測試用例

        在test包下創建UserServiceTest.java類

package com.tuling.service;

import org.junit.*;
import com.tuling.service.*;
public class UserServiceTest {
	
	@Test
    public void sayHello(){
        UserService service = new UserService();
		Assert.assertEquals(service.sayHello("hello"),"hellohello");
    }
}

        並在pom.xml文件下添加Junit依賴:

<dependencies>
		<dependency>
		  <groupId>junit</groupId>
		  <artifactId>junit</artifactId>
		  <version>4.11</version>
		  <scope>test</scope>
		</dependency>
</dependencies>

        執行成功:

F:\yun6\tuling-gene>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building tuling-gene 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ tuling-gene ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ tuling-gene ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ tuling-gene ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ tuling-gene ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\yun6\tuling-gene\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ tuling-gene ---
[INFO] Surefire report directory: F:\yun6\tuling-gene\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.tuling.service.UserServiceTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.058 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.040 s
[INFO] Finished at: 2019-05-09T22:39:00+08:00
[INFO] Final Memory: 14M/161M
[INFO] ------------------------------------------------------------------------

    5 我們maven裏面的jar包是從哪裏獲取的呢?

         maven用戶 -> 查找本地 -> 私服查找 -> 遠程倉庫(在maven文件夾lib文件夾下的maven-model-build

        下 [http://repo.maven.apache.org])

    6 配置阿里雲的鏡像

<mirrors> 
    <mirror> 
    <id>alimaven</id> 
    <name>aliyun maven</name> 
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
    <mirrorOf>central</mirrorOf>
    </mirror> 
</mirrors>

    7 這裏總結下maven的命令

        mvn clean 清楚

        mvn compile 編譯

        mvn test 執行測試用例

        mvn package 打包(打包時上面的命令都會執行一遍)

        mvn install 打包完成上傳本地倉庫

    8 講一下scope標籤<scope></scope>

        <scope>test</scope> 作用域是在測試環境中(@Test只能出現在test包下)

        <scope>compile</scope> 默認的作用範圍,在在編譯和運行時都會神效

        <scope>provide</scope>  編譯的時候生效,打包的時候不生效

        <scope>runtime</scope>  編譯不生效,打包的時候生效

    9 關於maven依賴關係的選擇

        四個字:就近原則

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