maven入門(一)


現在基本上很多項目開始使用maven進行構建項目,已經不是直接在lib文件夾下統一放置jar包,所以還是有必要學習掌握下maven的。

針對maven。這篇文章主要介紹一下幾點,大家如果都明白了,就可以參照這個思維導圖,再復習下,畢竟知識長時間不用,會忘記的。
enter image description here

一、 maven 是什么

Maven的Apache公司開源項目,是項目構建工具,也用來依賴管理。

二、maven的好處

1、由於maven構建的項目是沒有jar包的,所以項目的大小上,肯定是maven的項目比較小。
2、jar包統一交給maven管理。
3、maven同樣可以進行項目構建。

maven主要就是 項目構建和依賴(jar包)管理

三、maven安裝

maven程序安裝前提:maven程序java開發,它的運行依賴jdk。

1、 首先去Maven官網,下載Maven的包,地址為http://maven.apache.org/download.cgi
2、下載完解壓,然后配置一下環境變量,和JDK的環境變量配置類似(如圖)
enter image description here

enter image description here
3、查詢maven版本信息
enter image description here
4、 配置本地倉庫
找到 解壓目錄下的 config/setting.xml,我的就是 E:\app\apache-maven-3.5.3\conf\setting.xml
主要修改2個地方
4.1 修改本地倉庫路徑
enter image description here
4.2 修改成阿里雲鏡像

	<mirror>
		<id>nexus-aliyun</id>
		<mirrorOf>*</mirrorOf>
		<name>Nexus aliyun</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public</url>
	</mirror>

enter image description here

4.4 復制剛剛設置好的setting.xml 到你設置的本地倉庫路徑 我的是E:\dev_maven
enter image description here

5.倉庫類型有哪些
enter image description here

四、 使用maven構建項目

這里我是用eclipse 進行創建的

1、eclipse配置

1.1 配置maven程序
enter image description here
1.2 配置userSetting ,知道倉庫位置
enter image description here
1.3 構建索引,方便查找jar包(Window->show view ->maven Repository)
enter image description here

2、開始創建項目
2.1 這里選擇普通項目- Maven Project ,點擊next
http://p7zk4x9pv.bkt.clouddn.com/maven/TIM%E6%88%AA%E5%9B%BE20180430165750.png

enter image description here

2.2 打包方式選擇war ,完成。
enter image description here

2.3 web.xml缺失報錯

此時,會報錯,需要在src-main-webapp 下面創建 WEB-INF/web.xml

目錄結構
enter image description here

web.xml 內容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

</web-app>

同時創建一個index.xml_(src\main\webapp\index.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello maven</h1>
</body>
</html>

設置jdk編譯版本為1.8.默認為1.5
修改pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zhb</groupId>
  <artifactId>maven_hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  
  	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.5.1</version>  
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


更新maven
enter image description here

  1. 運行項目

enter image description here

tomcat:run

enter image description here

4.大功告成
enter image description here
enter image description here

五、maven 常用命令

clean:清理

將項目根目錄下target目錄清理掉

compile:編譯

將項目中.java文件編譯為.class文件

test:單元測試

單元測試類名有要求:XxxxTest.java
將項目根目錄下src/test/java目錄下的單元測試類都會執行。

package:打包

web project --- war包
java project --- jar 包
將項目打包,打包項目根目錄下taget目錄。

install:安裝

本地多個項目公用一個jar包。
打包到本地倉庫

這里大家自己嘗試一下,進入工程目錄里面
如打包 則執行 mvn package
enter image description here
enter image description here

2018年5月20日更新

我現在發現 我先在使用idea開發,大家可以在自己電腦的c盤的,m2 文件夾下將setting.xml 考到里面,這樣本地倉庫就是你設置的了

好了,寫了好久。終於弄完了。玩的開心。


免責聲明!

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



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