Maven概览
Maven的核心思想,约定由于配置
1 Maven坐标
1.1 本项目的坐标
- groupId: 必须。项目组名称,定义当前Maven项目所隶属的实际项目,通常与域名反向一一对应,与Java包名表示方式类似
- artifactId: 必须。项目名称
- version: 必须。版本
- packaging: (可选,没写则为jar)。打包方式,jar、war等
- classifier (不能直接定义)。帮助定义构建输出的附属构件
1.2 项目的依赖的坐标

- groupId: 必须。依赖的组名称
- artifactId: 必须。依赖的工程名称
- version: 必须。版本
- type: (可选,没写则为jar),该依赖的类型
- scope (可选,没写则为compile),依赖的范围:compile、test、provided、runtime、system、import,用来控制在 编译classpath、测试classpath、运行classpath 三种classpath中依赖是否起作用
1.compile: 默认编译依赖范围。对于编译,测试,运行三种classpath都有效
2.test:测试依赖范围。只对于测试classpath有效
3.provided:已提供依赖范围。对于编译,测试的classpath都有效,但对于运行无效。因为容器已经提供,例如servlet-api
4.runtime:运行时提供。例如:jdbc驱动
- optional (可选,没写则为必选)标记依赖是否可选,一般不用,因为按面向对象设计单一职责原则,一个类应只有一个职责而非糅合太多功能。
- exclusions (可选),用来排除依赖的传递性。只需要指定 groupId 和 artifactId
当Maven通过这些坐标无法从中心仓库获取该组件时,可以通过下面的方法处理:
- 用安装插件安装本地依赖,命令:
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
- 创建你自己的仓库,并部署它
- 设置依赖scope到system,并定义一个systemPath,但这个不推荐
2 Maven命令
- mvn archetype:generate 创建maven项目,只运行该命令会以交互模式运行,让写groupId、artifactId、version等。可以非交互模式运行:
- 创建普通工程:
mvn archetype:generate -DgroupId=com.trinea.maven.test -DartifactId=maven-quickstart -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- 创建Web工程:
mvn archetype:generate -DgroupId=com.trinea.maven.web.test -DartifactId=maven-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
- 创建普通工程:
- mvn clean 删除target文件夹(清除编译出的类)
- mvn compile 编译类到target文件夹下
- mvn test 运行单元测试
- mvn package 打包(生成的包在本工程内)
- mvn install 安装(将包安装到本地maven仓库中,供其他项目使用)
执行test 会自动先调用compile
执行package 会自动先调用test
执行install 会自动先调用package
- mvn dependency:list 列出已解析依赖(Resolved Dependency)
- mvn dependency:tree 列出依赖树
- mvn dependency:analyze 依赖分析(分析编译主代码和测试代码需要的依赖,不会分析执行测试和运行时所需的依赖),会列出 未声明但使用了的依赖(Unused declared dependencies)、已声明但未使用的依赖(Used undeclared dependencies)
3 Maven核心概念
3.1 Maven的核心概念有 坐标、依赖、仓库、生命周期、插件等。
生命周期和插件关联密切,生命周期只是进行行为上的抽象定义,其实际执行由相应的插件来完成。生命周期与插件的绑定实际上是生命周期阶段与插件目标的绑定。
- 生命周期有三种:clean、default、site,三者间相互独立,每个周期包含若干个阶段,阶段前后有依赖关系。如clean周期的pre-clean、-clean、post-clean三个阶段。
- default周期各阶段:validate, initialize, 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, pre-integration-test, integration-test, post-integration-test, verify, install, deploy
clean周期各阶段:pre-clean, clean, post-clean
site周期各阶段:pre-site, site, post-site, site-deploy
- 每个插件都集成有若干同类的功能,一个功能就是该插件的一种目标 。如dependency的目标有analyze、tree等
三个生命周期主要阶段及其插件目标的默认绑定关系如下(default生命周期的绑定关系与打包类型有关,这里以jar为例):


插件及其目标介绍详见:https://maven.apache.org/plugins/
4 聚合与继承
聚合主要为了快速构建项目,继承主要为了消除重复。
聚合就是在父项目的pom中指定modules,继承就是在子项目pom中指定parent
聚合时列出的模块不需要考虑顺序,Maven将自己根据依赖关系排序。
4.1 super pom
所有pom默认都继承自一个super pom,对apache-maven-3.2.5而言,该文件在 yourmaven/lib/maven-model-builder-3.2.5.jar里的pom.xml,内容如下:
<?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.
--> <!-- START SNIPPET: superpom -->
<project>
<modelVersion>4.0.0</modelVersion> <repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories> <build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<!-- NOTE: These plugins will be removed from future versions of the super POM -->
<!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</pluginManagement>
</build> <reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting> <profiles>
<!-- NOTE: The release profile will be removed from future versions of the super POM -->
<profile>
<id>release-profile</id> <activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation> <build>
<plugins>
<plugin>
<inherited>true</inherited>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles> </project>
<!-- END SNIPPET: superpom -->
5 Maven pom.xml结构
<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> <!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties> <!-- Build Settings -->
<build>...</build>
<reporting>...</reporting> <!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors> <!-- Environment Settings -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>
参考文献:http://blog.csdn.net/tomato__/article/details/13168191
- The Basics:http://blog.csdn.net/tomato__/article/details/13169437
- Build Settings:http://blog.csdn.net/tomato__/article/details/13625497
- More Project Information:http://blog.csdn.net/tomato__/article/details/13625811
- Environment Settings:http://blog.csdn.net/tomato__/article/details/13770733
6 其他
maven跳过测试进行编译示例:mvn -DskipTests=true install assembly:single
高版本的maven要求jdk为1.7或later,对低版本的jdk会报错。
版本要求:
maven 3.3要去至少 jdk 1.7
maven 3.2要去至少jdk 1.6
maven 3.1要求至少jdk 1.5
maven 3.0要求至少jdk 1.5
参考资料:
1、《Maven实战》
Maven概览的更多相关文章
- Maven入门指南(一)
Maven介绍: Maven是一个强大的Java项目构建工具. 什么是构建工具? 构建工具是将软件项目构建相关的过程自动化的工具.构建一个软件项目通常包含以下一个或多个过程: 生成源码(如果项目使用自 ...
- Maven详解(转)
原文出自: http://www.cnblogs.com/hongwz/p/5456578.html http://ifeve.com/maven-1/ Maven介绍: Maven是一个强大的Jav ...
- MAVEN学习笔记之基础(1)
MAVEN学习笔记之基础(1) 0.0 maven文件结构 pom.xml src main java package resource test java package resource targ ...
- 001-软件架构概览、maven补充【分包工程、合并包、web容器插件】、git补充
一.整体概述 1.1.共性问题 技术瓶颈.不成体系.不能实际使用.不能落地.无法入门 1.2.目标-软件架构 专注于构建:高可扩展.高性能.大数据量.高并发.分布式的系统架构. 各项技术.组合构建分布 ...
- maven打包springboot项目的插件配置概览
jar包的术语背景: normal jar: 普通的jar,用于项目依赖引入,不能通过java -jar xx.jar执行,一般不包含其它依赖的jar包. fat jar: 也叫做uber jar,是 ...
- maven全局配置文件settings.xml详解
概要 settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. settings.xml文件是干什么的,为什么要配置它 ...
- Maven学习之 插件plugin
Maven本质上是一个执行插件的框架.插件共分两类:build插件和reporting插件. build插件,会在build阶段被执行,应该配置在POM的<build/>元素中. repo ...
- Maven学习之 Settings
虽然天天在用,但是没有系统的学习过,总觉得别扭. 只能用于Java项目. 约定: repository 翻译成 仓库 build 翻译成 构建 build system 翻译成 构建系统 build ...
- Maven实战(七)settings.xml相关配置
一.简介 settings.xml对于maven来说相当于全局性的配置,用于所有的项目,当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们 ...
随机推荐
- this Activity.this Activity.class
1. this 与 Activity.this this是你当前对象的引用,在你的例子中你肯定在内部类ClickEvent里面实现intent,他指向的是ClickEvent,而不是你要传入的Acti ...
- PHP 解析 ElasticSearch 的 json 方法,有關遍歷所有 json 元素
以下是eleasticsearch返回的json資料:{"took" : 12,"timed_out" : false,"_shards" ...
- 关于使用 lua 脚本抢红包
1 java代码 package com.robert.RedisTest; import redis.clients.jedis.Jedis; public class RedisClient { ...
- Vim快捷键操作命令
Vim是一个超牛的编辑器,命令功能十分强大 .而且这些命令大都可以进行组合 ,比如,9yy命令表示复制9行内容,9表示要复制的行数,同样100dd表示删除100行,当数字和命令合作的时候,就比单纯的命 ...
- bzoj 1257
商最多有sqrt(n)个. #include<iostream> #include<cstdio> #include<cstring> #include<al ...
- win10前面板耳机没声音
首先去装Relteck的驱动,windows64位的下载地址是: http://12244.wpc.azureedge.net/8012244/drivers/rtdrivers/pc/audio/0 ...
- 一次更愚蠢的NOIP模拟赛
都可以从COGS上找到 纵横字谜(krizaljka) 时间限制: 1 Sec 内存限制: 32 MB 题目描述 给出两个单词,找到第一个相同的字母,然后第一个单词横数输出,第二个竖着输出形成十字形 ...
- 【poj3342】 Party at Hali-Bula
http://poj.org/problem?id=3342 (题目链接) 题意 给出一棵树,要求在不存在两个节点相邻的条件下,选出尽可能多的节点,并且判断是否有多种选法. Solution 很水的树 ...
- 禁止apache显示目录索引的常见方法(apache禁止列目录)
禁止Apache显示目录索引,禁止Apache显示目录结构列表,禁止Apache浏览目录,这是网上提问比较多的,其实都是一个意思.下面说下禁止禁止Apache显示目录索引的常见的3种方法. 要实现禁止 ...
- Android成长日记-ViewPager的使用
ViewPager在安卓应用中主要用于作为程序的引导页面,欢迎页面,以及其他的动画效果,下面将给你讲述ViewPager的使用 在Android3.0以上的Api中,提供了ViewPager的接口,所 ...