Maven 命令整理


Maven 命令整理

#查看環境變量
echo %M2_HOME%       
echo %MAVEN_HOME% 

#查看版本
mvn -v
mvn -version
mvn --version

#清除產生的項目
mvn clean

#編譯源代碼
mvn compile

#編譯測試代碼
mvn test-compile

#編譯並測試
mvn test

mvn clean compile
mvn clean test
mvn clean package
#在本地Repository中安裝jar
mvn clean install
#執行 test 之前會先執行 compile,編譯並測試
#執行 package 之前會先執行 test,測試並打包
#執行 install 之前會先執行 package,打包並安裝

#指定java全局屬性
mvn -Dxxx=yyy 
#給任何目標添加 maven.test.skip 屬性就能跳過測試
mvn -Dmaven.test.skip=true  

 

#只打包不測試:
mvn -Dtest package

#只打jar包
mvn jar:jar

#只測試而不編譯,也不測試編譯( -skipping 的靈活運用,當然也可以用於其他組合命令)
mvn test -skipping compile -skipping test-compile

#強制更新snapshot類型的插件或依賴庫(否則maven一天只會更新一次snapshot依賴)
mvn -U 

#打印整個依賴樹
mvn dependency:tree

#列出項目的所有jar包
mvn dependency:list

#打印出已解決依賴的列表
mvn dependency:resolve  


#創建Maven的普通java項目: 
   mvn archetype:create 
   -DgroupId=packageName 
   -DartifactId=projectName  
#創建Maven的Web項目:   
   mvn archetype:create 
   -DgroupId=packageName    
   -DartifactId=webappName 
   -DarchetypeArtifactId=maven-archetype-webapp

maven的mvn dependency依賴分析和常用命令介紹

來源

在項目開發過程中,我們經常會有分析項目依賴的需要,查找jar依賴路徑,查找jar沖突等等。這時候,dependency命令會非常的有用,在此我們介紹下關於maven的dependency命令的一些用法。

1. mvn dependency:list ---- 列出項目的所有jar包

mvn dependency:list -Dverbose
該命令可以列出項目依賴的所有jar包,-Dverbose參數會把被忽略的jar,即相同jar包的不同版本引入也列出來。
輸出示例:

[INFO]    org.springframework:spring-aop:jar:5.0.6.RELEASE:compile
[INFO]    org.hibernate.validator:hibernate-validator:jar:6.0.9.Final:compile
[INFO]    com.fasterxml.jackson.core:jackson-core:jar:2.9.5:compile
[INFO]    com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.5:compile
[INFO]    org.springframework:spring-expression:jar:5.0.6.RELEASE:compile
[INFO]    org.springframework.boot:spring-boot-starter:jar:2.0.2.RELEASE:compile
[INFO]    org.springframework.boot:spring-boot-starter-logging:jar:2.0.2.RELEASE:compile
[INFO]    org.yaml:snakeyaml:jar:1.19:runtime
[INFO]    org.springframework.boot:spring-boot:jar:2.0.2.RELEASE:compile
[INFO]    junit:junit:jar:3.8.1:test

2. mvn dependency:tree ---- 列出項目的包依賴樹

mvn dependency:tree -Dverbose
這個命令跟上一個命令的區別就是,這個命令的依賴,輸出來是個樹,更方便看依賴關系。
輸出示例:

[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:spring-core:jar:5.0.6.RELEASE:compile
[INFO]    |     \- org.springframework:spring-jcl:jar:5.0.6.RELEASE: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

dependency:tree有幾個比較重要的參數,非常有用:

  • includes
    • 說明:該參數可以列出指定要求的jar,其他的忽略
    • 示例:-Dincludes=velocity:velocity,只列出velocity的依賴關系
    • 參數值:[groupId]:[artifactId]:[type]:[version],參數格式就是這樣的,沒有的值可以留空,舉例-Dincludes=:spring-aop-Dincludes=:::5.0.6.RELEASE-Dincludes=org.springframework
    • 通配符:在參數中可以使用通配符,例如org.apache.*, :::*-SNAPSHOT
    • 多個參數值:參數后面可以跟多個參數值,以英文逗號分隔,舉例-Dincludes=org.apache.maven*,org.codehaus.plexus
  • excludes
    • 說明:該參數的用法跟includes是一樣的,不過這個參數的作用是排除指定的jar

示例:查看包沖突

比如我們查看一下commons-collections包的沖突
命令:
mvn dependency:tree -Dverbose -Dincludes=commons-collections
輸出:

[INFO] [dependency:tree]
[INFO] org.apache.maven.plugins:maven-dependency-plugin:maven-plugin:2.0-alpha-5-SNAPSHOT
[INFO] +- org.apache.maven.reporting:maven-reporting-impl:jar:2.0.4:compile
[INFO] |  \- commons-validator:commons-validator:jar:1.2.0:compile
[INFO] |     \- commons-digester:commons-digester:jar:1.6:compile
[INFO] |        \- (commons-collections:commons-collections:jar:2.1:compile - omitted for conflict with 2.0)
[INFO] \- org.apache.maven.doxia:doxia-site-renderer:jar:1.0-alpha-8:compile
[INFO]    \- org.codehaus.plexus:plexus-velocity:jar:1.1.3:compile
[INFO]       \- commons-collections:commons-collections:jar:2.0:compile

我們可以看到,2.1版本的jar被忽略掉了,因為maven是根據路徑最近原則來解析依賴,2.0版本路徑更短,所以2.0版本被引入,2.1版本路徑比2.0要長,所以被忽略。
使用這個命令可以輕松的查看包的引入路徑,包的沖突情況。
此處一定不要省略-Dverbose參數,要不然是不會顯示被忽略的包

3. dependency:analyze-only ---- 分析依賴

dependency:analyze-only命令可以分析整個項目,並且找出項目中依賴有如下情況的:

  • 聲明了並且使用了的依賴
  • 沒有聲明但是使用了的依賴
  • 聲明了但是沒有使用的依賴

輸出示例:

[INFO] Used declared dependencies found:
[INFO]    cn.hutool:hutool-all:jar:4.3.1:compile
[INFO]    com.google.guava:guava:jar:27.0.1-jre:compile
[WARNING] Used undeclared dependencies found:
[WARNING]    org.slf4j:slf4j-api:jar:1.7.25:compile
[WARNING]    org.springframework:spring-context:jar:4.3.22.RELEASE:compile
[WARNING]    org.springframework.boot:spring-boot-autoconfigure:jar:1.5.19.RELEASE:compile
[WARNING]    org.springframework.boot:spring-boot:jar:1.5.19.RELEASE:compile
[WARNING]    org.springframework:spring-beans:jar:4.3.22.RELEASE:compile
[WARNING]    junit:junit:jar:4.12:test
[WARNING] Unused declared dependencies found:
[WARNING]    org.springframework.boot:spring-boot-starter-test:jar:1.5.19.RELEASE:test
[WARNING]    com.h2database:h2:jar:1.4.197:test
[WARNING]    org.springframework.boot:spring-boot-starter:jar:1.5.19.RELEASE:compile
[WARNING]    com.fasterxml.jackson.core:jackson-databind:jar:2.8.11.3:compile
[WARNING]    org.projectlombok:lombok:jar:1.16.22:compile
[WARNING]    com.alibaba:fastjson:jar:1.2.55:compile

需要注意的是,如果你要查看聲明了並且使用了的依賴,必須加上參數-Dverbose

4. dependency:analyze-duplicate ---- 分析 and

這個命令會查找<dependencies/> 和 <dependencyManagement/>中重復聲明的依賴

5. dependency:list-repositories ---- 列出所有的遠程repositories

命令:
mvn dependency:list-repositories

輸出:

[INFO]        id: sonatype-nexus-snapshots
      url: https://oss.sonatype.org/content/repositories/snapshots
   layout: default
snapshots: [enabled => true, update => daily]
 releases: [enabled => false, update => daily]

[INFO]        id: apache.snapshots
      url: https://repository.apache.org/snapshots
   layout: default
snapshots: [enabled => true, update => daily]
 releases: [enabled => false, update => daily]

[INFO]        id: central
      url: https://repo.maven.apache.org/maven2
   layout: default
snapshots: [enabled => false, update => daily]
 releases: [enabled => true, update => daily]

6. dependency:purge-local-repository ---- 清理本地repository

這個命令的會首先解析整個項目的依賴,然后從本地repository中清理這些依賴,重新從遠程repository下載。

  • 直接依賴
    有一點要說清楚,這個命令默認的對所有的依賴項進行操作。所以它會在清除操作之前,下載某些缺失的依賴來收集完整的依賴樹信息。為了避免這些預下載的操作,你可以設置參數-DactTransitively=false,僅對項目的直接依賴進行操作。
  • 指定/排除依賴
    你也可以有針對性的只操作某些包,需要添加參數-Dincludes,明確的聲明包,這個是可以傳多個值的,用英文逗號分隔,舉例:dependency:purge-local-repository -Dincludes=org.slf4j:slf4j-api,org.slf4j:log4j-over-slf4j-Dexcludes也是一樣的道理,只不過是排除某些依賴。
  • 自定義清理
    如果你想清理不在本項目中的依賴,也可以使用這個,不過參數是不一樣的。mvn dependency:purge-local-repository -DmanualIncludes=org.apache:apache,參數-DmanualInclude可以讓你清理不在本項目中的依賴,但是不會重新解析依賴了,因為本項目不需要這些依賴。這個對清理parent pom,導入的pom,maven插件非常有用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM