Apache Maven基础
1.http://maven.apache.org/
1.下载apache-maven-3.1.1-bin.zip,解压至C盘根目录
2.添加环境变量至path:C:\apache-maven-3.1.1\bin
3.cmd命令下输入:mvn -version/mvn-v
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 23:22:2
2+0800)
Maven home: C:\apache-maven-3.1.1\bin\..
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_51\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows xp", version: "5.1", arch: "x86", family: "windows"
2.mvn -help
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a system property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml).
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
-ff,--fail-fast Stop at first failure in
reactorized builds
-fn,--fail-never NEVER fail the build, regardless
of project result
-gs,--global-settings <arg> Alternate path for the global
settings file
-h,--help Display help information
-l,--log-file <arg> Log file to where all build output
will go.
-llr,--legacy-local-repository Use Maven 2 Legacy Local
Repository behaviour, ie no use of
_remote.repositories. Can also be
activated by using
-Dmaven.legacyLocalRepo=true
-N,--non-recursive Do not recurse into sub-projects
-npr,--no-plugin-registry Ineffective, only kept for
backward compatibility
-npu,--no-plugin-updates Ineffective, only kept for
backward compatibility
-nsu,--no-snapshot-updates Suppress SNAPSHOT updates
-o,--offline Work offline
-P,--activate-profiles <arg> Comma-delimited list of profiles
to activate
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path.
-q,--quiet Quiet output - only show errors
-rf,--resume-from <arg> Resume reactor from specified
project
-s,--settings <arg> Alternate path for the user
settings file
-T,--threads <arg> Thread count, for instance 2.0C
where C is core multiplied
-t,--toolchains <arg> Alternate path for the user
toolchains file
-U,--update-snapshots Forces a check for updated
releases and snapshots on remote
repositories
-up,--update-plugins Ineffective, only kept for
backward compatibility
-V,--show-version Display version information
WITHOUT stopping build
-v,--version Display version information
-X,--debug Produce execution debug output
3.maven项目结构
${basedir} 存放 pom.xml(Project Object Model)和所有的子目录
${basedir}/src/main/java 项目的 java源代码
${basedir}/src/main/resources 项目的资源,比如说 property文件
${basedir}/src/test/java 项目的测试类,比如说 JUnit代码
${basedir}/src/test/resources 测试使用的资源
编译后 的 classes 会放在 ${basedir}/target/classes 下面, JAR会放在 ${basedir}/target
-->惯例优于配置
4.Maven仓库
1.仓库分类:本地仓库和远程仓库。Maven根据坐标寻找构件的时候,它先会查看本地仓库,如果本地仓库存在构件,则直接使用;如果没有,则从远程仓库查找,找到后,下载到本地
2.默认情况下,每个用户在自己的用户目录下都有一个路径名为.m2/repository/的仓库目录。可以自定义本地仓库的地址.修改路径时,先将$M2_HOME/conf/settings.xml文件复制到~/.m2/settings.xml,再对后者进行编辑,设置localRepository元素的值为想要的仓库地址
3.远程中央仓库的地址为 http://repo1.maven.org/
4.私服/镜像
5.settings.xml
5.POM
1.Maven坐标
1.groupId:组织标识.如com.landon,在m2_repository目录下:com/landon
2.artifactId:项目名称,如mavs,在m2_repository目录下:com/landon/mavs
3.version:版本号,如1.0.0,在m2_repository目录下:com/landon/mavs/1.0.0
4.packaging:打包的格式,如jar,war
2.依赖关系dependencies(项目中依赖的jar)
dependency属性
1.groupId/artifactId/version 依赖的具体工程
2.scope 依赖范围 compile/test/runtime/provided/system
3.optional 去除依赖传递
4.exclusions 排除依赖
3.聚合关系
1.通过一个父模块将所有的要构建模块整合起来,将父模块的打包类型声明为 POM,通过 <modules> 将各模块集中到父 POM.<module></module> 中间的内容为子模块工程名的相对路径.
2.Maven 会首先解析聚合模块的 POM 文件,分析要构建的模块,并通过各模块的依赖关系计算出模块的执行顺序,根据这个潜在的关系依次构建模块。将各子模块聚合到父模块中后,我们就可以对父模块进行一次构建命令来完成全部模块的构建
4.继承关系
1.通过构建父模块将子模块共用的依赖,插件等进行统一声明
2.parent
5.插件
1.Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven-compiler-plugin完成的
2.每个任务对应了一个插件目标(goal),每个插件会有一个或者多个目标,例如maven-compiler-plugin的compile目标用来编译位于src/main/java/目录下的主源码,testCompile目标用来编译位于src/test/java/目录下的测试源码
3.maven-antrun-plugin能让用户在Maven项目中运行Ant任务
4.plugins/plugin
6.构建
1.build
2.结合plugins/resources
3.profile(多环境构建)
6.maven属性(和ant差不多)
1.内置属性。这种属性跟 Maven Project 自身有关,比如要引入当前 Project 的版本信 息,那么只需要在使用的位置引用 ${version} 就行了。
2.Setting 属性。上文中已经提到 Maven 自身有一个 settings.xml 配置文件,它里面含有包括仓库,代理服务器等一些配置信息,利用 ${settings.somename} 就可以得到文件里相应元素的值。
3.POM
属性。这种属性对应 POM 文件中对应元素的值,例如 ${project.groupId} 对应了
<groupId></groupId> 中的值,${project.artifactId} 对应了
<artifactId> </ artifactId > 中的值。
4.系统环境变量。可以使用 env.${name} 来获得相应 name 对应的环境变量的值,例如 ${env.JAVA_HOME} 得到的就是 JAVA_HOME 的环境变量值。
5.用户自定义变量。这种类型的变量是使用最频繁和广泛的变量,完全由用户自己定义。在 POM 文件中加入 <properties> 元素并将自定义属性作为其子元素
7.查找依赖jar
http://search.maven.org/
你懂得
8.参考:
1.http://maven.apache.org/settings.html
2.http://maven.apache.org/pom.html
3.http://maven.apache.org/guides/index.html