如何按照mvn以及在idea中配置maven

1、 Maven概述
1.1 Maven简介
Maven翻译为“专家”,“内行”。 Maven是Apache下的一个纯java开发的开源项目。
Maven 是一个项目管理工具,可以对 Java 项目进行构建、依赖管理。
Maven 也可被用于构建和管理各种项目,例如 C#, Ruby, Scala 和其他语言编写的项目。 Maven创始者希望能够更多的让Java开发人员的日常工作更加容易,帮助理解任何基于Java项目。
1.2 Maven的两大功能
项目构建和依赖管理。
1.2.1项目构建
项目构建是一个项目从:源代码、编译、测试、打包、部署、运行的过程。
1.2.1.1 传统项目构建过程
原来构建项目的过程如下:以web项目为例
1)在idea中创建一个java web工程
2)在工程中编写源代码及配置文件等
3)对源代码进行编译, java源文件编译成.class文件
4)执行Junit单元测试
5)将工程打成war包部署至tomcat运行
1.2.1.2 Maven项目构建过程
Maven将项目构建的过程进行标准化,每个阶段使用一个命令完成,下图展示了构建过程的一些阶 段:

清理:删除以前的编译结果,为重新编译做好准备。
编译:将Java源程序编译为字节码文件。
测试:针对项目中的关键点进行测试,确保项目在迭代开发过程中关键点的正确性。
报告:在每一次测试后以标准的格式记录和展示测试结果。
打包:将一个包含诸多文件的工程封装为一个压缩文件用于安装或部署。Java 工程对应 jar 包,Web工程对 应war包。
安装:在Maven环境下特指将打包的结果——jar包或war包安装到本地仓库中。
部署:将打包的结果部署到远程仓库或将war包部署到服务器上运行

1.2.1.3 项目的构建方式
1、Ant
Ant只是一个项目构建工具,它没有集成依赖管理。
Ant在进行项目构建时,它没有对项目目录结构进行约定,需要手动指定源文件、类文件等目录地址。同 时它执行task时,需要显示指定依赖的task,这样会造成大量的代码重复。
2、 Maven
Maven不仅是一个项目构建工具,更是一个项目管理工具。它在项目构建工程中,比ant更全面,更灵 活。 Maven在进行项目构建时,它对项目目录结构拥有约定,知道你的源代码在哪里,类文件应该放 到哪里去。它拥有生命周期的概念, Maven的生命周期是有顺序的,在执行后面的生命周期的任务时, 不需要显示的配置前面任务的生命周期。例如执行 mvninstall 就可以自动执行编译,测试,打包等构建 过程
3、 Gradle
一个开源的自动化构建系统,建立在Apache Ant和Maven Apache概念的基础上,并引入了基于Groovy 的特定领域语言(DSL),而不是使用Apache Maven宣布的项目配置XML形式。
1.2.2 什么是依赖管理
什么是依赖?一个java项目可能要使用一些第三方的jar包才可以运行,那么我们说这个java项目依赖 了这些第三方的jar包。
例如大家前面学过的javaWeb中需要的连接数据库的jar包、数据源的jar包等等。
什么是依赖管理?就是对项目所有依赖的jar包进行规范化管理。
1.2.2.1 传统项目的依赖管理
传统的项目工程要管理所依赖的jar包完全靠人工进行,程序员从网上下载jar包添加到项目工程中。
手动拷贝jar包添加到工程中的问题是:
1、没有对jar包的版本统一管理,容易导致版本冲突。
2、从网上找jar包非常不方便,有些jar找不到。
3、jar包添加到工程中导致工程过大。
1.2.2 .2 Maven项目的依赖管理
Maven项目管理所依赖的jar包不需要手动向工程添加jar包,只需要在pom.xml(Maven工程的配置文 件)添加jar包的坐标,自动从Maven仓库中下载jar包运行。
使用Maven依赖管理添加jar的好处:
1、通过pom.xml文件对jar包的版本进行统一管理,可避免版本冲突。
2、 Maven团队维护了一个非常全的Maven仓库,里边包括了当前使用的jar包, Maven工程可以自动从 Maven仓库下载jar包,非常方便。
1.3 使用Maven的好处
通过上边介绍传统项目和Maven项目在项目构建及依赖管理方面的区别, Maven有如下的好处:
1、一步构建
Maven对项目构建的过程进行标准化,通过一个命令即可完成构建过程。

2、依赖管理
Maven工程不用手动导jar包,通过在pom.xml中定义坐标从Maven仓库自动下载,方便且不易出错。
3、 Maven的跨平台,可在window、 linux上使用。
4、 Maven遵循规范开发,有利于提高大型团队的开发效率,降低项目的维护成本,公司都会考虑使用 Maven来构建项目。
2、 Maven的安装与配置
2.1 Maven的版本是3.3.9版本。
如果大家需要其他版本,自行从网站http://Maven.apache.org/download.cgi 下载。
2.2 Maven的安装
将下载好的Maven安装包解压到一个不含有中文和空格的目录中。
安装包解压即可使用。

目录说明:
bin目录: 该目录包含了 mvn 运行的脚本
boot目录: Maven运行需要类加载器
conf目录: 该目录包含了一个非常重要的文件 settings.xml。直接修改该文件,就能在机器上全局 地定制 Maven 的行为
lib目录: Maven运行依赖jar包

通过 mvn -v命令检查 Maven是否安装成功
在这里插入图片描述

2.4 修改Maven的配置文件
找到Maven安装路径下的settings.xml配置文件
在这里插入图片描述

2.4.1 配置默认的JDK版本
Maven默认使用jdk1.7,我们常用的是jdk1.8,所以直接在配置文件中修改。

<profile>
<id>jdk-1.8</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>

我的setting配置文件如下:

<?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> -->
	<!-- 配置本地仓库 -->
                        <localRepository>D:\.m2</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> -->
		<mirror>
			<id>nexus-aliyun</id>
			<name>Nexus aliyun</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<mirrorOf>*</mirrorOf>
		</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> -->
<profile>
<id>jdk-1.8</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>
	
</profiles>

	<!-- activeProfiles | List of profiles that are active for all builds. | 
		<activeProfiles> <activeProfile>alwaysActiveProfile</activeProfile> <activeProfile>anotherAlwaysActiveProfile</activeProfile> 
		</activeProfiles> -->
</settings>

3.2.1 maven仓库的分类
maven的仓库可以分为本地仓库和远程仓库。
3.2.1.1 本地仓库
本地仓库相当于一个缓存,在电脑上是一个文件夹,我们可以设置这个文件夹的路径(具体怎么设置会 在下面的配置体现) ,工程第一次需要某种jar包时,会从远程仓库(互联网)下载并保存到本地仓库中 (在程序员的电脑上),当第二次使用时,不需要去远程仓库下载,会先去本地仓库中找,如果找不到 才会去远程仓库上下载。
默认情况下,每个用户在自己的用户目录下都有一个路径名为 .m2/respository/ 的仓库目录。

3.2.1.2 远程仓库
远程仓库中分为中央仓库和私服两类。
3.2.1.2.1 中央仓库
中央仓库中的jar包由专业团队(Maven团队)维护,中央仓库中存放了全世界大多数流行的开源软件的
jar包, 是Maven默认的远程仓库。
要浏览中央仓库的内容, Maven 社区提供了一个 URL: http://search.Maven.org/#browse。使用这个 仓库,开发人员可以搜索所有可以获取的代码库。
3.2.1.2.2 私服
私服是另一种特殊的远程仓库,为了节省带宽和时间,应该在局域网内架设一个私有的仓库服务器,用 其代理所有外部的远程仓库。内部的项目还能部署到私服上供其他项目使用。除了中央仓库和私服,还 有很多其他公开的远程仓库,常见的有java.net Maven库(http://download.java.net/maven/2/)和jboss Maven库(http://repository.jboss.com/maven2/)等

3.2.2 配置本地仓库
当你运行 Maven 命令, Maven 将下载依赖的文件到你指定的路径中。
在Maven的安装路径下的conf文件中的setting.xml文件下添加如下内容:
PS:自己先新建一个文件夹作为本地仓库,路径不要有中文和非法字符。

D:.m2

3.2.3 配置远程仓库镜像
Maven默认的远程仓库是Maven团队维护的中央仓库,由于网络原因,去中央仓库下载jar包需要到国外 的网站,不太便捷,速度慢,于是我们可以选择把国内的阿里云的Maven仓库作为中央仓库镜像。
修改 Maven 根目录下的 conf 文件夹中的 settings.xml 文件,在 mirrors 节点上,添加内容如下:

nexus-aliyun Nexus aliyun http://Maven.aliyun.com/nexus/content/groups/public/ *

3.3 Maven的核心文件pom.xml

POM( Project Object Model,项目对象模型 )是 Maven 工程的基本工作单元,是一个XML文件,包含 了项目的基本信息,用于描述项目如何构建,声明项目依赖,等等。
执行任务或目标时, Maven 会在当前目录中查找 POM。它读取 POM,获取所需的配置信息,然后执行 目标。

3.4 Maven的坐标
3.4.1什么是坐标
在平面几何中坐标(x,y)可以标识平面中唯一的一点。在Maven中坐标就是为了定位一个唯一确定的 jar包。
Maven世界拥有大量构件,我们需要找一个用来唯一标识一个构建的统一规范。拥有了统一规范,就可 以把查找工作交给机器.

3.4.2 Maven坐标主要组成
groupId:定义当前Maven项目组织名称
artifactId:定义实际项目名称
version:定义当前项目的当前版本或者是所依赖的jar包的版本

com.xx
demoProject
0.0.1-SNAPSHOT
3.5 Maven的生命周期
3.5.1 什么是生命周期
Maven生命周期就是为了对所有的构建过程进行抽象和统一。包括项目清理、初始化、编译、打包、测 试、部署等几乎所有构建步骤。生命周期可以理解为构建工程的步骤。
3.5.2 Maven三大生命周期
Maven拥有三套相互独立的生命周期,分别是clean、 default和site.
clean Lifecycle: 在进行真正的构建之前进行一些清理工作。 mvn clean
default Lifecycle: 构建的核心部分,编译,测试,打包,部署等等。
site Lifecycle: 生成项目报告,站点,发布站点。
3.5.2.1 clean生命周期
clean的目的是清理项目。
每套生命周期都由一组阶段(Phase)组成,我们平时在命令行输入的命令总会对应于一个特定的阶段。比 如,运行mvn clean ,这个的clean是Clean生命周期的一个阶段。有Clean生命周期,也有clean阶段。

包含3个阶段:
1.pre-clean 执行一些清理前需要完成的工作
2.clean 清理上一次构建过程中生成的文件,比如编译后的class文件等
3.post-clean 执行一些清理后需要完成的工作
3.5.2.2 default生命周期- 构建项目
所有生命周期中最核心的部分,绝大部分工作都发生在这个生命周期中。
咱们只介绍一些比较重要和常用的阶段:
generate-resources: 产生主代码中的资源在classpath中的包
process-resource: 复制并处理资源文件,至目标目录,准备打包。
compile: 编译项目的主源码 .一般来说:编译src/main/java目录下的java文件至项目输出的主 classpath目录中
test-compile:编译项目的测试代码 ,是编译src/test/java目录下的java文件至项目输出的测试 classpath目录中
test:使用单元测试框架运行测试 ,测试代码不会被打包或部署 .
package:打包成可发布的格式
install: 将包安装到Maven本地仓库 ,供本地其他Maven项目使用
deploy:将最终的包复制到远程仓库 ,供其他开发人员和Maven项目使用

PS:运行任何一个阶段的时候,它前面的所有阶段都会被运行,这也就是为什么我们运行mvninstall 的 时候,代码会被编译,测试,打包。此外, Maven的插件机制是完全依赖Maven的生命周期的,因此理 解生命周期至关重要。
参考资料地址:http://Maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
3.5.2.3 site生命周期-生成项目站点
目的:建立和发布项目站点

pre-site 执行一些在生成项目站点之前需要完成的工作 .
site 生成项目站点文档
post-site 执行一些在生成项目站点之后需要完成的工作 .
site-deploy 将生成的项目站点发布到服务器上
4、 Idea搭建Maven项目
4.1 IDEA集成Maven插件
通过入门程序中命令行的方式使用Maven工作效率不高,可以在开发工具中集成Maven软件, idea是 一个开发工具, Maven是一个项目管理工具, Maven有一套项目构建的规范,在idea集成Maven软件, 最终通过idea创建Maven工程。
1、打开IDEA,进入主界面后点击 configure,然后点击 settings
在这里插入图片描述

2、在上面的快捷查找框中输入Maven,查找与Maven相关的设置,然后点击Maven,按如图配置。
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/delete_bug/article/details/120796370