GXPT環境搭建——Maven環境的搭建

  前兩篇博客,我們搭建了一個Maven的Nexus私服,這篇我們來共同學習一下Maven的配置和使用。

1、前提

  安裝mavne首先需要安裝JDK,並配置環境變量。這部分比較簡單,就不再做演示了。

2、安裝

  直接解壓文章末尾提供的apache-maven-3.1.1-bin.zip文件到D:\maven\apache-maven-3.1.1(可自定義文件位置)即可完成安裝。

3、配置

  1)、環境變量

  右鍵 我的電腦->屬性->高級系統設置->環境變量,進行如下設置。
   a、在用戶變量中添加MAVEN_HOME=D:\maven\apache-maven-3.1.1
   b、修改PATH,在配置中添加%MAVEN_HOME%\bin;
   c、調出命令行窗口(win + r 之後輸入cmd)

   d、輸入mvn –version,出現界面如下,則安裝Maven配置成功


  2)、settings.xml文件配置

  Maven中最重要的配置就是settings.xml文件的配置,其默認位置爲%MAVEN_HOME%\conf\settings.xml,相關具體配置如下:
    a、全局settings.xml位於%MAVEN_HOME%\conf\settings.xml,在該文件中配置的任何選項對於使用maven的所有應用程序均會產生影響,且影響力最大。(該文件如果不做任何修改,在第一次啓動maven之時會在當前用戶的文件夾下建立一個.m2的文件夾,其中存放了maven本地的所有jar文件)。所以,這裏的settings.xml一般不動。




    b、用戶settings.xml,拷貝%MAVEN_HOME%\conf\settings.xml文件到當前系統用戶文件下的.m2下的settings.xml文件,並在其中修改爲自定義的maven本地倉庫存放位置(用戶settings.xml並非一開始就有,它的意義在於不修改maven全局配置的情況下,更加合理對的配置用戶自己的maven配置文件)。我們只在這裏保存一份自定義的settings.xml,配置本地倉庫(下載的jar都會放入到配置的D:\maven\repository下)。到時候構建項目的時候,需要用到的jar包,首先回來自定義倉庫中尋找,如果沒有找到,就從私服下載到這個倉庫中!


  <!-- 自定義本地倉庫存放位置 -->
	<localRepository>D:\maven\repository</localRepository>



  c、自定義settings.xml文件,即拷貝%MAVEN_HOME%\conf\settings.xml文件到當前自定義用戶自定義的maven本地倉庫存放位置的同級目錄下(自定義settings.xml同用戶settings.xml一樣,它的出現也是爲了更加合理的使用maven的配置文件。)


<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
		  
	<!-- 設置本地倉庫路徑 -->
	<localRepository>D:/maven/repository</localRepository>
	
	<!-- 設置發佈 jar 包時的用戶名及密碼 -->
	<servers>
		<server>
			<id>releases</id>
			<username>admin</username>
			<password>admin123</password>
		</server>
				
		<server>
			<id>snapshots</id>
			<username>admin</username>
			<password>admin123</password>
		</server>		
	</servers>
	
	<!-- 設置 maven 的遠程倉庫爲 nexus -->
	<mirrors>
		<mirror>
			<id>nexus</id>
			<mirrorOf>*</mirrorOf>
			<name>Local Repository</name>
			<url>http://192.168.24.128:8081/nexus/content/groups/public/</url>
		</mirror>
	</mirrors>
	
	<!-- 設置 nexus 的路徑等 -->
	<profiles>
		<profile>
			<id>nexus</id>
			<repositories>
				<repository>
					<id>central</id>
					<name>local private nexus</name>
					<url>http://localhost:8081/nexus/content/groups/public</url>
					<releases><enabled>true</enabled></releases>
					<snapshots><enabled>true</enabled></snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>central</id>
					<name>local private nexus</name>
					<url>http://localhost:8081/nexus/content/groups/public</url>
					<releases><enabled>true</enabled></releases>
					<snapshots><enabled>true</enabled></snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
	</profiles>

	<!-- 激活 nexus -->
	<activeProfiles> 
		<activeProfile>nexus</activeProfile>
	</activeProfiles>
	
	<!-- 配置eclipse插件 -->
	<pluginGroups>
		<pluginGroup>org.mortbay.jetty</pluginGroup>	
		<pluginGroup>org.codehaus.cargo</pluginGroup>		
	</pluginGroups>
	
</settings>

 這個配置文件配置內容:

  1、本地倉庫的路徑

  2、設置發佈 jar 包時的用戶名及密碼。我們把各個模塊構建成了jar或者war文件,發佈到私服nexus中需要用到。

    3、設置 maven 的遠程倉庫爲 nexus並激活。項目添加jar依賴後,會從設置的本地倉庫中查找,如果查找到了,自動添加到項目中;如果沒有查到,會訪問我們配置好的maven中央倉庫,而這裏使用<mirror>元素,攔截了對所有倉庫的訪問,換言之,對任何倉庫的訪問都會轉成對nexus/content/groups/public倉庫的訪問,而這個倉庫是一個倉庫組,保持了對所有倉庫的訪問。

  至此,Maven配置完成。

  原先以爲Maven只是一個構建工具,但其實Maven藉助其豐富的插件實現了測試,打包,發佈,構建等衆多功能,如果有興趣可以深入瞭解一下。




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