Maven日常 —— 你应该知道的一二三
以前在日常工作中,使用Maven只是机械的执行Maven clean、Maven install,对其中的原理与过程并无了解,近期阅读了《Maven实战》,对Maven有了更深入的理解。
本篇就针对读书后的理解,对Maven的日常使用相关的知识进行总结与归纳。
如果想要了解更多内容参考:《Maven实战》
1 什么是Maven?
如果没有Maven,你可能不得不经历下面的过程:
1 如果使用了spring,去spring的官网下载jar包;如果使用hibernate,去hibernate的官网下载Jar包;如果使用Log4j,去log4j的官网下载jar包.....
2 当某些jar包有依赖的时候,还要去下载对应的依赖jar包
3 当jar包依赖有冲突时,不得不一个一个的排查
4 执行构建时,需要使用ant写出很多重复的任务代码
5 当新人加入开发时,需要拷贝大量的jar包,然后重复进行构建
6 当进行测试时,需要一个一个的运行....检查
有了Maven,它提供了三种功能:
1 依赖的管理:仅仅通过jar包的几个属性,就能确定唯一的jar包,在指定的文件pom.xml中,只要写入这些依赖属性,就会自动下载并管理jar包。
2 项目的构建:内置很多的插件与生命周期,支持多种任务,比如校验、编译、测试、打包、部署、发布...
3 项目的知识管理:管理项目相关的其他内容,比如开发者信息,版本等等
2 Maven如何管理jar包
关于jar包的坐标,有过使用经验的都应该有所了解,maven是通过groupId,artifactId,以及version确定一个唯一的jar包。
这部分的内容可以参考前一篇:构建过程
例如,最常使用的Junit的声明就是如下:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope> 这是声明的范围,不同的生命周期所要求的范围是不一样的,详情参考《Maven实战》
</dependency>
首先先来说说Maven下载jar包的过程:

在Maven中会涉及到几种仓库:
1 工作空间,即我们的项目工程,这里面可能会放着pom.xml文件,这个pom.xml就是maven的配置文件
2 本地仓库,本地仓库用于存放jar包,其实Jar包并不是直接放入工作空间的,它是存放在本地仓库,然后在执行发布打包的时候,添加依赖路径
3 私库:私库是使用者自己搭建的maven仓库,用于缓解频繁从外网下载jar包资源的压力。而且使用私库作为缓存层,也相对安全一些。
4 共享仓库:书中所说的中央仓库或者一些常用的镜像网站都属于这种,国内比较著名的oschina以及163都是不错的maven仓库。
当我们在pom中声明了依赖关系后,参考上面的图:
1 Maven在执行相关的任务时,会先去本地仓库查看是否有该资源,如果有的话,判断版本是否正确,如果一切都没问题则直接使用;否则,执行下一步
2 Maven会去配置的共享仓库中查找,如果找到就拷贝到本地仓库中;找不到则会给出相关的提示
3 Maven在本地如果搭建了私库,则会去私库中查找,找到就拷贝到本地仓库;找不到就会去共享仓库中查找,然后放入私库和本地库。有了私库,局域网内的开发者可以共享依赖,就不用每个人都去外网下载jar包,浪费带宽了。
关于本地仓库和共享仓库的配置都在settings.xml中,这个文件位于conf中,如果没有则拷贝一份即可。
<?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.home}/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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
--> <!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
--> <!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
--> <!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups> <!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<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
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
|-->
<servers>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
--> <!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
</servers> <!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
</mirrors> <!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
|
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
|
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
|
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
| repositories, plugin repositories, and free-form properties to be used as configuration
| variables for plugins in the POM.
|
|-->
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id> <activation>
<jdk>1.4</jdk>
</activation> <repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
--> <!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id> <activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation> <properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
</profiles> <!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>
其中本地仓库的配置为:
<localRepository>F:\apache-maven-3.3.9\repo</localRepository>
默认是在 用户的本地目录/.m2/repository中。
共享仓库的地址配置为:
<mirrors>
<mirror>
<id>CN</id>
<name>OSChina Central</name>
<url>http://maven.oschina.net/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
3 Maven的生命周期与阶段
Maven中有三大生命周期,他们相互独立,分别是:
1 clean 清理
2 default 构建
3 site 建站
一般来说,clean和default比较常用。
每个生命周期又有不同的阶段,阶段按顺序执行,并且可以指定执行结束的阶段。构建的时候,会依次从最上面的阶段执行到指定的那个阶段。
比如,clean有3个阶段:
1 pre-clean 执行清理前要完成的工作
2 clean 清理上一次构建生成的文件
3 post-clean 执行清理后需要完成的工作
当我们输入mvn clean的时候,执行的是pre-clean和clean两个阶段。
default的阶段比较多:
1 validate
2 initialize
3 generate-sources
4 process-sources
5 generate-resources
6 process-resources
7 compile
8 process-classes
9 generate-test-sources
10 process-test-sources
11 generate-test-resources
12 process-test-resources
13 test-compile
14 process-test-classes
15 tet
16 prepare-package
17 package
18 pre-integration-test
19 integration-test
20 post-integration-test
21 verify
22 install
23 deploy
看名字大概就能理解,当执行mvn install的时候,实际会执行validate-->initialize-->...-->verify-->install等二十几个阶段。
为了操作方便,不同的声明周期可以在一起执行,比如mvn clean install,会先执行clean的阶段,在执行install的阶段。
在IDE开发环境中,当我们Run as的时候,就可以执行maven clean进行清理,或者执行maven install进行构建,也可以执行maven build同时执行clean和install两个任务。
基本上了解上面两个知识点,就足够日常工作使用了。当然Maven可不止这么一点点的东西,比如它还涉及到自定义构建任务、间接依赖的管理、插件的使用、私库的搭建等等,如果需要的话,读者可以参考《Maven实战》,这本书讲述的很全面了。
Maven日常 —— 你应该知道的一二三的更多相关文章
- maven相关基础
0. 本文主要参考一下良心maven原创文摘: 0.0 maven官网传送门 http://maven.apache.org/ 0.1 maven日常 http://www.cnblogs.com/x ...
- Maven的日常
强烈建议把 Maven 的 settings.xml 文件同时放在:%USER_HOME%/.m2/settings.xml 和${maven.home}/conf/settings.xml 两个地方 ...
- Jenkins日常运维笔记-重启数据覆盖问题、迁移、基于java代码发版(maven构建)
之前在公司机房部署了一套jenkins环境,现需要迁移至IDC机房服务器上,迁移过程中记录了一些细节:1)jenkins默认的主目录放在当前用户家目录路径下的.jenkins目录中.如jenkins使 ...
- Maven在IDEA中的日常使用
1.为什么使用MavenMaven是我们在开发过程中常用的工具,主要用途有两种:1)方便的下载jar包2)项目打包接下来以windows操作系统为例,介绍一下Maven在IDEA中如何设置和常用的功能 ...
- 日常开发需要掌握的Maven知识
文章来自:https://www.jianshu.com/p/e224a6dc8f20和https://www.jianshu.com/p/20b39ab6a88c Maven出现之前 jar包默认都 ...
- 【抖音测试开发日常实习】一二三四面面经,攒人品求Offer
总结: 一.流程 (估计)一面二面技术leader面,三面部门领导面,四面肯定是HR面 二.系统 用的牛客网面试系统,长相大概是这样: 打字聊天框我在听不清对面声音的时候用过,感觉没啥卵用,手撕代 ...
- 理解Maven中的SNAPSHOT版本和正式版本
Maven中建立的依赖管理方式基本已成为Java语言依赖管理的事实标准,Maven的替代者Gradle也基本沿用了Maven的依赖管理机制.在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个 ...
- 对Maven、gradle、svn、spring 3.0 fragment、git的想法
1.Maven Maven可以构建项目,采用pom方式配置主项目和其他需要引用的项目.同时可结合spring3.0的新特性web fragment. 从现实出发,特别是对于管理不到位,程序员整体素质 ...
- Maven在Eclipse中的实用小技巧
前言 我们在开发的工程中很多都是Maven项目,这样更加便于我们jar包的管理.而我们一般使用的IDE都是Eclipse,由于我们在日常的开发过程中会经常要用到一些Maven的操作,所以我今天 ...
随机推荐
- 用js把数据从一个页面传到另一个页面
用js把数据从一个页面传到另一个页面的层里? 如果是传到新页面的话,你网站基于什么语言开发直接用get或者post获取,然后输出到这个层 通过url传参 如果是HTML页面的话JS传到新页面就wind ...
- asp.net mvc自定义JsonResult类来防止MaxJsonLength超过限制
前不久在做一个项目的时候,我用到了mvc的webapi返回了一个大数据,结果报了500错误,如下图所示: Server Error in ‘/’ Application. Error during s ...
- Tsinsen A1493 城市规划(DP + CDQ分治 + NTT)
题目 Source http://www.tsinsen.com/A1493 Description 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了. 刚才说过, 阿狸的国家有n个城市, 现在 ...
- 关于在header里增加参数的方式
在使用一个API的时候,文档里写的返回值类型是json,可是试了下返回的明明是xml,还小小的鄙视了一把. 可是解析xml,好麻烦的.最好是json可以直接decode . 意外看到文档下面有一句 J ...
- iMetro
body { background:#FFFFFF url("http://images.cnblogs.com/cnblogs_com/mookmark/745172/o_8.jpg&qu ...
- js判断图片是否加载完成
var img = new Image(); //新建一个图片对象:img.src = ...; //图片地址是你准备要加载的地址:if(img.complete){ //表示图片已经加载完成}
- linux进程后台运行及输出重定向
本机环境为ubuntu 14.04 以ping www.baidu.com为例: 1.程序的前台运行 ping www.baidu.com 可以看到,屏幕上输出了baidu返回的结果 2.实现程序后台 ...
- Unity3d刚体Rigidbody与碰撞检测Collider
做了一个碰撞的小Demo,用一个球去撞击一堵墙,结果在球和墙都设置了刚体和碰撞体的情况下,球穿过了墙.移动球的位置,球有时能穿过墙,有时会被墙阻挡. 对于球穿过了墙,这个问题,在网上找了一下答案,基本 ...
- nginx config
from : http://www.ha97.com/5194.html 更详细的模块参数请参考:http://wiki.nginx.org/Main #定义Nginx运行的用户和用户组user ww ...
- 《寒江独钓_Windows内核安全编程》中修改类驱动分发函数
最近在阅读<寒江独钓_Windows内核安全编程>一书的过程中,发现修改类驱动分发函数这一技术点,书中只给出了具体思路和部分代码,没有完整的例子. 按照作者的思路和代码,将例子补充完整,发 ...