Detailed Maven project

maven conceptual model

Maven是Apache下的一个开源项目,它是一个项目管理工具,它用于对java项目进行项目构建、依赖管理及项目信息管理。当前使用Maven的项目在持续增长。
Maven包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Dependency Management System),和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。

Here Insert Picture Description
1.Pom Project Object Model
Create a maven project, each project has a maven pom.xml file, in this file we can configure information
2. dependency management
By defining the coordinates of the project dependency dependent components managed by maven.
For example: The project relies struts2.3.24, by the definition of dependent in pom.xml struts2 can be automatically added to the jar package works:
3.maven project life cycle
Here Insert Picture Description

Maven repository

关于仓库的种类:
1.本地仓库
由我们自己配置的一个仓库,从远程仓库中下载下的jar,都会存储到本地仓库中。
2.远程仓库
 网络上的其它仓库
3.中央仓库:
在maven环境内部内置一个远程仓库地址http://repo1.maven.org/maven2 ,它是中央仓库,服务于整个互联网,它是由Maven自己维护,里面有大量的常用类库,并包含了世界上大部分流行的开源项目构件。它本身也是一个远程仓库,因为它是由maven团队维护

Maven installation configuration

Download URL: http://maven.apache.org/download.cgi
Here Insert Picture Description
This tutorial uses version 3.1.1
Here Insert Picture Description
unzip directory does not contain spaces and Chinese
Here Insert Picture Description

bin目录 mvn.bat (以run方式运行项目)、 mvnDebug.bat(以debug方式运行项目 )
boot目录 maven运行需要类加载器 
conf目录 settings.xml 整个maven工具核心配置文件 
lib目录 maven运行依赖jar包

Environment variable configuration

电脑上 安装JDK1.4 + 版本  (将JAVA_HOME/bin 配置环境变量path )
1.在环境变量中创建一个MAVEN_HOME配置的是D:\apache-maven-3.3.9
2.在环境变量中path配置  %MAVEN_HOME%\bin

Here Insert Picture Description
The% MAVEN_HOME% \ bin added to the environment variable path
Here Insert Picture Description
command to check the configuration is successful by mvn -v
Here Insert Picture Description

Maven repository configuration (local repository)

本地仓库是用来存放联网下载maven的插件和jar包,maven本地仓库有的jar不再从互联网下载,所以本地仓库相当于一个缓存。
本地仓库有一个默认位置:${user.dir}/.m2/repository,${user.dir}表示windows用户目录,如下

Here Insert Picture Description

全局本地仓库:
	我们在maven目录下的conf下的setttings.xml文件中配置的就是一个全局的本地仓库
局部本地仓库:
	可以将settings.xml文件复制到指定的目录下,来进行配置,它就是一个局部的。

Note: How the designated warehouse location in settings.xml file:
Here Insert Picture Description

Eclipse maven integration

The current version of the eclipse is Mars.2 Release (4.5.2), this version comes with maven plugin not installed separately.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Settings.xml file specified
Here Insert Picture Description
Here Insert Picture Description
Note: If you modify the setting.xml Click on the picture "update settings", local warehouse rebuild the index, click on the "Reindex". Specify it, will help us rebuild the index.
When we rebuild the index, the subsequent introduction of dependence in the pom.xml file, it will add a prompt for us.
Browse warehouse in eclipse
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Maven Commands

These commands are used by way of plug-ins.
Compile command compile, in mvn eclipse can be omitted, under cmd to add mvn compile
Here Insert Picture Description
compiled .class files in the target / classes (java This command will only compile the source code, test code will not compile, compile the test class mvn test-compile, compiled .class file in the target \ test-classes)
Here Insert Picture Description
zeroes the command clean, empty the target under the compiled file includes lay package.
Mvn test command, the compiler under test package file to the target directory.
Package command, the next target will be packaged into a project, generated by default jar package name: artifactId-version.jar java project build jar package, web project generates war package
Here Insert Picture Description
Here Insert Picture Description
Install command: jar or war items packaged and released to the warehouse ---- installed warehouse / groupId / artifactId / version directory
test command mvn test method to perform all the test cases, recompile

In the actual development, usually in eclipse maven to execute the command, we can execute the command line.
Mvn run command Run as employed, Debug as the debug run command using mvnDebug (may break point)
Here Insert Picture Description
as: maven clean maven install maven test may be performed directly
Here Insert Picture Description

maven command at the command line

Mvn command line execution, the premise must navigate to the path where the execution pom.xml file need to add mvn, eclipse does not need to be added.

maven project life cycle

Here Insert Picture Description
Maven clean maven compile maven test……

Maven有三套相互独立的生命周期这三套生命周期分别是: 
Clean Lifecycle 在进行真正的构建之前进行一些清理工作。 
Default Lifecycle 构建的核心部分,编译,测试,打包,部署等等。 
Site Lifecycle 生成项目报告,站点,发布站点。

Behind the implementation of the life cycle of the command, in front of performed automatically, such as the above command will execute package automatically.
Here Insert Picture Description
1, the life cycle Clean
Clean each lifecycle stage of the life cycle by a group (Phase) composition, we usually command on a command line always corresponds to a particular stage. For example, run mvn clean, this is a stage of clean Clean lifecycle. There Clean lifecycle, but also clean stage. Clean the life cycle consists of a total of three stages:
pre-clean need to perform some work done before the clean
clean to remove all build files generated on a
post-clean performing some needs to be done immediately after the clean work
mvn clean is clean the above is clean, in a life cycle, run a stage when all stages will be run before it, that is to say, mvn clean equivalent to mvn pre-clean clean, if we run mvn post-clean, then pre-clean, clean will be run. This is a very important rule Maven, can greatly simplify the command-line input.

2, the life cycle of default
Default Default life cycle life cycle Maven life cycle of the most important, most of the work takes place in the life cycle:

validate
generate-sources
process-sources
generate-resources
process-resources     复制并处理资源文件,至目标目录,准备打包。
compile     编译项目的源代码。
process-classes
generate-test-sources 
process-test-sources
generate-test-resources
process-test-resources     复制并处理资源文件,至目标测试目录。
test-compile     编译测试源代码。
process-test-classes
test     使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署。
prepare-package
package     接受编译好的代码,打包成可发布的格式,如 JAR 。
pre-integration-test
integration-test
post-integration-test
verify
install     将包安装至本地仓库,以让其它项目依赖。
deploy     将最终的包复制到远程的仓库,以让其它开发人员与项目共享。

3, life cycle site
Site life cycle of pre-site to perform some need to complete paperwork before generating station
site to build the project document site of
post-site to perform some need to complete paperwork after generating sites, and prepare for the deployment of
site- deploy the generated site documentation deployed to specific servers
frequently used here is the site stage and site-deploy stage, to generate and publish sites Maven, Maven this is pretty powerful, Manager prefer, documents and statistical data automatic generation, very nice.

Detailed profiles Pom.xml

pom.xml是Maven项目的核心配置文件,位于每个工程的根目录,基本配置如下:
<project > :文件的根节点 .
<modelversion > : pom.xml使用的对象模型版本(可以理解为约束的版本) .
<groupId > :项目名称,一般写项目的域名
<artifactId > :模块名称,子项目名或模块名称
<version > :产品的版本号 . 
<packaging > :打包类型,一般有jar、war、pom 等 
<name > :项目的显示名,常用于 Maven 生成的文档。  
<description > :项目描述,常用于 Maven 生成的文档
<dependencies> :项目依赖构件配置,配置项目依赖构件的坐标
<build> :项目构建配置,配置编译、运行插件等。

第一部分: POM Relationships 关系 
	Coordinates 坐标: 在仓库中唯一标识项目位置三个参数 
	<groupId> 项目名称
	<artifactId> 模块名称
	<version> 版本号
		
	Aggregation 聚合(多模块) : 将项目分解为多个不同模块 
	Inheritance 继承 : 项目之间继承,实现POM复用 
	Dependencies 依赖: 项目依赖另一个项目进行编译或者运行 
第二部分: Project Information 项目信息 
	name :项目名称 
	desciption: 项目描述
第三部分: Build settings  构建配置
	properties : 配置属性 
	build :构建项目需要插件配置 
	packaging :打包方式 jar、war、pom (使用继承)
	reporting : 报表
第四部分: Build Environment 构建环境 (在依赖、构建、运行 生效配置 )
	Distribution Management :版本锁定 
	Profile : 灵活自定义配置,在特定情况激活 

Project default encoding
Here Insert Picture Description
on the maven tomcat plugin uses
can manually configure the port: maven tomcat plugin configuration port
Here Insert Picture Description
tomcat7 If you are using, you can press the following programs:

<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<version>2.2</version>			
</plugin>
在eclipse中运行,使用 tomcat7:run就可以

Here Insert Picture Description
If you are using tomcat6, you press the following scheme:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>tomcat-maven-plugin</artifactId>
	<version>1.1</version>
</plugin>
运行:tomcat:run

May be added simultaneously both a tomcat plug (. 7 \. 6)
finalName: Configure the war file name maven_ssh
Here Insert Picture Description
add dependencies
defined in pom.xml dependency tag introduced dependence
Here Insert Picture Description
Here Insert Picture Description
by introducing eclipse:
Here Insert Picture Description
Here Insert Picture Description

Find coordinates

Method One: Use the site search
http://search.maven.org/
http://mvnrepository.com/
Site Search example:
Here Insert Picture Description
Here Insert Picture Description
Method Two: Use the indexing maven plugin
Here Insert Picture Description

Dependence range scope

所谓的依赖范围,是指我们导入的jar包它的生命周期,简单说,就是它在什么情况下可有效
默认:compile
compile:编译范围,指A在编译时依赖B,此范围为默认依赖范围。编译范围的依赖会用在编译、测试、运行,由于运行时需要所以编译范围的依赖会被打包。
provided:provided依赖只有在当JDK或者一个容器已提供该依赖之后才使用, provided依赖在编译和测试时需要,在运行时不需要,比如:servlet api被tomcat容器提供。
runtime:runtime依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如:jdbc的驱动包。由于运行时需要所以runtime范围的依赖会被打包。
test:test范围依赖 在编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用,比如:junit。由于运行时不需要所以test范围依赖不会被打包。
system:system范围依赖与provided类似,但是你必须显式的提供一个对于本地系统中JAR文件的路径,需要指定systemPath磁盘路径,system依赖不推荐使用。

Here Insert Picture Description
Test Summary:
Default introduction of the jar package ------- compile range [default] can not write (compile, test run are valid)
the servlet-API, JSP-API ------- Provided (compiler, test valid, invalid jar to prevent conflict and Tomcat) running
jdbc driver jar package ---- runtime (test effective operation)
JUnit ----- test (test valid)
dependent range from strong to weak is: compile > provided> runtime> test

Transitive dependencies

A dependent B, C dependent B, C automatically introduced after introduction of B in A, A C is dependent of transmission, if dependent C A D is the D-dependent transmission. Why is there such a situation, the reasons dependent jar package corresponding pom file also dependent statement.

Dependent transmission range

Simply put, by relying on the scope of delivery can be resolved if you want to import dependent jar package.
Columns: item A depends on the range B.
Row: Project B depends on the range C.
A result obtained depends on the box, i.e. C range.
Here Insert Picture Description

Reliance version conflict resolved

<!-- struts2-spring-plugin依赖spirng-beans-3.0.5 -->
  	<dependency>
  		<groupId>org.apache.struts</groupId>
  		<artifactId>struts2-spring-plugin</artifactId>
  		<version>2.3.24</version>
  	</dependency>
  	
<!-- spring-context依赖spring-beans-4.2.4 -->
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>4.2.4.RELEASE</version>
  	</dependency>
org.apache.struts依赖spirng-beans-3.0.5,spring-context依赖spring-beans-4.2.4,但是发现spirng-beans-3.0.5加入到工程中,而我们希望spring-beans-4.2.4加入工程。
maven自动按照下边的原则调解依赖标准:
	1.第一声明者优先原则
		在pom文件中谁先声明以谁为准。
	2.路径近者优先原则
		例如:A-> spirng-beans-4.2.4,A->B-> spirng-beans-3.0.5,则spirng-beans-4.2.4优先

Solution:
1, eliminate dependence (development applications less)
Here Insert Picture Description
2, locking version (recommended)
face of numerous dependency, there is a way without considering the dependence path statement optimization factors can be used directly locked version of the method to determine the dependency artifact version, this method is commonly used in enterprise development:
Here Insert Picture Description
Pom view label location rules
Click on the project f2
Here Insert Picture Description

Published 81 original articles · won praise 5 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_36205206/article/details/104007341