Java中项目管理与构建工具,目前有Ant,Maven,Gradle工具。
没有这些工具,也可以做开始,但是会增大开发量,我本人也是实际工作中接触到了项目构建工具,才感受到构建工具的好处。在我做的第一个项目总就是用的ant,后来用android studio接触到了gradle,现在做的项目用的是maven,所以学习总结一下。
资源
apache maven官网
www.imooc.com
总结
定义
maven:基于项目对象模型(pom),可以通过一小段描述信息来管理项目的构建、报告和文档的软件项目管理工具
pom:project object model
安装配置
- 官网下载
- 配置环境变量(intellij自带maven) M2-HOME/PATH
目录结构
maven目录
src
-main
-java
-package
-test
-java
-package
-resources
target(maven构建之后生成)
-classes
-surefire-reports(mvn test)
-*.jar/war(mvn package)
常用的构建命令
mvn -v 查看maven版本
compile 编译
test 测试
package 打包
clean 删除target
install 安装jar包到本地仓库中
建立目录骨架
- mvn archetype:generate 手动输入
- mvn archetype:genetate -DgroupId=com.imooc.maven -DartifactId=maven01 -Dversion=1.0-SNAPSHOT -Dpackage=com.imooc.maven
坐标
用于构建
groupId
artifactId
version
仓库
- 本地仓库
- 远程仓库
- 镜像仓库
中央仓库能由于某些原因,不能访问,在maven conf settings.xml在的mirrors配置
更改本地仓库位置
maven/conf/settings.xml中的localRepository
在ide中集成maven
在高版本的eclipse/myeclipse/intellij中都有集成maven
maven的生命周期
clean
default
site
一些命令依次进行
pom.xml解析
<?xml version="1.0" encoding="UTF-8"?>
<!--模板定义-->
<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>com.imooc.maven</groupId>
<!--项目名+模块名-->
<artifactId>maven-model</artifactId>
<!--版本
三个数字:大版本 分支版本 小版本 0.0.1
snapshot
alpha
beta
release
ga 正式版 general availability
-->
<version>0.0.1-SNAPSHOT</version>
<!--默认是jar war jar pom在聚合的时候用-->
<packaging>jar</packaging>
<name>maven学习示例</name>
<url>http://www.site.com</url>
<description>demo</description>
<developers>
</developers>
<licenses>
</licenses>
<organization></organization>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<!--只在测试范围内又用-->
<scope>test</scope>
<!--<optional></optional>-->
<exclusions>
<!--<exclusion></exclusion>-->
</exclusions>
</dependency>
</dependencies>
<!-- 继承的时候用,和parent跟properies组合 -->
<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>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!--<parent></parent>-->
<modules></modules>
</project>
依赖范围
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope
dependency scope
compile 默认的
provided
runtime
test
system
import
依赖传递
A->B->C
A不必要依赖C
exclusions 排除依赖
依赖冲突
短路优先 A->B->C->X A->D->X(优)
先声明先优先 A->B(优) A->C
聚合和继承
聚合: pom modules
继承: parent properies dependencyManagement(只是定义而已,并不会被执行)
使用maven构架web项目
- 注意output目录
- 发布时去掉test目录
- 容器 tomcat jetty