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. you-get中文说明

    来源于:https://github.com/soimort/you-get/wiki/%E4%B8%AD%E6%96%87%E8%AF%B4%E6%98%8E You-Get 乃一小小哒命令行程序, ...

  2. 重新打开singleTask画面时传值问题

    记录学习之用 大家都知道假如当我们的A画面设置了android:launchMode="singleTask"时,从A画面跳到B画面之前没有finishA画面,然后在B画面使用st ...

  3. 【BZOJ 4517】【SDOI 2016 Round1 Day2 T2】排列计数

    本蒟蒻第一次没看题解A的题竟然是省选$Round1$ $Day2$ $T2$ 这道组合数学题. 考试时一开始以为是莫队,后来想到自己不会组合数的一些公式,便弃疗了去做第三题,,, 做完第三题后再回来看 ...

  4. 小 div在大 div中左右上下居中

    <!DOCTYPE HTML><html><head> <meta http-equiv="Content-Type" content=& ...

  5. gym923B

    Even though he isn't a student of computer science, Por Costel the pig has started to study Graph Th ...

  6. 表单提交中get和post方式的区别

    表单提交中get和post方式的区别有5点 1.get是从服务器上获取数据,post是向服务器传送数据. 2.get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一 ...

  7. 浏览器上网 (Safari & Chrome)

    浏览器上网 (Safari & Chrome) Command + L = 光标移到地址框 Command + R = 刷新页面 Command + + = 大屏幕的时候很实用,放大页面,基本 ...

  8. golang学习之旅:搭建go语言开发环境

    从今天起,将学习go语言.今天翻了一下许式伟前辈写的<Go语言编程>中的简要介绍:Go语言——互联网时代的C语言.前面的序中介绍了Go语言的很多特性,很强大,迫不及待地想要一探究竟,于是便 ...

  9. 基于SURF特征的目标检测

    转战matlab了.步骤说一下: 目标图obj 含目标的场景图scene 载入图像 分别检测SURF特征点 分别提取SURF描述子,即特征向量 用两个特征相互匹配 利用匹配结果计算两者之间的trans ...

  10. 降低磁盘IO使Oracle性能优化(转)

    文章转自:http://blog.chinaunix.net/uid-26813519-id-3207996.html 硬件方面虽然只占Oracle性能优化的一个方面(另一方面是软件),但是仍不可忽视 ...