1、Maven背景
項目中jar包資源越來越多,jar包中的管理越來越繁瑣
要為每個項目手動導入所需的jar,需要搜集全部jar
項目中的jar包如果需要版本更新,就需要再重新搜集jar
項目的jar在不同的項目中儲存了多份,存儲備援,散亂
java項目需要一個統一的便捷管理方案
2、簡介
Maven是一個基于項目對象模型的概念的純Java開發的開源的項目管理工具。
主要來管理java項目,進行依賴管理(jar包依賴管理)和項目建構(項目編譯,打包,測試,部署)。
此外還能分塊開發,提高開發效率。
3、Maven安裝
網址:https://maven.apache.org/
- 下載下傳後解壓
- 配置環境變量
- M2_HOME maven目錄下的bin目錄
- MAVEN_HOME maven的目錄
- 系統path中配置 %MAVEN_HOME%\bin
- 測試安裝是否成功
- mvn -version
(配置好變量後,并確定配置無誤。若測試時,仍發現有問題,比如在管理者模式下才可以成功,而普通模式下不能成功,則嘗試移動環境變量的順序,比如将其放到最上面,或将maven環境和java環境都移到最上面。)
4、maven配置
4.1添加本地倉庫
maven的conf目錄下settings.xml是maven的配置檔案,對其做如下配置
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
在apache-maven-3.8.1目錄下建一個maven-repo檔案夾
MAVEN中存儲jar包的位置
-->
<localRepository>D:\APPinstall\apache-maven-3.8.1\maven-repo</localRepository>
4.2、配置jdk
<profile>
<id>yourjdk</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<activeProfiles>
<activeProfile>yourjdk</activeProfile>
</activeProfiles>
<!--使配置生效-->
5、倉庫
5.1、本地倉庫
D:\APPinstall\apache-maven-3.8.1\maven-repo
在Settings.xml中配置的maven-repo目錄
5.2、遠端倉庫
- 私服
- 公共倉庫
- 除中央倉庫外,還有其他公共倉庫
- 中央倉庫在國外,下載下傳依賴速度慢,是以要配置一個國内的公共倉庫代替中央倉庫(鏡像)
-
<!--settings.xml中添加以下配置--> <mirror> <id>aliyun</id> <!--中心倉庫的mirror(鏡像)--> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <!-- aliyun倉庫位址以後所有要指向中心倉庫的請求,都會指向aliyun倉庫--> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
- 中央倉庫
- 中央倉庫的位址:http://repo.maven.apache.org/maven2/
- 不需配置
6、idea-maven
6.1、在idea中關聯maven
6.2、基于Maven建構項目
- 建構一個空maven項目
- 定義項目辨別
- 組,公司的域名
- 項目名
- 版本
- 确定項目資訊
6.3、maven項目結構
- src--------------------------項目中所有内容的存放位置
- main----------------項目所有的主程式以及相關的配置檔案
- java----------放置所有的主程式.java檔案
- resources-相關的配置檔案
- test-----------------所有測試程式以及配置檔案
- java----------放置所有的測試程式.java檔案
- resources-測試程式相關的配置檔案(自己添加)
- pom.xml---------項目的描述檔案
-
<?xml version="1.0" encoding="UTF-8"?> <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.xiaodu</groupId> <artifactId>mavenTest5_8</artifactId> <version>1.0-SNAPSHOT</version> <!--設定項目的類型 jar(預設) war--> <packaging>jar</packaging> <!--定義項目所需要的的依賴--> <dependencies> <!--依賴--> </dependencies> </project>
-
- main----------------項目所有的主程式以及相關的配置檔案
配置辨別
6.4、導入依賴jar
- 建好項目後,需要導入需要的jar
- 每個建構都有自己的辨別(groupld+artifactid+version)
- 在maven項目中隻需要配置坐标,maven便會自動加載對應的依賴删除坐标則會移除依賴
- 查找依賴
-
https://mvnrepository.com/
-
6.5、web依賴
導入JSP,servlet,JSTL依賴,是項目具有web編譯環境
<dependency>
<!--jstl支援-->
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<!--servlet支援-->
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<!--jsp編譯環境-->
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
main下級目錄建構webapp目錄
webapp目錄下建WEB-INF目錄
webapp目錄下建web.xml檔案,web.xml模闆如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xm1/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--這是一個空白的web.xml檔案模闆-->
</web-app>
配置serclet
java目錄下建com.test01.servlet包,建HttpServlet子類
package com.test01.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/shine")
public class ShineServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("shine servlet");
//轉發跳轉 index.jsp
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
webapp目錄下建index.jsp檔案
配置Tomcat
6.6、maven資源到處失敗的問題
<!--在build中配置resources,來防止我們資源導出失敗的問題-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
***.xml false ```