1.maven的常用命令
compile 编译
test 测试
package 打包
clean 删除生成的target的目录
install 会将打包好的项目安装到本地仓库中,提供给其他的模块使用
2.maven生成目录骨架
使用archetype插件生成模板
> mvn archetype:generate
按照需求设置groupId,artifactId,version..
也可以直接就是参数指定
> mvn archetype:generate
-DgroupId=组织名,公司的反写
-DartifactId=项目名
-Dversion=版本号
-Dpackage=代码所存在的包名
> mvn archetype:generate
-DarchetypeCatalog=internal
-DgroupId=cn.everlook.myweb
-DartifactId=mywebapp
-DarchetypeArtifactId=maven-archetype-webapp
archetypeArtifactId可以为以下值
archetypeArtifactId(项目骨架的类型)
* maven-archetype-archetype
* maven-archetype-j2ee-simple
* maven-archetype-mojo
* maven-archetype-portlet
* maven-archetype-profiles (currently under development)
* maven-archetype-quickstart
* maven-archetype-simple (currently under development)
* maven-archetype-site
* maven-archetype-site-simple
* maven-archetype-webapp
3.maven的中坐标和仓库
坐标: 唯一标识一个maven构件groupId,artifactId,version唯一确定了构件
仓库:
本地仓库,
中央仓库,依赖的jar包首先在本地仓库中查找如果不存在就去远程仓库中下载,如果远程中央仓库没有,则会报错
所有的pom都继承至maven根目录下的maven-model-builder jar中的pom-4.0.0.xml文件
镜像仓库:提供和中央仓库一样的功能的国内服务器可以在settings.xml中的
<mirrors>标签中配置
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>human readable name for this Mirror</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
更改本地仓库的位置
修改maven根目录下的conf下的settings.xml
<localRepository>/path/to/local/repo</localRepository>
4.maven的生命周期和插件
clean,compiler,deploy,install,resource,source..配置插件
在POM中的<build>标签下
配置一个source插件
source插件可以将源码进行打包
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>表示在package之后运行
<execution><phase>package</phase>
<goals>
<gola>jar-no-fork</gola>
</golas>
</execution>
</executions>
</plugin>
</plugins>
5.pom常用元素介绍
<project> 根目录
<modelVersion>4.0.0</modelVersion> 指定POM的版本,
<groupId>公司域名反写</groupId>
<artifactId>项目名+模块名</artifactId>
<packaging>jar</packaging> //打包类型 jar war zip pom
<version>1.1</version> //版本号
0.0.1snapshot 第一个0表示大版本号,第二个表示分支版本号,第三个表示小版本号
<name></name>项目的描述名
<url></url>项目的地址
<description></description>项目的描述
<developers></developers>开发人员列表
<licenses></licenses>许可证信息
<organization></organization>组织信息
依赖列表
<dependencies>
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version>1.1</version>
<type></type> 包类型
<scope></scope> 作用范围
作用范围的值
compile 默认:编译测试运行都有效
provided 在测试和编译是有效,在最后运行时不会被加入项目中如serlvet-api
runtime 只在项目运行才使用,比如jdbc的驱动
test 只在测试范围有效,比如junit
system 测试和编译有效,和本地系统相关
import 导入的范围,只使用在父模块中dependencyManagement中,表示从其他的pom中导入dependency的配置
<optional></optional> true/false 设置依赖是否可选默认false
排除依赖传递列表
<exclustions>
<exclusion>
<exclusion>
</exclustions>
<dependency>
<dependencies>
依赖管理(不会引入依赖中,主要用于定义在父模块中供子模块继承)
<dependencyManagement>
<dependencies>
<dependency>
</dependency>
</dependencies>
<dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<gola>jar-no-fork</gola>
</golas>
</execution>
</executions>
</plugin>
</plugins>
</build>
在子模块中声明对父模块的继承
在父模块中声明的<dependencyManager>中的依赖
<packaging>标签填写pom
<parent><groupId></groupId>
<artifactId></artifactId>
<version></version>
</parent>
可以将多个模块进行聚合
可以将子模块一起进行编译,打包,安装到仓库中。
<modules>
<module>模块名</module>
</modules>
依赖冲突
短路优先使用2的x
A->B—>C->X 1
A->D->X 2
6.使用maven发布web项目
配置jetty插件
<plugin><groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<executions>
<exection>
在打包成功后使用jetty:run 来运行jetty服务
<phase>package</phase>
<goals>
<goal>run</goal>
<goals>
</execution>
</executions>
</plugin>
使用 mvn jetty:run 启动
使用maven-tomcat插件
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<exection>
<phase>package</phase>
<goals>
<goal>run</goal>
<goals>
</execution>
</executions>
</plugin>