#POM继承与多模块构建 ##约定(管理)优先于配置
标准的目录结构
*.java
放到src/main/java
*.class
在target/classes
目录下
- 对于web项目,配置及静态文件
src/webapp
- 对于web项目,配置及静态文件
- resources目录存放在
src/resources
- resources目录存放在
- ...
项目描述符,pom.xml ##POM
xml文件,pom.xml
一个pom.xml定义了一个项目,告诉项目类型、构建所需要的配置project <-> artifact
构建概念,我们假定认为一个构建就是一个项目项目类型
- pom、jar、war
坐标
- 由groupId、artifactId、version三个属性唯一确定其坐标
属性,${property}
- 通过在properties中定义属性,可以在其他地方通过${property}直接进行引用
4.3.2.RELEASE UTF-8 1.7 1.6.1.RELEASE 4.1.3.RELEASE 依赖dependencies
javax.servlet javax.servlet-api 3.1.0 provided 构建配置build
Restaurant org.apache.maven.plugins maven-compiler-plugin 1.6 UTF-8 <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <uriEncoding>UTF-8</uriEncoding> <finalName>Restaurant</finalName> <server>tomcat</server> </configuration> </plugin> </plugins>
多项目、继承parent
com.netease.restaurant restarant-parent 1.0.0-SNAPSHOT ../pom.xml 项目总体信息
##问题 大项目分为多个子项目进行管理,会造成如下问题:
- 多个项目配置重复
- 修改繁琐
- 依赖版本不一致
- 配置管理混乱
以上问题如何解决?
##继承 pom叫做项目对象模型,可以借助面向对象的思想,把公共的配置,放到父配置子项目进行继承
- 继承或者覆盖,Override
- 可继承项
- 坐标属性,如groupId
- 依赖配置
- 插件配置
- 一般性信息,如开发者信息
继承的根在哪里?Java中的Object类 ##Super POM
所有的Maven项目的POM都继承Super POM
是Maven的组成部分
超级POM定义了一组被所有项目共享的默认设置
- 默认的文件位置(约定的位置)
- 统一的插件配置
- 默认的中央仓库配置
- ...
显示继承Super POM的pom内容命令:
由于内容过长自行查看
mvn help:effective-pom
- ...
继承配置
- 使用parent元素进行配置
##多模块构建
- parent项目,类型pom
- 定义多个modules
- 子模块中配置parent,进行继承
- 统一配置提到parent项目中
- 既使用继承又使用组合
#依赖管理、仓库及构建生命周期 ##依赖配置
依赖坐标 groupId、artifactId、version
scope配置
- compile、provided
- compile:如果不进行配置则compile为默认值,在编译使用,在打包时打进包内
- provided:需要进行配置,在编译使用,在打包时不打进包内
用于继承的pom配置
- dependencyManagement
... javax.servlet javax.servlet-api 3.1.0 provided - dependencyManagement
##仓库
- 用于统一存储所有Maven项目,共享的构建空间
- 通过坐标去仓库中查询
- 仓库分类
- 本地仓库
Maven本地仓库默认路径为${user.home}/.m2/repository
- 本地仓库
- 远程仓库 ##远程仓库
- 本地仓库优先查找
- 提供Maven项目访问并下载构建至本地仓库
/<groupId>/<artifactId>/<version>/<artifactId>-<version>.<packagin>
- 分类
- 中央仓库
The Central Repository
国内速度慢
- 中央仓库
- 其他公共仓库(镜像仓库)
- 私服 - 常见的artifactory
- 配置(远程仓库)
- 全局:使用配置文件settings.xml
- 某项目:在pom.xml为依赖制定repositories
##构建生命周期
- 一个构建生命周期是一组精心组织的有序的阶段
- 每一个阶段执行预先定义的
动作
- 编译
- 构建
- 运行
- ...
- 这些
动作
会根据项目类型进行选择
###三套独立的生命周期
一些命令
- mvn clean
- mvn clean install - 先执行clean在执行install
- mvn site - 生成文档
- mvn package - 打包
##默认生命周期 mvn default 如果想知道mvn default的所有生命周期过程,则需要查询文档 Introduction to the Build Lifecycle
- validate 验证-验证项目是正确的,所有必要的信息是可用的
- process-resources 复制-将资源文件复制到目录下,为了打包使用
- compile 编译-编译项目的源代码
- test 测试-使用合适的单元测试框架测试编译后的源代码。这些测试不应该要求被打包或部署的代码
- package 打包-包以编译的代码在其分配的格式封装,如JAR。
- verify 验证-对集成测试的结果进行检查,以确保质量标准得到满足
- install 安装-将软件包安装到本地存储库中,作为本地其他项目的依赖项
- deploy 部署-在生成环境中进行部署,将最终包复制到远程存储库,用于与其他开发人员和项目共享。
##插件和目标
- 插件(Plugin)式架构
- 所有的插件本身也是一个Maven构建,由Maven仓库管理
- 每一个插件提供多个目标(Goal)
- 调用目标的格式
mvn <Plugin>:<Goal>
,举例mvn tomcat7:run
##插件目标绑定构建生命周期
- 生命周期阶段与目标绑定 当你调用mvn 命令时,实际上调用的是mvn对应的目标
- 用户通过制定生命周期阶段就能够隐式的通过插件执行任务
当我们调用mvn compile
时,实际上调用的是mvn comppiler:compile
目标
对于package阶段,会根据项目类型不同,绑定到不同的目标 ##插件配置
插件配置元素
configuration配置 - 不同插件的配置项查询,查询模板如下
mvn help:describe -Dplugin=
-Dgoal= -Ddetail 对于继承的父pom
- pluginManagement
查询实例:
mvn help:describe -Dplugin=tomcat7 -Dgoal=run -Ddetail
插件的例子:
<build>
<finalName>Restaurant</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
备注:mvn help插件查看详细内容
#插件及总结演示 ##mvn compile 插件compiler的目标compile的配置项:
mvn help:describe -Dplugin=compiler -Dgoal=compile -Ddetail=true
##Plugins
- mvn内建插件
- help
mvn help:help -Ddetail=true
- tomcat7
- exec - 我们简单跑jar包
- 更多插件内容
- 自定义插件
##pom.xml描述内容
##使用Maven的大致过程 我们使用pom.xml,来描述项目构建。 远端有依赖仓库。 首先要根据pom.xml解析项目对象模型,去依赖仓库查找对应依赖。项目依赖管理从项目对象模型pom.xml配置。构建生命周期,不同的构建阶段。不同的构建阶段,实际上是由对应的构建插件所完成的。一个构建阶段可以绑定到一个或者多个目标。一个目标,可以认为是构建逻辑。
##mvn的全局配置settings mvn的配置文件是${M2_HOME}/conf/settings.xml
#总结
- 默认标准的目录结构
- pom继承、Super POM、多项目管理
- 依赖及依赖配置
- 仓库的概念
- 构建生命周期
- 插件及目标
#附录 ##教学项目的目录树结构
.
├── Kitchen
│ ├── Kitchen.iml
│ ├── pom.xml
│ ├── src
│ │ ├── main
│ │ │ ├── java
│ │ │ │ └── com
│ │ │ │ └── netease
│ │ │ │ ├── App.java
│ │ │ │ └── Kitchen.java
│ │ │ └── resources
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── netease
│ │ └── AppTest.java
│ └── target
│ ├── classes
│ │ └── com
│ │ └── netease
│ │ ├── App.class
│ │ └── Kitchen.class
│ ├── generated-sources
│ │ └── annotations
│ ├── generated-test-sources
│ │ └── test-annotations
│ ├── Kitchen-1.0.0-SNAPSHOT.jar
│ ├── maven-archiver
│ │ └── pom.properties
│ ├── surefire
│ ├── surefire-reports
│ │ ├── com.netease.AppTest.txt
│ │ └── TEST-com.netease.AppTest.xml
│ └── test-classes
│ └── com
│ └── netease
│ └── AppTest.class
├── pom.xml
├── pom.xml~
├── restarant-parent.iml
└── Restaurant
├── pom.xml
├── Restaurant.iml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── netease
│ │ │ ├── HelloServlet.java
│ │ │ └── NoodlesServlet.java
│ │ ├── resources
│ │ └── webapp
│ │ ├── index.jsp
│ │ ├── SoybeanMilk.html
│ │ └── WEB-INF
│ │ ├── web.xml
│ │ └── web.xml~
│ └── test
│ └── java
└── target
├── apache-tomcat-maven-plugin
├── classes
│ └── com
│ └── netease
│ ├── HelloServlet.class
│ └── NoodlesServlet.class
├── generated-sources
│ └── annotations
├── maven-archiver
│ └── pom.properties
├── maven-status
│ └── maven-compiler-plugin
│ └── compile
│ └── default-compile
│ ├── createdFiles.lst
│ └── inputFiles.lst
├── Restaurant
│ ├── index.jsp
│ ├── META-INF
│ │ └── MANIFEST.MF
│ ├── SoybeanMilk.html
│ └── WEB-INF
│ ├── classes
│ │ └── com
│ │ └── netease
│ │ ├── HelloServlet.class
│ │ └── NoodlesServlet.class
│ ├── lib
│ │ ├── javax.servlet-api-3.1.0.jar
│ │ └── Kitchen-1.0.0-SNAPSHOT.jar
│ └── web.xml
├── Restaurant.war
├── surefire
├── test-classes
└── tomcat
├── conf
│ ├── logging.properties
│ ├── tomcat-users.xml
│ └── web.xml
├── logs
│ ├── access_log.2016-08-24
│ └── access_log.2016-08-25
├── webapps
└── work
└── Tomcat
└── localhost
└── Restaurant
└── org
└── apache
└── jsp
├── index_jsp.class
└── index_jsp.java
##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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.netease.restaurant</groupId>
<artifactId>Restaurant</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>Restaurant Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 添加总管理依赖 -->
<parent>
<groupId>com.netease.restaurant</groupId>
<artifactId>restarant-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<!-- 依赖Kitchen -->
<dependency>
<groupId>com.netease.restaurant</groupId>
<artifactId>Kitchen</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>Restaurant</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<uriEncoding>UTF-8</uriEncoding>
<finalName>Restaurant</finalName>
<server>tomcat</server>
</configuration>
</plugin>
</plugins>
</build>
</project>
##mvn compiler:compile的配置项查询
zhanpeng@GE70:~$ mvn help:describe -Dplugin=compiler -Dgoal=compile -Ddetail=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:describe (default-cli) @ standalone-pom ---
[INFO] Mojo: 'compiler:compile'
compiler:compile
Description: Compiles application sources
Implementation: org.apache.maven.plugin.compiler.CompilerMojo
Language: java
Bound to phase: compile
Available parameters:
annotationProcessorPaths
Classpath elements to supply as annotation processor path. If specified,
the compiler will detect annotation processors only in those classpath
elements. If omitted, the default classpath is used to detect annotation
processors. The detection itself depends on the configuration of
annotationProcessors.
Each classpath element is specified using their Maven coordinates
(groupId, artifactId, version, classifier, type). Transitive dependencies
are added automatically. Example:
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.sample</groupId>
<artifactId>sample-annotation-processor</artifactId>
<version>1.2.3</version>
</path>
<!-- ... more ... -->
</annotationProcessorPaths>
</configuration>
annotationProcessors
Names of annotation processors to run. Only applies to JDK 1.6+ If not
set, the default annotation processors discovery process applies.
compilerArgs
Sets the arguments to be passed to the compiler if fork is set to true.
Example:
<compilerArgs>
<arg>-Xmaxerrs=1000</arg>
<arg>-Xlint</arg>
<arg>-J-Duser.language=en_us</arg>
</compilerArgs>
compilerArgument
Sets the unformatted single argument string to be passed to the compiler
if fork is set to true. To pass multiple arguments such as -Xmaxerrs 1000
(which are actually two arguments) you have to use compilerArguments.
This is because the list of valid arguments passed to a Java compiler
varies based on the compiler version.
compilerArguments
Sets the arguments to be passed to the compiler (prepending a dash) if
fork is set to true.
This is because the list of valid arguments passed to a Java compiler
varies based on the compiler version.
To pass -Xmaxerrs 1000 -Xlint -Xlint:-path -Averbose=true you should
include the following:
<compilerArguments>
<Xmaxerrs>1000</Xmaxerrs>
<Xlint/>
<Xlint:-path/>
<Averbose>true</Averbose>
</compilerArguments>
Deprecated. use {@link #compilerArgs} instead.
compilerId (Default: javac)
User property: maven.compiler.compilerId
The compiler id of the compiler to use. See this guide for more
information.
compilerReuseStrategy (Default: ${reuseCreated})
User property: maven.compiler.compilerReuseStrategy
Strategy to re use javacc class created:
- reuseCreated (default): will reuse already created but in case of
multi-threaded builds, each thread will have its own instance
- reuseSame: the same Javacc class will be used for each compilation even
for multi-threaded build
- alwaysNew: a new Javacc class will be created for each compilation
Note this parameter value depends on the os/jdk you are using, but the
default value should work on most of env.
compilerVersion
User property: maven.compiler.compilerVersion
Version of the compiler to use, ex. '1.3', '1.5', if fork is set to true.
debug (Default: true)
User property: maven.compiler.debug
Set to true to include debugging information in the compiled class files.
debuglevel
User property: maven.compiler.debuglevel
Keyword list to be appended to the -g command-line switch. Legal values
are none or a comma-separated list of the following keywords: lines,
vars, and source. If debug level is not specified, by default, nothing
will be appended to -g. If debug is not turned on, this attribute will be
ignored.
encoding (Default: ${project.build.sourceEncoding})
User property: encoding
The -encoding argument for the Java compiler.
excludes
A list of exclusion filters for the compiler.
executable
User property: maven.compiler.executable
Sets the executable of the compiler to use when fork is true.
failOnError (Default: true)
User property: maven.compiler.failOnError
Indicates whether the build will continue even if there are compilation
errors.
fileExtensions
file extensions to check timestamp for incremental build default contains
only .class
forceJavacCompilerUse (Default: false)
User property: maven.compiler.forceJavacCompilerUse
compiler can now use javax.tools if available in your current jdk, you
can disable this feature using
-Dmaven.compiler.forceJavacCompilerUse=true or in the plugin
configuration
fork (Default: false)
User property: maven.compiler.fork
Allows running the compiler in a separate process. If false it uses the
built in compiler, while if true it will use an executable.
generatedSourcesDirectory (Default:
${project.build.directory}/generated-sources/annotations)
Specify where to place generated source files created by annotation
processing. Only applies to JDK 1.6+
includes
A list of inclusion filters for the compiler.
maxmem
User property: maven.compiler.maxmem
Sets the maximum size, in megabytes, of the memory allocation pool, ex.
'128', '128m' if fork is set to true.
meminitial
User property: maven.compiler.meminitial
Initial size, in megabytes, of the memory allocation pool, ex. '64',
'64m' if fork is set to true.
optimize (Default: false)
User property: maven.compiler.optimize
Set to true to optimize the compiled code using the compiler's
optimization methods.
outputFileName
Sets the name of the output file when compiling a set of sources to a
single file.
expression='${project.build.finalName}'
proc
Sets whether annotation processing is performed or not. Only applies to
JDK 1.6+ If not set, both compilation and annotation processing are
performed at the same time.
Allowed values are:
- none - no annotation processing is performed.
- only - only annotation processing is done, no compilation.
showDeprecation (Default: false)
User property: maven.compiler.showDeprecation
Sets whether to show source locations where deprecated APIs are used.
showWarnings (Default: false)
User property: maven.compiler.showWarnings
Set to true to show compilation warnings.
skipMain
User property: maven.main.skip
Set this to 'true' to bypass compilation of main sources. Its use is NOT
RECOMMENDED, but quite convenient on occasion.
skipMultiThreadWarning (Default: false)
User property: maven.compiler.skipMultiThreadWarning
(no description available)
source (Default: 1.5)
User property: maven.compiler.source
The -source argument for the Java compiler.
staleMillis (Default: 0)
User property: lastModGranularityMs
Sets the granularity in milliseconds of the last modification date for
testing whether a source needs recompilation.
target (Default: 1.5)
User property: maven.compiler.target
The -target argument for the Java compiler.
useIncrementalCompilation (Default: true)
User property: maven.compiler.useIncrementalCompilation
to enable/disable incrementation compilation feature
verbose (Default: false)
User property: maven.compiler.verbose
Set to true to show messages about what the compiler is doing.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.768s
[INFO] Finished at: Fri Aug 26 22:25:55 CST 2016
[INFO] Final Memory: 9M/180M
[INFO] ------------------------------------------------------------------------
##mvn help:help -Ddetail=true
zhanpeng@GE70:~$ mvn help:help -Ddetail=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:help (default-cli) @ standalone-pom ---
[INFO] Maven Help Plugin 2.2
The Maven Help plugin provides goals aimed at helping to make sense out of the
build environment. It includes the ability to view the effective POM and
settings files, after inheritance and active profiles have been applied, as
well as a describe a particular plugin goal to give usage information.
This plugin has 9 goals:
help:active-profiles
Displays a list of the profiles which are currently active for this build.
Available parameters:
output
Optional parameter to write the output of this help in a given file,
instead of writing to the console.
Note: Could be a relative path.
help:all-profiles
Displays a list of available profiles under the current project.
Note: it will list all profiles for a project. If a profile comes up with a
status inactive then there might be a need to set profile activation
switches/property.
Available parameters:
output
Optional parameter to write the output of this help in a given file,
instead of writing to the console.
Note: Could be a relative path.
help:describe
Displays a list of the attributes for a Maven Plugin and/or goals (aka Mojo -
Maven plain Old Java Object).
Available parameters:
artifactId
The Maven Plugin artifactId to describe.
Note: Should be used with groupId parameter.
cmd
A Maven command like a single goal or a single phase following the Maven
command line:
mvn [options] [<goal(s)>] [<phase(s)>]
detail
This flag specifies that a detailed (verbose) list of goal (Mojo)
information should be given.
goal
The goal name of a Mojo to describe within the specified Maven Plugin. If
this parameter is specified, only the corresponding goal (Mojo) will be
described, rather than the whole Plugin.
groupId
The Maven Plugin groupId to describe.
Note: Should be used with artifactId parameter.
medium
This flag specifies that a medium list of goal (Mojo) information should
be given.
minimal
This flag specifies that a minimal list of goal (Mojo) information should
be given.
output
Optional parameter to write the output of this help in a given file,
instead of writing to the console.
Note: Could be a relative path.
plugin
The Maven Plugin to describe. This must be specified in one of three ways:
1. plugin-prefix, i.e. 'help'
2. groupId:artifactId, i.e. 'org.apache.maven.plugins:maven-help-plugin'
3. groupId:artifactId:version, i.e.
'org.apache.maven.plugins:maven-help-plugin:2.0'
version
The Maven Plugin version to describe.
Note: Should be used with groupId/artifactId parameters.
help:effective-pom
Displays the effective POM as an XML for this build, with the active profiles
factored in.
Available parameters:
output
Optional parameter to write the output of this help in a given file,
instead of writing to the console.
Note: Could be a relative path.
help:effective-settings
Displays the calculated settings as XML for this project, given any profile
enhancement and the inheritance of the global settings into the user-level
settings.
Available parameters:
output
Optional parameter to write the output of this help in a given file,
instead of writing to the console.
Note: Could be a relative path.
showPasswords
For security reasons, all passwords are hidden by default. Set this to
true to show all passwords.
help:evaluate
Evaluates Maven expressions given by the user in an interactive mode.
Available parameters:
artifact
An artifact for evaluating Maven expressions.
Note: Should respect the Maven format, i.e.
groupId:artifactId[:version][:classifier].
expression
An expression to evaluate instead of prompting. Note that this must not
include the surrounding ${...}.
help:expressions
Displays the supported Plugin expressions used by Maven.
Available parameters:
output
Optional parameter to write the output of this help in a given file,
instead of writing to the console.
Note: Could be a relative path.
help:help
Display help information on maven-help-plugin.
Call mvn help:help -Ddetail=true -Dgoal=<goal-name> to display parameter
details.
Available parameters:
detail
If true, display all settable properties for each goal.
goal
The name of the goal for which to show help. If unspecified, all goals
will be displayed.
indentSize
The number of spaces per indentation level, should be positive.
lineLength
The maximum length of a display line, should be positive.
help:system
Displays a list of the platform details like system properties and environment
variables.
Available parameters:
output
Optional parameter to write the output of this help in a given file,
instead of writing to the console.
Note: Could be a relative path.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.853s
[INFO] Finished at: Fri Aug 26 22:33:07 CST 2016
[INFO] Final Memory: 8M/180M
[INFO] ------------------------------------------------------------------------