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 (可选),用来排除依赖的传递性。只需要指定 groupIdartifactId

 当Maven通过这些坐标无法从中心仓库获取该组件时,可以通过下面的方法处理:

  1. 用安装插件安装本地依赖,命令:

    mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
  2. 创建你自己的仓库,并部署它
  3. 设置依赖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三个阶段。
    1. 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
    2. clean周期各阶段:pre-clean, clean, post-clean

    3. 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

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概览的更多相关文章

  1. Maven入门指南(一)

    Maven介绍: Maven是一个强大的Java项目构建工具. 什么是构建工具? 构建工具是将软件项目构建相关的过程自动化的工具.构建一个软件项目通常包含以下一个或多个过程: 生成源码(如果项目使用自 ...

  2. Maven详解(转)

    原文出自: http://www.cnblogs.com/hongwz/p/5456578.html http://ifeve.com/maven-1/ Maven介绍: Maven是一个强大的Jav ...

  3. MAVEN学习笔记之基础(1)

    MAVEN学习笔记之基础(1) 0.0 maven文件结构 pom.xml src main java package resource test java package resource targ ...

  4. 001-软件架构概览、maven补充【分包工程、合并包、web容器插件】、git补充

    一.整体概述 1.1.共性问题 技术瓶颈.不成体系.不能实际使用.不能落地.无法入门 1.2.目标-软件架构 专注于构建:高可扩展.高性能.大数据量.高并发.分布式的系统架构. 各项技术.组合构建分布 ...

  5. maven打包springboot项目的插件配置概览

    jar包的术语背景: normal jar: 普通的jar,用于项目依赖引入,不能通过java -jar xx.jar执行,一般不包含其它依赖的jar包. fat jar: 也叫做uber jar,是 ...

  6. maven全局配置文件settings.xml详解

    概要 settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. settings.xml文件是干什么的,为什么要配置它 ...

  7. Maven学习之 插件plugin

    Maven本质上是一个执行插件的框架.插件共分两类:build插件和reporting插件. build插件,会在build阶段被执行,应该配置在POM的<build/>元素中. repo ...

  8. Maven学习之 Settings

    虽然天天在用,但是没有系统的学习过,总觉得别扭. 只能用于Java项目. 约定: repository  翻译成 仓库 build 翻译成 构建 build system 翻译成 构建系统 build ...

  9. Maven实战(七)settings.xml相关配置

    一.简介 settings.xml对于maven来说相当于全局性的配置,用于所有的项目,当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们 ...

随机推荐

  1. 关于QString中的arg()函数使用方法

    例:正确做法:ui->label->setText(QString("Processingfile%1").arg(index));错误做法: ui->label ...

  2. applicationContext.xml和dispatcher-servlet.xml的区别

    在SpringMVC项目中我们一般会引入applicationContext.xml和dispatcher-servlet.xml两个配置文件,这两个配置文件具体的区别是什么呢? Spring 官方文 ...

  3. 【CodeVS 1199】【NOIP 2012】开车旅行

    http://codevs.cn/problem/1199/ 主要思想是倍增,对于第一个回答从后往前扫,依次插入平衡树中. 我写的splay,比较繁琐. #include<cmath> # ...

  4. sed巧用,随手记

    删除文件后几行,或者前几行 方法1 都当做最后一行,来删除  for((i=1;i<4;i++)); do sed -i '$d' seq01.txt ; done  方法二 倒过来看 cat ...

  5. Thinking in java学习笔记之初始化

    1.基本数据类型:类的每个基本数据类型保证有一个初值(char为0输出则是空白) 2.构造器: 3.静态初始化顺序示例及总结 4.非静态初始化顺序 4.数组

  6. Python 从零学起(纯基础) 笔记(一)

    作者身份:初学Python,菜鸟 ================================================= 1. 主提示符和次提示符  >>> 主提示符   ...

  7. Jenkins邮件配置,实现邮件发送策略(可实现每个Job对应不同的发送邮箱)

    前言: 首先,要有一个用来发送的邮箱,首选网易!参考:http://www.cnblogs.com/EasonJim/p/6051636.html,这里我注册了网易的免费企业邮箱. 并且我新建没多个邮 ...

  8. 微信公众平台教程和SDK收集

    教程收集: 1.Senparc(.net) http://www.cnblogs.com/szw/archive/2013/05/14/weixin-course-index.html 2.方倍工作室 ...

  9. Oracle数据库查看当前数据库版本的方法

    常用的有三种方法:   方法一:v$version SQL> select * from v$version; BANNER ---------------------------------- ...

  10. Android数据存储-通过SharedPreferences实现记住密码的操作

    在Android中登陆中,为了实现用户的方便,往往需要根据用户的需要进行记住密码的操作,所以,在Android数据存储中SharedPreferences恰恰可以实现这一点 下面,小编将带领大家通过S ...