Maven(==约定大于配置==)

用途:

  1. 再写Java程序的时候,有的时候要导入大量的jar包,我们要导入
  2. 我们想要一个东西来帮我们自动导包

一、环境变量的配置

  1. 配置M2_HOME: 安装maven的bin目录下
  2. 配置MAVEN_HOME:安装maven的目录
  3. 在path下面配置MAVEN_HOME的bin目录;就是M2_HOME的位置
1
2
3
4
5
#配置成功后
Maven home: D:\JAVA\apache-maven-3.6.3-bin\apache-maven-3.6.3\bin\..
Java version: 9.0.4, vendor: Oracle Corporation, runtime: D:\JAVA\jdk
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

二、阿里云镜像配置

配置setting.xml

1
2
3
4
5
6
  <mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>

三、本地仓库

自己建一个文件夹在加进去

1
<localRepository>D:\JAVA\apache-maven-3.6.3-bin\apache-maven-3.6.3\maven_repo</localRepository>

四、ideal使用maven

1.新建一个maven目录创建一个maven项目,可以选择有模板和没模版的;二者的区别也不一样。

image-20200724134718443

image-20200724135123957

image-20200724140517641

2.在创建的maven web模板中加入Java(resource root)和resources(resource root);

3.配置tomcat,在ideal中

image-20200724170428914

Warning: No artifacts marked for deployment:警告:没有标记为部署的项目,要添加一个项目

4.启动Tomcat

image-20200724171302317

1
http://localhost:8080/Saxon_web_maven_war/ <!-- 配置完的就是显示后缀名->

5.pom.xml(pom文件)

maven的核心文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?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>
<!-- 刚才配置的GAV-->
<groupId>org.example</groupId>
<artifactId>Saxon_web_maven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<!--jar:java 程序的包
war:javaweb 应用的包-->
<!-- 自己设置的名字-->
<name>Saxon_web_maven Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<!-- 默认的构建编码-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 编码版本-->
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<!--项目依赖-->
<dependencies>

<dependency> <!-- 具体依赖的包-->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--以后用要用的包 可以连他依赖的包一起下下来 <dependency> </dependency> 用这个包起来的 -->
<build>
<finalName>Saxon_web_maven</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

由于maven的约定大于配置,所以我们在Java文件夹上面如果写的文件不是Java文件,就导不出去。需要在build中配置一下resource文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

修改项目默认生成的文件夹WEB-INF里面的web.xml让版本提高,更好的一个测试环境,不是target

原版:

1
2
3
4
5
6
7
8
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>

更改可以参考tomcat的web.xml

五、maven仓库

第一次使用某一个包的时候,他没在我们本地的仓库中,我们就要去网上的maven仓库寻找我们要找的资源

1
2
3
4
5
6
7
<dependency>   <!--    具体依赖的包-->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>