命令:
clean:清除class文件,以及删除target目录,target目录中存放的是字节码文件和测试报告
compile:编译形成字节码文件(target目录)
test:运行测试文件
package:打一个jar包,会在target目录中存在
install:将jar包安装到本地仓库中
完整的项目构建过程包括:
maven的生命周期
clean
:清理项目。
pre-clean
clean
post clean
default
:构建项目(core)
包含: compile test package install
site
:生成项目站点
pre-site
site
post-site
site-deploy
maven基础知识
source插件:可以将项目源码进行打包。
- 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">
<!-- 指定当前pom版本 -->
<modelVersion>4.0.0</modelVersion>
<!--
groupId:反写的公司网址+项目名,
artifactId:项目名+模块名
version:当前项目版本号:
第一个0表示大版本号
第二个0表示分支版本号
第三个0表示小版本号
0.0.1snapshot快照版本
packaging:打包类型,默认为jar,还有war包等
name:项目描述名
url:项目的地址
description:项目描述
developers:开发者
licenser:许可
organization:组织信息
-->
<groupId>com.imooc.hi</groupId>
<artifactId>hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--
此处比较重要:
依赖列表:dependencies
optional:true或false,设置依赖是否可选,默认为false
exclusions:排除依赖列表
依赖管理:dependencyManagement:不会被运行,不会被引入到项目中
构建:build
插件列表:plugins
parent:通常用于子模块中对父模块的一个继承
modules:用来聚合运行多个maven项目,其中可以指定多个模块
-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 构建插件 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<!-- 在执行package命令的时候,会将项目的源码进行打包 -->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 依赖范围scope
项目的classpath:
编译 测试 运行
依赖范围的作用的用来控制依赖和三种classpath的关系
属性:
compile:默认范围,编译测试运行都有效
provider:在编译测试时有效
runtime:测试运行 eg:jdbc
test:测试 eg:junit
system:编译,测试时有效 与本机系统相关,可移植性差
import:导入的范围,只使用在dependencyManagement中,表示从其他的pom中 导入dependcy的配置
依赖传递
依赖冲突
短路优先
a->b->c-x(jar)
a->d->x(jar)
优先解析a->d->x(jar)
先声明先优先
如果路径长度相同,则谁先声明,先解析谁
- 聚合和继承
首先将项目的packaging改为pom,使用modules,将其他的几个子项目加入,这样的话,子项目就不用一个个的去install了。
只需要将父项目install了之后,子项目会自动的install,节省了很多时间
另外使用dependencyManagement可以将重复使用的jar包抽象出来 eg:junit
聚合和继承合并的时候需要及其注意,子项目首先不要继承父项目,等父项目install完成之后再继承,这里楼主满满的泪的教训啊,调了一下午,
<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>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<junit.version>4.10</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 此项目作为其他三个项目的容器进行聚合作用的, -->
<modules>
<module>../hongxing-bge</module>
<module>../hongxing-nange</module>
<module>../hongxing-shanji</module>
</modules>
</project>
子类hongxing-bge hongxing-nange hongxing-shanji三个子项目,全部都使用同一版本的junit,防止版本不兼容
<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>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-shanji</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-nange</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--
此处排除bge的依赖
maven编译默认使用的jre为1.5,可以通过setting.xml修改
-->
<exclusions>
<exclusion>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bge</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
使用maven创建web项目
- 新建maven project的时候将packaging选择为war
步骤:
1:首先查看项目的classes文件的输出路径 build path ->source ->output folder
2:将项目转化成web项目 右键->properties->project Facets ->选择Dynamic 文本modules ->apply
3:修改部署时的默认配置 右键->properties ->deployment Assembly 将测试目录的代码删除,发布产品时是不需要测试文件 的
4:记得将packaging打包方式变成war
5:可以使用jetty来跑项目,或者tomcat都可以
6 : 选择jetty的话在pom文件中加入jetty插件
- 在中央仓库中寻找Jetty :: Jetty Maven Plugin插件
http://www.mvnrepository.com/artifact/org.mortbay.jetty/jetty-maven-plugin/8.1.16.v20140903- 将其加入到pom文件中后,run ->jetty:run
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
</plugin>
</plugins>
</build>
Started Jetty Server之后,在浏览器输入127.0.0.1:8080就可以看到运行结果
如果想要在打包的阶段就运行jetty插件 jetty:run
<build> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.16.v20140903</version> <!-- 在打包成功之后运行jetty文件 --> <executions> <execution> <phase>package</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build>