Maven 的使用與技巧

Maven 的使用與技巧

Maven 的安裝

 

下載地址:http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.tar.gz

 

配置環境變量:

Maven 對應項目結構

Project name

--src

-----main

--------java

--------resources

-----test

--------java

--------resources

 

POM 文件介紹與基本組成

說明:全稱是Project Object Model,通俗點的話說就是要對構建的項目進行建模。

組成的基本元素:

元素

可選值

描述

groupId

 

分組ID

artifactId

 

模塊id

version

 

版本

packaging

 

打包類型:pom、jar、war

modelVersion

 

對應的超級pom 版本

dependencies

 

項目依懶包

 

 

maven repository 與鏡像地址

http://mvnrepository.com maven 倉庫用於查找所需要pom項目

http://repo1.maven.org/maven2/ 全球總倉庫1

http://repo2.maven.org/maven2/ 全球總倉庫2

http://maven.aliyun.com/nexus/content/groups/public/ 阿里雲鏡像倉庫

http://maven.oschina.net/content/groups/public/ oschina 鏡像倉庫

鏡像配置

修改settings.xml 文件

<mirrors>

<mirror>

<id>alimaven</id>

<name>aliyun maven</name>

<url>http://maven.aliyun.com/nexus/content/groups/public/</url>

<mirrorOf>central</mirrorOf>

</mirror>

<!-- 中央倉庫1 -->

<mirror>

<id>repo1</id>

<mirrorOf>central</mirrorOf>

<name>Human Readable Name for this Mirror.</name>

<url>http://repo1.maven.org/maven2/</url>

</mirror>

 

<!-- 中央倉庫2 -->

<mirror>

<id>repo2</id>

<mirrorOf>central</mirrorOf>

<name>Human Readable Name for this Mirror.</name>

<url>http://repo2.maven.org/maven2/</url>

</mirror>

</mirrors>

 

mvn 基本命令

mvn clean // 清理

mvn compile // 編譯

mvn test // 測試

mvn package // 打包

mvn install // 打包並上傳到本地倉庫

mvn depeloy // 上傳到遠程倉庫

mvn -Dmaven.test.skip=true // 跳過測試
 

maven 私服搭建

 

nexus 下載地址:

https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.5-02-bundle.tar.gz

解壓並設置環境變量

#解壓

shell>tar -zxvf nexus-2.14.5-02-bundle.tar.gz

#在環境變量當中設置啓動用戶

shell> vim /etc/profile

#添加profile文件。安全起見不建議使用root用戶,如果使用其它用戶需要加相應權限

RUN_AS_USER=root

配置啓動參數:

shell> vi ${nexusBase}/conf/nexus.properties

#端口號

application-port=8081

#倉庫地址

nexus-work=${bundleBasedir}/../sonatype-work/nexus  

 

啓動與停止nexus

#啓動

shell> ${nexusBase}/bin/nexus start

#停止

shell> ${nexusBase}/bin/nexus stop

登錄nexus 界面

地址http://{ip}:8081/nexus/

用戶名:admin

密碼admin123

 

手動更新索引

前往maven中央倉庫下載indexer-cli-5.1.1.jar

http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.apache.maven.indexer%22%20AND%20a%3A%22indexer-cli%22

下載索引文件

http://repo.maven.apache.org/maven2/.index/

nexus-maven-repository-index.gz

nexus-maven-repository-index.properties

解壓索引引文件

java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer

拷貝索引文件至索引目錄

{nexus-home}/sonatype-work/nexus/indexer/central-ctx

基本插件的應用

 

 

常用的插件-----------

設置jre 版本

兩種方式:第一種是局部項目修改 pom.xml 設置compiler 插件。第二種是全局修改setting.xml 文件 配 置profile

<!-- 修改compiler 插件已設置jre 源碼版本和編譯版本-->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.7</source>

<target>1.7</target>

</configuration>

</plugin>

<!-- 設置setting.xml 文件中 profile 添加屬性-->

<properties>

<maven.compiler.source>1.7</maven.compiler.source>

<maven.compiler.target>1.7</maven.compiler.target>

<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>

</properties>

 

生成一個源碼包:

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-source-plugin</artifactId>

<version>2.4</version>

<executions>

<execution>

<id>attach-source</id>

<phase>verify</phase>

<goals>

<goal>jar-no-fork</goal>

</goals>

</execution>

</executions>

</plugin>

 

測試指定範圍

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.20</version>

<configuration>

<includes>

<include>**/User*.java</include>

</includes>

</configuration>

</plugin>

 

生命週期 lifecycle

 

 

 

clean lifecycle : 構建前的清理工作

  • pre-clean 執行一些需要在clean之前完成的工作
  • clean 移除所有上一次構建生成的文件
  • post-clean 執行一些需要在clean之後立刻完成的工作

Default lifecycle構建的核心部分,編譯、打包、部署、上傳

  • validate 項目及所必須的環境驗證
  • initialize 初始化構建狀態,例如設置屬性或創建目錄。
  • generate-sources 生成次源包
  • process-sources
  • generate-resources
  • process-resources 複製並處理資源文件,至目標目錄,準備打包。
  • compile 編譯項目的源代碼。
  • process-classes
  • generate-test-sources
  • process-test-sources
  • generate-test-resources
  • process-test-resources 複製並處理資源文件,至目標測試目錄。
  • test-compile 編譯測試源代碼。
  • process-test-classes
  • test 使用合適的單元測試框架運行測試。這些測試代碼不會被打包或部署。
  • prepare-package
  • package 接受編譯好的代碼,打包成可發佈的格式,如 JAR 。
  • pre-integration-test
  • integration-test
  • post-integration-test
  • verify
  • install 將包安裝至本地倉庫,以讓其它項目依賴。
  • deploy 將最終的包複製到遠程的倉庫,以讓其它開發人員與項目共享。

site lifecycle:項目報告生成,站點文檔生成

  • pre-site 執行一些需要在生成站點文檔之前完成的工作
  • site 生成項目的站點文檔
  • post-site 執行一些需要在生成站點文檔之後完成的工作,並且爲部署做準備
  • site-deploy 將生成的站點文檔部署到特定的服務器上

原官方檔:

http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

 

 

 

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