Maven入门浅析

目录

一、 Maven是什么?

二、 Maven使用带来便捷之处.

三、 Maven的基本概念理解.

四、 Maven项目结构目录.

五、 Maven的settings.xml文件.

六、 Maven的pom.xml文件.

七、 Maven坐标.

八、 Maven的生命周期.

九、 Maven依赖传递版本的确定.

十、 Maven常用命令.

附件1-settings.xml源配置.

附件2-pom.xml源配置源码.


一、 Maven是什么?

1.1 maven是一款自动化构建工具,专注java平台项目构建和依赖管理。

  • 项目构建:【清理】→【编译】→【测试】→【打包】→【安装】→【部署】

    【清理】:清理之前编译好的class等文件,为重编译做准备。

    【编译】:将java源程序编译为字节码文件。

    【测试】:对项目关键点进行测试。

    【打包】:将项目工程封装成一个jar或war文件,用于安装和部署

    【安装】:将打包结果安装到本地仓库中。

    【部署】:将打包结果部署到远程仓库或服务器上。

  • 依赖管理:项目依赖的库可能存在又依赖其它基础库,针对使用maven则只需要关注你的直属依赖,其它库maven会隐式自动依赖。

二、 Maven使用带来便捷之处.

2.1 在B/S开发过程中不使用maven也照样开发。那么我们为什么还要使用maven呢?区别从以下三点探讨。

  • 添加第三方jar包。

  • jar包的依赖关系。

  • jar包的获取

2.2 举例:(不使用maven)

  • 每个项目各自使用的jar都要复制到各自项目的lib下才能使用。

  • 需要用到的直属jar可能存在依赖其他jar包,都需要手动引入。

  • 需要网上海量搜索。

2.3 举例:(使用maven)

  • 提供仓库管理。

  • 引入直属jar包,自动引入基础依赖jar包。

  • 通过maven规范引入。(maven中央仓库URL: https://mvnrepository.com/ )

三、 Maven的基本概念理解.

四、 Maven项目结构目录.

五、 Maven的settings.xml文件.

六、 Maven的pom.xml文件.

七、 Maven坐标.

7.1 通过以下三点向量可以唯一确定仓库中的jar包(理解等同XYZ确定可以确定一个平面)

  • groupId:公司或者组织域名倒序+当前项目名称

  • artifactId:当前项目的模块名称

  • version:当前模块的版本

    导入pom.xml示例:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.1.3</version>
    </dependency>

7.2 依赖scope属性

  • compile (编译范围):适用编译范围,且被打包入项目。

  • provided (已提供范围): 已提供范围的依赖在编译和测试,不可以传递,也不会被打包。

    使用场景:使用servlet-api包的时候,启动tomcat就会报错,因为tomcat中也包含servlet-api所以导入包应设置提供范围这样启动就没问题provided。

  • runtime (运行时范围): 依赖在运行和测试系统的时候需要,但在编译的时候不需要。

  • test (测试范围): 编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用

  • system (系统范围): 与provided 类似,需提供一个jar路径用来编译时使用。

7.3 依赖exclusions属性

​ 用maven管理库依赖,有个好处就是连同库的依赖的全部jar文件一起下载,免去手工添加的麻烦,但同时也带来了同一个jar会被下载 了不同版本或需用的包。

  • 单个依赖排除

    <exclusions>
    <exclusion>
       <groupId>单个组织ID坐标</groupId>
      <artifactId>单个模块ID坐标</artifactId>
    </exclusion>
     </exclusions> 
  • 多依赖排除

    <exclusions>
      <exclusion>
       <groupId>*</groupId>
       <artifactId>*</artifactId>
      </exclusion>
    </exclusions>

八、 Maven的生命周期.

​ Maven有三套相互独立的生命周期,分别是clean、default和site。每个生命周期包含一些阶段(phase),阶段是有顺序的,后面 的阶段依赖于前面的阶段。

8.1 clean生命周期:清理项目,包含三个phase。

  • pre-clean:执行清理前需要完成的工作。

  • clean:清理上一次构建生成的文件。

  • post-clean:执行清理后需要完成的工作。

8.2 default生命周期:构建项目,重要的phase如下。

  • validate:验证工程是否正确,所有需要的资源是否可用。
  • compile:编译项目的源代码。
  • test:使用合适的单元测试框架来测试已编译的源代码。这些测试不需要已打包和布署。
  • Package:把已编译的代码打包成可发布的格式,比如jar。
  • integration-test:如有需要,将包处理和发布到一个能够进行集成测试的环境。
  • verify:运行所有检查,验证包是否有效且达到质量标准。
  • install:把包安装到maven本地仓库,可以被其他工程作为依赖来使用。
  • deploy:在集成或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享。

8.3 site生命周期:建立和发布项目站点,phase如下

  • pre-site:生成项目站点之前需要完成的工作。

  • site:生成项目站点文档。

  • post-site:生成项目站点之后需要完成的工作。

  • site-deploy:将项目站点发布到服务器。

九、 Maven依赖传递版本的确定.

资源包与包之前可能存在重复的间接依赖,那怎么确认maven打包会使用哪个版本呢?具体规则如下:

  • 工程依赖树上,依赖越浅,优先级越大。

  • 工程依赖树上都处于同一级,则依照pom.xml的导入顺序,越前优先级越大。

【示例资源如下三包】:

<dependency>
   <groupId>org.apache.activemq</groupId>
   <artifactId>activemq-optional</artifactId>
   <version>5.5.0</version>
 </dependency>

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>3.0.2.RELEASE</version>
 </dependency>

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jms</artifactId>
   <version>3.0.2.RELEASE</version>
 </dependency>

【示例(工程依赖树上,依赖越浅,优先级越大)】:通过以下依赖来观察maven打包版本的使用优先级

​ activemq-optional中包含了sping-jms5.2.8版本,spring-jms引入版本是3.0.2版本,打包项目后项目中存在不是5.2.8而是第一级别的3.0.2,具体如下图:

打包项目后项目中存在不是5.2.8而是第一级别的3.0.2,具体如下图:

十、 Maven常用命令.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
清除:mvn clean
编译源码:mvn compile
运行测试:mvn test
打包:mvn package
安装:mvn install
部署:mvn deploy
只打包不测试:mvn -Dtest package
只打jar包:mvn jar:jar
源码打包:mvn source:jar
查看依赖树:mvn dependency:tree
组合命令:mvn clean install (清除安装)
	 mvn clean package(清除打包)
         mvn clean install -Pproduct(清除安装对应product环境)
         mvn test compile(测试编译)

附件1-settings.xml源配置.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<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:/ENV/apache-maven-3.6.1/repository</localRepository>
  
<!-- 插件组 -->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>
  
<!-- http代理组 -->
  <proxies>
    <!-- 
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>
  
<!-- 认证配置 -->
  <servers>
	<server>
    <id>releases</id>
    <username>admin</username>
    <password>Sin1/2=30</password>
   </server>
   <server>
    <id>snapshots</id>
    <username>admin</username>
    <password>Sin1/2=30</password>
   </server>
  </servers>

<!-- 镜像列表 -->
  <mirrors>
	 <mirror>
		<!-- 镜像唯一标识 -->
        <id>nexus-central</id>
		<!-- 镜像名称 -->
        <name>Nexus Central</name>
		<!-- 镜像地址:构建地址优先使用该地址,而非默认服务器URL -->
        <url>http://nexus.youmengbest.com/repository/maven-public/</url>	
		<!-- 被镜像的服务器Id,必须和中央仓库Id一致 -->
        <mirrorOf>central</mirrorOf>
    </mirror>
	<!-- 阿里云镜像 -->
    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>alimaven-central</mirrorOf>
    </mirror>
  </mirrors>

<!-- 配置信息组 -->
  <profiles>
    <profile>
      <id>mycof</id>
	  <!-- 配置仓库地址 -->
      <repositories>
        <repository>
          <id>nexus</id>
          <name>name</name>
          <url>url</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
  </profiles>

<!-- 激活信息配置 -->
  <activeProfiles>
    <activeProfile>mycof</activeProfile>
    <!-- <activeProfile>……</activeProfile> -->
  </activeProfiles>
 
</settings>

附件2-pom.xml源配置源码.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?xml version="1.0" encoding="UTF-8"?>

-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">

<!-- pom模板版本 -->
<modelVersion>4.0.0</modelVersion>

<!-- maven打包类型 -->
<packaging>war</packaging>

<!-- 基础配置 -->
<groupId>com.deyuanit.demo</groupId>
<artifactId>dy-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>

<!-- 项目名称和描述 -->
<name>projectName</name>
<description>Demo project</description>

<!-- 统一版本管理 -->
-<properties>
<!-- base -->
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.compilerVersion>${java.version}</maven.compiler.compilerVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.3.RELEASE</spring-boot.version>
</properties>

<!-- 依赖配置列表-->
-<dependencies>

<!-- 如:内嵌容器 -->
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
-<exclusions>

<!--排除tomcat依赖-->
-<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<!-- 配置依赖管理 -->
-<dependencyManagement>
-<dependencies>
-<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<!-- 主要用于环境区分配置(不同环境使用不同配置) -->
-<profiles>

<!-- 开发环境 -->
-<profile>
<id>dev</id>
-<properties>
<activatedProperties>dev</activatedProperties>
</properties>
-<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>

<!-- 测试环境 -->
-<profile>
<id>beta</id>
-<properties>
<activatedProperties>beta</activatedProperties>
</properties>
</profile>

<!-- 生产环境 -->
-<profile>
<id>prod</id>
-<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>

<!-- 构建配置(构建项目相关信息) -->
-<build>
-<resources>
-<resource>
<directory>src/main/resources</directory>
-<excludes>
<exclude>application-**.yml</exclude>
</excludes>
<filtering>true</filtering>
</resource>
-<resource>
<directory>src/main/resources</directory>
-<includes>

<!-- activatedProperties与上profiles配置对应 -->
<include>application-${activatedProperties}.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
-<plugins>
-<plugin>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
-<executions>
-<execution>
-<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值