15分鐘Maven

Maven

Scope

  1. provided - Dependencies that are required for compiling the project code, but that will be provided at runtime by a container running the code (e.g., the Java Servlet API).
  2. test - Dependencies that are used for compiling and running tests, but not required for building or running the project’s runtime code.

常用命令

mvn install

The install goal will compile, test, and package your project’s code and then copy it into the local dependency repository, ready for another project to reference it as a dependency.

mvn compile

編譯,target/classes/*

mvn package

打包,target/artifact + version .jar

忽略測(cè)試打包

mvn install -Dmaven.test.skip=true

Maven

包沖突

exclusions:忽略掉curator-framework對(duì)log4j的依賴(lài)

 <dependency>
            <groupId>com.netflix.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${curator_version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

dependencies VS dependencyManagement

Maven 使用dependencyManagement 元素來(lái)提供了一種管理依賴(lài)版本號(hào)的方式。通常會(huì)在一個(gè)組織或者項(xiàng)目的最頂層的父POM 中看到dependencyManagement 元素。使用pom.xml 中的dependencyManagement 元素能讓
所有在子項(xiàng)目中引用一個(gè)依賴(lài)而不用顯式的列出版本號(hào)。Maven 會(huì)沿著父子層次向上走,直到找到一個(gè)擁有dependencyManagement 元素的項(xiàng)目,然后它就會(huì)使用在這個(gè)dependencyManagement 元素中指定的版本號(hào)。

這樣做的好處不言而喻的。 因?yàn)椴恍枰教帍?fù)制版本號(hào),版本升級(jí)也狠簡(jiǎn)單,只要改住pom中的dependencyManagement中的版本。

Java編譯器版本設(shè)置

Intellij IDEA中Module設(shè)置里雖然有設(shè)置JDK編譯器版本(language level), 但是直接在父pom里設(shè)置可以一勞永逸!

編譯器插件配置

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
   <source>1.7</source>
   <target>1.7</target>
 </configuration>
</plugin>

發(fā)布二方庫(kù)

在root module的pom里指定倉(cāng)庫(kù)地址,正式版本倉(cāng)庫(kù)和snapshot

<distributionManagement>
   <repository>
       <id>grade2.releases.dist</id>
       <url>http://mvn.grade2-ent.com/nexus/content/repositories/releases</url>
   </repository>
   <snapshotRepository>
       <id>grade2.snapshots.dist</id>
       <url>http://mvn.grade2-ent.com/nexus/content/repositories/snapshots</url>
   </snapshotRepository>
</distributionManagement>

如果項(xiàng)目有很多模塊組成,部分子模塊不需要發(fā)布二方庫(kù),那么在子模塊的pom中添加:

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>${maven-deploy-plugin.version}</version>
      <configuration>
          <skip>true</skip>
      </configuration>
  </plugin>

多模塊升級(jí)版本號(hào)

  • 升級(jí)版本號(hào)
mvn versions:set -DnewVersion=2.50.1-SNAPSHOT
  • 如果出錯(cuò)了,可以revert,因?yàn)閙vn會(huì)備份pom文件
mvn versions:revert
  • 提交升級(jí),mvn會(huì)刪除備份的pom文件
mvn versions:commit

后記

其實(shí)是Maven雜。

要對(duì)得起標(biāo)題,還需要完善一下,哈哈

最后編輯于
?著作權(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)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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