参考慕课网课程:项目管理利器——maven
1..目录结构:
src/main/java/package
src/test/java/package
src/resources
2..常见构建命令:
mvn -v 查看maven版本
compile 编译
test 测试
package 打包
clean 删除target(里面字节码文件、测试报告)
install 安装jar包到本地仓库中
3..创建目录骨架的两种方式:
cmd中maven03目录下:
(1)mvn archetype:generate
(2)mvn archetype:generate -DgroupId= com.imooc.maven04 -DartifactId=maven04-demo -Dversion=1.0.0SNAPSHOT -Dpackage=com.imooc.maven04-demo
(加引号代替,才会不报错,即:)mvn archetype:generate “-DgroupId= com.imooc.maven04” “-DartifactId=maven04-demo” “-Dversion=1.0.0SNAPSHOT” “-Dpackage=com.imooc.maven04-demo”
DgroupId=组织名,公司网址的反写+项目名
DartifactId=(项目在组织中唯一标识)项目名-模块名
Dversion=版本号
Dpackage=代码所存在的包
4..坐标
构件
仓库
本地仓库和远程仓库
镜像仓库(国内的)
E:\maven\apache-maven-3.5.2\lib\maven-model-builder-3.5.2.jar\org\apache\maven\model的pom文件中,https://repo.maven.apache.org/maven2为中央仓库网址
修改镜像仓库位置:E:\maven\apache-maven-3.5.2\conf的settings.xml文件中246行左右内
修改本地仓库位置:还是settings.xml文件中,同时将settings.xml文件复制一份到新的路径下
5..完整的项目构件过程:清理、编译、测试、打包、集成测试、验证、部署
maven三套相互独立的生命周期:
clean清理项目:pre-clean clean post-clean
default构件项目:compile test package install
site生成项目站点:pre-site site post-site site-deploy
6..pom.xml文件中修改:(网站 插件)
http://maven.apache.org/plugins/index.html crtl+f:source
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<!-- 希望在运行package命令时就能将源码进行打包,绑定source到default命令中的package阶段 -->
<executions>
<execution>
<phase>package</phase><!-- 阶段 -->
<goals>
<goal>jar-no-fork </goal><!-- 网站source目标中一个 -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
7..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">
<modelVersion>4.0.0</modelVersion><!-- 指定当前pom的版本 -->
<groupId>com.imooc.hi</groupId><!-- 反写的公司网址+项目名 -->
<artifactId>hi</artifactId><!-- 项目名+模块名 -->
<!--大版本号、分支版本号、小版本号。snapshot快照 alpha内测 beta公测 release稳定 GA正式发布 -->
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging><!-- 打包方式,默认jar zip pom -->
<name>hi</name><!-- 项目表述名,文档。。 -->
<url>http://maven.apache.org</url>
<!-- 项目描述 -->
<description></description>
<developers></developers>
<licenses></licenses>
<organization></organization>
<properties><!-- -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies><!-- 依赖列表 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<!--———————— -->
<type></type>
<scope>test</scope><!--依赖范围-->
<optional></optional><!--设置依赖是否可选:true/false,默认为false(继承的),若为true、子项必须显示地引入-->
<exclusions><!--排除依赖传递列表-->
<exclusion></exclusion>
</exclusions>
</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>
<!--———————— -->
<!-- 希望在运行package命令时就能将源码进行打包,绑定source到default命令中的package阶段 -->
<executions>
<execution>
<phase>package</phase><!-- 阶段 -->
<goals>
<goal>jar-no-fork </goal><!-- 网站source目标中一个 -->
</goals>
</execution>
</executions>
<!--———————— -->
</plugin>
</plugins>
</build>
<parent></parent><!-- 对父模块的继承 -->
<modules><!--聚合运行多个maven项目 -->
<module></module>
</modules>
</project>
8..依赖范围:
引入的junit框架的classpath
编译、测试、运行
网站:http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope
<dependency>
<scope>test</scope><!--依赖范围--> </dependency>
9..依赖传递:
<dependency>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-nange</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions><!--排除依赖传递列表-->
<exclusion>
<groupId>com.hongxing</groupId>
<artifactId>hongxing-bge</artifactId>
</exclusion>
</exclusions>
</dependency>
10..依赖冲突:
如果A、B依赖了不同版本的相同构件,对于依赖于A和B的C来说,它依赖哪个
(1)短路优先(路径短)
(2)路径相同时,先声明先优先(谁先声明,先解析谁)
11..聚合、继承:
<parent></parent><!-- 定义于子类模块中,对父模块的继承 -->
<modules><!--聚合运行多个maven项目 -->
<module></module>
</modules>
<!-- 依赖的管理,主要定义于父类模块中,让子模块继承,父模块本身所在项目不会运行 -->
<dependencyManagement>
<dependencies>
<dependency></dependency>
</dependencies>
</dependencyManagement>
聚合例子:
<modules>
<module>../hongxing-bge</module>
<module>../hongxing-nange</module>
<module>../hongxing-shanji</module>
</modules>
clean install
继承例子:
父模块:
<packaging>pom</packaging>
<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>
子模块:
<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>
</dependencies>
12..构建web项目:
filter 不选quickstart,搜索框输入web
用jetty:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc.webdemo</groupId>
<artifactId>webdemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webdemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>webdemo</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.8.v20171121</version>
<!-- 在打包成功后就直接运行jetty插件 -->
<!-- jetty:run -->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<stopPort>9966</stopPort>
<stopKey>foo</stopKey>
<stopWait>10</stopWait>
</configuration>
</plugin>
</plugins>
</build>
</project>
用tomcat:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc.webdemo</groupId>
<artifactId>webdemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webdemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>webdemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<!-- 在打包成功后就直接运行jetty插件 -->
<!-- jetty:run -->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>