Maven项目管理利器,maven项目管理
分享于 点击 16450 次 点评:233
Maven项目管理利器,maven项目管理
一、maven的目录结构
- -src
- -main
- -java
- -test
- -java
- -main
- -pom.xml
二、 maven常用命令
- mvn -v 查看maven版本
- mvn -compile 编译程序
- mvn -clean 删除target
- mvn -install 安装jar包到本地仓库中
三、 创建目录的两种方式
- archetype:generate 按照提示进行选择
- archetype:generate
-Dgroup=组织名,公司网址的反写+项目名
-DartifactId=项目名-模块名
-Dversion=版本号
-Dpackage=代码所存在的包名
四、Maven中的坐标和仓库
构件坐标
- groupId:公司名字+项目名
- artifactId:项目名+模块名
- varsion:版本号
仓库
- 本地仓库
- 远程仓库
- 镜像仓库
五、更改仓库默认路径
安装路径conf文件夹下settings.xml文件 打开找到这段备注是的代码:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
复制粘贴出来
<localRepository>/path/to/local/repo</localRepository>
将localRepository便签内的值替换成新路径即可。
六、完整的项目构件过程
清理、编译、测试、打包、集成测试、验证、部署。
六、Maven的生命周期
完整的项目构件过程
清理、编译、测试、打包、集成测试、验证、部署。
maven三套独立的生命周期
clean 清理项目
1.pre-clean 执行清理前的工作
2.clean 清理上一次构建生成的所有文件
3.post-clean 执行清理后的文件
default 构建项目(最核心)
compile test package install
site 生成项目站点
1. pre-site 在生成项目站点前要完成的工作
2. site 生成项目的站点文档
3. post-site 在生成项目站点后要完成的工作
4. site-deploy 发布生成的站点到服务器上
七、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.tiakon.maven.demo</groupId>
<artifactId>HoictasStudio-MavenDemo01</artifactId>
<version>1.0-SNAPSHOT</version>
<!--
第一个0表示大版本号
第二个0表示分支版本号
第三个0表示小版本号
0.0.1
snapshot 快照
alpha 内部测试
beta 公测
Release 稳定
GA 正式发布
-->
<!--
打包方式:默认是jar,可选war、zip、pom
<packaging></packaging>
-->
<!--指定编码格式-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--
项目名
<name></name>
项目地址
<url></url>
项目描述
<description></description>
开发人员列表
<developers></developers>
许可证信息
<licenses></licenses>
组织信息
<organization></organization>
-->
<!--依赖列表-->
<dependencies>
<!--依赖项-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<!--<type></type>-->
<!--依赖范围-->
<!--<scope></scope>-->
<!--设置依赖是否可选(默认)false-->
<!--<optional></optional>-->
<!--排斥依赖传递列表-->
<!--
<exclusions>
<exclusion>
</exclusion>
</exclusions>
-->
</dependency>
</dependencies>
<!--依赖的管理,作用主要定义在父模块中,对子模块进行管理-->
<!--
<dependencyManagement>
<dependencies>
</dependencies>
</dependencyManagement>
-->
<!--对构件的行为提供相应的支持-->
<build>
<!--插件列表-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>
jar-no-fork
</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!--通常用于子模块对父模块pom的继承-->
<!--<parent></parent>-->
<!--用来聚合运行Maven项目,指定多个模块一起编译-->
<!--
<modules>
<module></module>
</modules>
-->
</project>
八、依赖范围
三种classpath
1. 编译
2. 测试
3. 运行
maven提供了6种可选依赖范围:
Importing Dependencies
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>maven</groupId>
<artifactId>B</artifactId>
<packaging>pom</packaging>
<name>B</name>
<version>1.0</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>maven</groupId>
<artifactId>A</artifactId>
<version>1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>test</groupId>
<artifactId>d</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>a</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>test</groupId>
<artifactId>c</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
假设A是前面示例中定义的pom,那么最终结果将是相同的。所有管理的依赖项都将被合并到B中,除了在这个pom中定义的d之外。
九、依赖传递
【C依赖B,B依赖A,那么C就依赖A】
十、依赖冲突
1.短路优先:
C->B->A->X1(jar)
C->B->X2(jar)
【C依赖B,B依赖A,A和B都包含同一个不同版本的Jar,则取B的依赖版本。(c的pom.xml中不必注明jar坐标)】
2.先声明先优先
如果路径相同长度相同,则谁先声明,先解析谁。
【C依赖A和B,A和B都包含同一个不同版本的Jar,谁依赖在前取谁的依赖版本。】
十一、聚合和继承
聚合
创建一个新的maven容器,在pom.xml中添加如下代码
<modules>
<module>../hongxing-bge</module>
<module>../hongxing-nage</module>
<module>../hongxing-shanji</module>
</modules>
输入clean install命令后,即可同时安装多个jar到本地仓库中
[INFO] hongxing-bge ............................... SUCCESS [ 2.745 s]
[INFO] hongxing-nage .............................. SUCCESS [ 0.556 s]
[INFO] hongxing-shanji ............................ SUCCESS [ 0.526 s]
[INFO] hongxing-aggreation ........................ SUCCESS [ 0.078 s]
继承
parent—>pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>3.8.1</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
child继承了parent
child—>pom.xml
<parent>
<groupId>com.dlf.hongxing</groupId>
<artifactId>hongxing-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
相关文章
- 暂无相关文章
用户点评