Maven - 人生若只如初見(jiàn)(三)

今天來(lái)講講Maven的基本概念 - 新手村

首先看一條常用命令的執(zhí)行輸出

sunyelw@windows:hymvn$
sunyelw@windows:hymvn$ mvn clean package -DskipTests
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hymvn 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ hymvn ---
[INFO] Deleting C:\idea\workspace\hymvn\target
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ hymvn ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\idea\workspace\hymvn\src\main\resources
[INFO] skip non existing resourceDirectory C:\idea\workspace\hymvn\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ hymvn ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 4 source files to C:\idea\workspace\hymvn\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ hymvn ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\idea\workspace\hymvn\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ hymvn ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\idea\workspace\hymvn\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ hymvn ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ hymvn ---
[INFO] Building jar: C:\idea\workspace\hymvn\target\hymvn-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.2.RELEASE:repackage (default) @ hymvn ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.668 s
[INFO] Finished at: 2019-12-19T21:02:17+08:00
[INFO] Final Memory: 31M/237M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:hymvn$
  • 你知道輸出的這一大串是什么東西嗎?

  • [INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ hymvn --- 這一行的意義是什么你清楚嗎?

帶著這些問(wèn)題開(kāi)始我們的探險(xiǎn).


一、基本概念

  • maven 自身的核心功能就是解析 XML 文檔、管理生命周期與插件
  • 將主要的職責(zé)委派給一組 maven 插件執(zhí)行,這些插件可以影響 maven的生命周期,提供對(duì)目標(biāo)的訪(fǎng)問(wèn)

二、插件與目標(biāo)

1. 插件<plugin>

  • 由一個(gè)或一組目標(biāo)組成,供階段 phase <compile/package/install/deploy等> 調(diào)用

Plugins are artifacts that provide goals to Maven. (插件是為 Maven 提供功能的 構(gòu)件 <artifacts> )

舉例

  • Compiler 插件, 負(fù)責(zé)編譯成二進(jìn)制文件
  • Jar 插件, 打一些可執(zhí)行 JAR

這些插件都是開(kāi)源的,可以在github上找到源碼

2. 目標(biāo)<goal>

  • maven 具體執(zhí)行的任務(wù),可以單獨(dú)執(zhí)行,也可以與其他目標(biāo)組合執(zhí)行
  • 這些目標(biāo)可以設(shè)置一些具體的參數(shù),比如打包時(shí)是否跳過(guò)測(cè)試以減少打包時(shí)間(-Dmaven.test.skip=true/-DskipTests)、編譯時(shí)指定JDK版本等

說(shuō)到這里,可能還是不知道插件與目標(biāo)是個(gè)什么玩意,還好Maven提供了一個(gè)很有用的插件來(lái)讓我們切身感受一下那個(gè)世界的樣子~

三、help插件詳述

maven插件列表中有一個(gè)特殊的插件,可以用于幫助獲取一些有用的信息,這個(gè)插件的名字就叫 help

help 既然是一個(gè)插件,那么它也是由目標(biāo)組成, 看看其有哪些目標(biāo)

對(duì)于初學(xué)者來(lái)說(shuō)最重要的一個(gè)目標(biāo)就是 describe, 通過(guò)這個(gè)目標(biāo)可以獲取其他的插件的用法(激動(dòng)一會(huì)......)

我們用help插件的describe目標(biāo)來(lái)描述它本身

mvn help:describe -Dplugin=help -Dgoal=describe -Ddetail

看看輸出

sunyelw@windows:hymvn$
sunyelw@windows:hymvn$ mvn help:describe -Dplugin=help -Dgoal=describe -Ddetail
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hymvn 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:describe (default-cli) @ hymvn ---
[INFO] Mojo: 'help:describe'
help:describe
  Description: Displays a list of the attributes for a Maven Plugin and/or
    goals (aka Mojo - Maven plain Old Java Object).
  Implementation: org.apache.maven.plugins.help.DescribeMojo
  Language: java

  Available parameters:

    artifactId
      User property: artifactId
      The Maven Plugin artifactId to describe.
      Note: Should be used with groupId parameter.

    cmd
      User property: cmd
      A Maven command like a single goal or a single phase following the Maven
      command line:
      mvn [options] [<goal(s)>] [<phase(s)>]

    detail (Default: false)
      User property: detail
      This flag specifies that a detailed (verbose) list of goal (Mojo)
      information should be given.

    goal
      User property: goal
      The goal name of a Mojo to describe within the specified Maven Plugin. If
      this parameter is specified, only the corresponding goal (Mojo) will be
      described, rather than the whole Plugin.

    groupId
      User property: groupId
      The Maven Plugin groupId to describe.
      Note: Should be used with artifactId parameter.

    minimal (Default: false)
      User property: minimal
      This flag specifies that a minimal list of goal (Mojo) information should
      be given.

    output
      User property: output
      Optional parameter to write the output of this help in a given file,
      instead of writing to the console.
      Note: Could be a relative path.

    plugin
      User property: plugin
      The Maven Plugin to describe. This must be specified in one of three
      ways:

      1.  plugin-prefix, i.e. 'help'
      2.  groupId:artifactId, i.e. 'org.apache.maven.plugins:maven-help-plugin'
      3.  groupId:artifactId:version, i.e.
        'org.apache.maven.plugins:maven-help-plugin:2.0'

    version
      User property: version
      The Maven Plugin version to describe.
      Note: Should be used with groupId/artifactId parameters.


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.392 s
[INFO] Finished at: 2019-12-19T21:20:39+08:00
[INFO] Final Memory: 17M/155M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:hymvn$
  • -Dplugin=help 指定需要描述的插件名稱(chēng),上面輸出中給了-Dplugin的三種寫(xiě)法
  • -Dgoal=describe 執(zhí)行插件目標(biāo),不加會(huì)顯示所有可用目標(biāo), help 插件共有 8 個(gè)目標(biāo)
  • -Ddetail 是否顯示詳細(xì)插件使用信息,加了會(huì)顯示具體如何使用及一些說(shuō)明,不加就一個(gè)簡(jiǎn)單介紹

有了這個(gè)插件,其他插件的用法就都可以獲知了,激動(dòng)一會(huì)~

看下插件dependencytree目標(biāo)

sunyelw@windows:hymvn$
sunyelw@windows:hymvn$ mvn help:describe -Dplugin=dependency -Dgoal=tree
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hymvn 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:describe (default-cli) @ hymvn ---
[INFO] Mojo: 'dependency:tree'
dependency:tree
  Description: Displays the dependency tree for this project.

For more information, run 'mvn help:describe [...] -Ddetail'

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.228 s
[INFO] Finished at: 2019-12-19T21:24:37+08:00
[INFO] Final Memory: 22M/220M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:hymvn$

展示項(xiàng)目的依賴(lài)樹(shù),執(zhí)行一下看看效果

sunyelw@windows:hymvn$
sunyelw@windows:hymvn$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hymvn 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ hymvn ---
[INFO] com.hy.demo:hymvn:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.0.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.0.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.0.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.2.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  |  +- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  |  |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] |  |  +- org.springframework:spring-core:jar:5.0.6.RELEASE:compile
[INFO] |  |  |  \- org.springframework:spring-jcl:jar:5.0.6.RELEASE:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.0.2.RELEASE:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.5:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.5:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.5:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.5:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.5:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.2.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.31:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.31:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.31:compile
[INFO] |  +- org.hibernate.validator:hibernate-validator:jar:6.0.9.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] |  +- org.springframework:spring-web:jar:5.0.6.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:5.0.6.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.0.6.RELEASE:compile
[INFO] |     +- org.springframework:spring-aop:jar:5.0.6.RELEASE:compile
[INFO] |     +- org.springframework:spring-context:jar:5.0.6.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:5.0.6.RELEASE:compile
[INFO] +- org.springframework:spring-context-indexer:jar:5.0.6.RELEASE:compile (optional)
[INFO] \- org.springframework.boot:spring-boot-loader:jar:2.0.2.RELEASE:provided
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.499 s
[INFO] Finished at: 2019-12-19T21:26:42+08:00
[INFO] Final Memory: 21M/228M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:hymvn$

很好,完全符合預(yù)期效果~

然后我們會(huì)用插件了,但插件又是定義在哪的呢?

4. 插件來(lái)源

執(zhí)行一個(gè)不存在的插件命令試試

sunyelw@windows:hymvn$
sunyelw@windows:hymvn$ mvn 1:2
[INFO] Scanning for projects...
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-metadata.xml
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/mojo/maven-metadata.xml
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/com/sun/yelw/maven-metadata.xml
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-metadata.xml (9.7 kB at 4.5 kB/s)
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/mojo/maven-metadata.xml (21 kB at 9.5 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.839 s
[INFO] Finished at: 2019-12-19T21:28:18+08:00
[INFO] Final Memory: 22M/192M
[INFO] ------------------------------------------------------------------------
[ERROR] No plugin found for prefix '1' in the current project and in the plugin groups [com.sun.yelw, org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (F:\repository), alimaven (http://maven.aliyun.com/nexus/content/groups/public/)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
sunyelw@windows:hymvn$

可以看到一個(gè)下載過(guò)程,說(shuō)明如果本地插件庫(kù)中沒(méi)找到就會(huì)去配置好的遠(yuǎn)程倉(cāng)庫(kù)去下載,這里配的alimaven的鏡像,故輸出如下:

Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-metadata.xml
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/mojo/maven-metadata.xml
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/com/sun/yelw/maven-metadata.xml
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-metadata.xml (9.7 kB at 4.5 kB/s)
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/mojo/maven-metadata.xml (21 kB at 9.5 kB/s)

中間的那個(gè)是用戶(hù)級(jí)別的插件倉(cāng)庫(kù),忽略
真正下載的就倆文件

  • org/apache/maven/maven-metadata.xml
  • org/codehaus/mojo/maven-metadata.xml

在本地倉(cāng)庫(kù)中找到這倆文件,打開(kāi)來(lái)看下:

<plugin>
  <prefix>dependency</prefix>
  <artifactId>maven-dependency-plugin</artifactId>
  <name>Apache Maven Dependency Plugin</name>
</plugin>
<plugin>
  <prefix>deploy</prefix>
  <artifactId>maven-deploy-plugin</artifactId>
  <name>Apache Maven Deploy Plugin</name>
</plugin>

這里面配置的全是插件,我們上面執(zhí)行的插件都能在里面找到。

還有一個(gè)東西挺好玩, 他的文件名會(huì)加上倉(cāng)庫(kù)的name, 比如 alimaven 的就是 maven-metadata-alimaven.xml

  • 如果從其他倉(cāng)庫(kù)下載過(guò), 那么同級(jí)目錄下還有其他名稱(chēng)的, 比如中央倉(cāng)庫(kù)的 maven-metadata-central.xml

上面報(bào)錯(cuò)還有一個(gè)地方需要注意

[ERROR] No plugin found for prefix '1' in the current project 
and in the plugin groups 
[com.sun.yelw, org.apache.maven.plugins, org.codehaus.mojo] 
available from the repositories 
[local (F:\repository), 
alimaven (http://maven.aliyun.com/nexus/content/groups/public/)] 
-> [Help 1]
  • 插件都是配置在文件 maven-metadata-name.xml [name 是倉(cāng)庫(kù) id]
  • mvn 命令的搜索范圍 在 com.sun.yelw, org.apache.maven.plugins, org.codehaus.mojo 這三個(gè)路徑中的配置文件中(前面加上local-repository 的路徑)
  • 后綴是 [local, alimaven], 這里說(shuō)明只有 localalimaven 后綴的文件生效,也就說(shuō)明當(dāng)前只有一個(gè)alimaven遠(yuǎn)程倉(cāng)庫(kù)生效

5. 生命周期<lifecycle>

Maven一共有三個(gè)生命周期

  • clean 進(jìn)行真正的構(gòu)建之前進(jìn)行一些清理工作
  • default 構(gòu)建的核心部分,編譯,測(cè)試,打包,部署等
  • site 生成項(xiàng)目報(bào)告,站點(diǎn),發(fā)布站點(diǎn)

這三套生命周期是獨(dú)立的,是線(xiàn)性的,是互不影響的,詳細(xì)參見(jiàn)完整生命周期的階段組成

本地匯總了一張圖


生命周期與階段

6. 一些錯(cuò)綜復(fù)雜的關(guān)系

  • 生命周期(lifecycle)
    • 階段(phase)
    • default > package / clean > clean / site > site
  • 插件(plugin)
    • 目標(biāo)(goal)
    • help:effective-settings / jar:jar
  • lifecycle 由一組 phase 組成, 這些 phase 是有順序的
  • 后面的 phase 依賴(lài)于前面的 phase,也就是說(shuō)如果你運(yùn)行了某個(gè)生命周期的某個(gè)階段,那么此生命周期在這個(gè)階段之前的階段都會(huì)得到運(yùn)行

注意不是之前的所有階段都會(huì)運(yùn)行,后文再細(xì)說(shuō)

注意兩點(diǎn)

  • phase 由一組目標(biāo)組成, 有些目標(biāo)(goal)可以不執(zhí)行(參數(shù)控制)
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ hymvn ---
[INFO] Tests are skipped.
[INFO]
  • phase 由一組目標(biāo)組成, 這組目標(biāo)的數(shù)量可以是0, 比如下面的validate就沒(méi)有綁定任何目標(biāo)
sunyelw@windows:hymvn$                                                               
sunyelw@windows:hymvn$ mvn validate                                                  
[INFO] Scanning for projects...                                                      
[INFO]                                                                               
[INFO] ------------------------------------------------------------------------      
[INFO] Building hymvn 1.0-SNAPSHOT                                                   
[INFO] ------------------------------------------------------------------------      
[INFO] ------------------------------------------------------------------------      
[INFO] BUILD SUCCESS                                                                 
[INFO] ------------------------------------------------------------------------      
[INFO] Total time: 0.899 s                                                           
[INFO] Finished at: 2019-12-19T21:48:48+08:00                                        
[INFO] Final Memory: 10M/155M                                                        
[INFO] ------------------------------------------------------------------------      
sunyelw@windows:hymvn$                                                               

是生命周期(lifecycle)的階段(phase)和插件(plugin)的目標(biāo)(goal)相互綁定,以完成某個(gè)具體的構(gòu)建任務(wù)

那么default 生命周期的其他階段都沒(méi)有綁定目標(biāo), 這些生命周期是否執(zhí)行了呢?

官方文檔中有一句話(huà)

Moreover, if a goal is bound to one or more build phases, that goal will be called in all those phases.
Furthermore, a build phase can also have zero or more goals bound to it.
If a build phase has no goals bound to it, that build phase will not execute.
But if it has one or more goals bound to it, it will execute all those goals.

  • 如果一個(gè)目標(biāo)被綁定到多個(gè)階段, 那么所有階段都會(huì)執(zhí)行這個(gè)目標(biāo)
  • 如果一個(gè)階段沒(méi)有綁定任何目標(biāo), 那么此階段不會(huì)被執(zhí)行
  • 如果一個(gè)階段綁定了多個(gè)目標(biāo), 那么此階段會(huì)執(zhí)行所有目標(biāo)
  • 階段是否綁定目標(biāo) 取決于默認(rèn) (也可以自定義添加或創(chuàng)建)

A goal not bound to any build phase could be executed
outside of the build lifecycle by direct invocation.

  • 可以在構(gòu)建生命周期之外直接調(diào)用沒(méi)有綁定到任何階段的目標(biāo)

7. packaging屬性的重要性

生命周期的階段與目標(biāo)的綁定關(guān)系 取決于 packaging 屬性, 比如在 jar 模式下

<phases>
  <process-resources>
    org.apache.maven.plugins:maven-resources-plugin:2.6:resources
  </process-resources>
  <compile>
    org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
  </compile>
  <process-test-resources>
    org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
  </process-test-resources>
  <test-compile>
    org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
  </test-compile>
  <test>
    org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
  </test>
  <package>
    org.apache.maven.plugins:maven-jar-plugin:2.4:jar
  </package>
  <install>
    org.apache.maven.plugins:maven-install-plugin:2.4:install
  </install>
  <deploy>
    org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
  </deploy>
</phases>
階段 插件:目標(biāo)
process-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war
install install:install
deploy deploy:deploy

可以看到如果執(zhí)行 package 會(huì)執(zhí)行以下目標(biāo)

  • resources
  • compile
  • testResources
  • testCompile
  • test
  • jar

installdeploy 就是后面再加兩個(gè),詳情請(qǐng)參見(jiàn)默認(rèn)綁定配置

8. mvn 創(chuàng)建項(xiàng)目

mvn archetype:generate -DgroupId=com.hy.demo -DartifactId=hymvn -DarchetypeArtifactId=maven-archetype-quickstart -Dpackage=com.hy.demo -DinteractiveMode=false
  • archetype:generate 插件與目標(biāo)的組合執(zhí)行

9. mvn命令報(bào)錯(cuò)定位

這里列幾種我個(gè)人常用的方法,比較實(shí)用,當(dāng)然要看問(wèn)題具體類(lèi)型。

  • 檢查配置文件,查看當(dāng)前生效的配置
sunyelw@windows:hymvn$
sunyelw@windows:hymvn$ mvn help:effective-settings
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hymvn 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:effective-settings (default-cli) @ hymvn ---
[INFO]
Effective user-specific configuration settings:

<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin on 2019-12-19T10:19:16                  -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
<!--                                                                        -->
<!-- ====================================================================== -->

<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Effective Settings for 'yello' on 'DESKTOP-S0L3088'                    -->
<!--                                                                        -->
<!-- ====================================================================== -->

<settings xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
  <localRepository xmlns="http://maven.apache.org/SETTINGS/1.1.0">F:\repository</localRepository>
  <mirrors xmlns="http://maven.apache.org/SETTINGS/1.1.0">
    <mirror>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <id>alimaven</id>
    </mirror>
  </mirrors>
  <pluginGroups xmlns="http://maven.apache.org/SETTINGS/1.1.0">
    <pluginGroup>com.sun.yelw</pluginGroup>
    <pluginGroup>org.apache.maven.plugins</pluginGroup>
    <pluginGroup>org.codehaus.mojo</pluginGroup>
  </pluginGroups>
</settings>

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.007 s
[INFO] Finished at: 2019-12-19T22:19:16+08:00
[INFO] Final Memory: 16M/155M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:hymvn$
  • 檢查生效的POM設(shè)置,這里只是簡(jiǎn)單看下central倉(cāng)庫(kù)配的地址
sunyelw@windows:hymvn$
sunyelw@windows:hymvn$ mvn help:effective-pom | grep 'central' -7
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>C:\idea\workspace\hymvn\src\main\java</sourceDirectory>
    <scriptSourceDirectory>C:\idea\workspace\hymvn\src\main\scripts</scriptSourceDirectory>
sunyelw@windows:hymvn$

發(fā)現(xiàn)一個(gè)依賴(lài)倉(cāng)庫(kù),一個(gè)構(gòu)件倉(cāng)庫(kù),地址都是https://repo.maven.apache.org/maven2

  • 還可以加 -X參數(shù)查看運(yùn)行過(guò)程,注意會(huì)特別多,拿打包舉例
mvn clean package -DskipTests -X > mvn_package_x.log

下面列出部分內(nèi)容
開(kāi)頭是Maven的版本信息

Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T15:58:13+08:00)
Maven home: E:\apache-maven-3.5.2
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: E:\Java\jdk1.8.0_151\jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

然后是一些導(dǎo)包

[DEBUG] Created new class realm maven.api
[DEBUG] Importing foreign packages into class realm maven.api
[DEBUG]   Imported: javax.annotation.* < plexus.core
...
[DEBUG]   Imported: org.slf4j.helpers.* < plexus.core
[DEBUG]   Imported: org.slf4j.spi.* < plexus.core

下面有兩行非常重要的信息

[DEBUG] Reading global settings from E:\apache-maven-3.5.2\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\yello\.m2\settings.xml

最終生效的settings.xml文件原來(lái)是由兩個(gè)文件構(gòu)成的,一個(gè)是用戶(hù)目錄${USER_HOME}\.m2\settings.xml, 還有一個(gè)安裝目錄${M2_HOME}\conf\settings.xml,所以如果你用戶(hù)的設(shè)置文件沒(méi)問(wèn)題而又多了些配置時(shí)不妨考慮下安裝目錄下的配置。

還有很多信息

本地倉(cāng)庫(kù)地址

[DEBUG] Using local repository at F:\repository

鏡像使用情況

[DEBUG] Using mirror alimaven (http://maven.aliyun.com/nexus/content/groups/public/) for central (https://repo.maven.apache.org/maven2).

執(zhí)行計(jì)劃

[DEBUG] === REACTOR BUILD PLAN ================================================
[DEBUG] Project: com.hy.demo:hymvn:jar:1.0-SNAPSHOT
[DEBUG] Tasks:   [clean, package]
[DEBUG] Style:   Regular
[DEBUG] =======================================================================

還有詳細(xì)的執(zhí)行過(guò)程與配置信息, 比如clean

[DEBUG] Goal:          org.apache.maven.plugins:maven-clean-plugin:3.0.0:clean (default-clean)
[DEBUG] Style:         Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <directory default-value="${project.build.directory}"/>
  <excludeDefaultDirectories default-value="false">${maven.clean.excludeDefaultDirectories}</excludeDefaultDirectories>
  <failOnError default-value="true">${maven.clean.failOnError}</failOnError>
  <followSymLinks default-value="false">${maven.clean.followSymLinks}</followSymLinks>
  <outputDirectory default-value="${project.build.outputDirectory}"/>
  <reportDirectory default-value="${project.build.outputDirectory}"/>
  <retryOnError default-value="true">${maven.clean.retryOnError}</retryOnError>
  <skip default-value="false">${maven.clean.skip}</skip>
  <testOutputDirectory default-value="${project.build.testOutputDirectory}"/>
  <verbose>${maven.clean.verbose}</verbose>
</configuration>

后面還有很多詳細(xì)的輸出,就不一一列舉了。


現(xiàn)在你看到開(kāi)篇的輸出能解答出Maven做了什么嗎?

`

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 經(jīng)過(guò)Maven生命周期的學(xué)習(xí),我們知道在Maven core中僅僅定義了抽象的生命周期,具體的實(shí)現(xiàn)是由插件完成的,...
    SonyaBaby閱讀 1,403評(píng)論 0 3
  • 五. 倉(cāng)庫(kù) 在Maven中,任何一個(gè)依賴(lài)、插件或者項(xiàng)目構(gòu)建的輸出,都可以稱(chēng)之為 構(gòu)件。Maven在某個(gè)統(tǒng)一的位置存...
    wind_sky閱讀 1,574評(píng)論 0 0
  • Maven[?mev?n]能幫你構(gòu)建工程,管理jar包,編譯代碼,還能幫你自動(dòng)運(yùn)行單元測(cè)試,打包,生成報(bào)表,甚至能...
    ZMRWEGo閱讀 1,041評(píng)論 0 1
  • 第1章 Maven 介紹 什么是 Maven 什么是 Maven Maven 的正確發(fā)音是[?mev?n],而不是...
    強(qiáng)某某閱讀 2,564評(píng)論 0 25
  • 坐在自己的書(shū)桌前,面對(duì)攤開(kāi)著的日記本,四周歲半的小女兒拿筆的姿勢(shì)已經(jīng)像模像樣。 就在半個(gè)小時(shí)前,她問(wèn)我:“老爸,你...
    正言鋒語(yǔ)閱讀 294評(píng)論 0 2

友情鏈接更多精彩內(nèi)容