一、pom文件
1.依赖
<dependency>
<groupId>com.google.guava</groupId> //组织名,一般为包名
<artifactId>guava</artifactId> //项目名
<version>19.0</version> //版本号
<scope>compile</scope> //范围,有test,compile(编译),runtime(运行,例如jdbc驱动等)
<exclusions> //排除依赖中的某个依赖,常用于解决依赖冲突
<exclusion>
//依赖
</exclusion>
</exclusions>
<optional>false</optional> //是否可选,如果为true的话,那么依赖此模块的项目需要手动声明 https://juejin.cn/post/6844903987322290189
</dependency>
2.打包方式,多模块
<packaging>pom</packaging> //如果当前项目是独立的项目,则用jar,如果当前项目是其他项目的父项目,则用pom打包方式
<modules> //子模块
<module>service</module> //在子模块中需要使用<parent>标签进行父项目的设置
<module>dao</module>
</modules>
3.父项目
<parent> //当前项目的父项目
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
4.定义变量
<properties>
<java-version>1.8</java-version> //标签名就是变量的名字
</properties>
使用: <version>${java-version}</version> 使用${}既可以读取pom文件中的变量,也可以读取配置文件中的变量
4.编译
<build>
<finalName>file</finalName> //最终打包的文件名
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source> //jdk版本
<target>1.8</target>
<compilerArgument>-Xlint:all</compilerArgument> //编译参数
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
5 dependencyManage
这里其实是起到管理依赖jar版本号的作用,一般只会在项目的最顶层的pom.xml中使用到,所有子module如果想要使用到这里面声明的jar,只需要在子module中添加相应的groupId和artifactId即可,并不需要声明版本号,需要注意的是这里面只是声明一个依赖,并不是真实的下载jar,只有在子module中使用到,才会去下载依赖。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
使用一下代码可以代替<parent>标签,用来解决一个项目需要集成多个pom文件的尴尬,只能适用于package 为pom类型
<type>pom</type>
<scope>import</scope>
示例:
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.boom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
6 提交jar 包到远程仓库
如果我们想把自己的新项目打包成jar 包并发布到maven私服上,可以使用该标签并配合 mvn deploy 使用 。
<distributionManagement>
//配置发布版地址
<repository>
<id>nexus-releases</id>
<url>http:/</url>
</repository>
//配置snapshot 版本地址
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://</url>
</snapshotRepository>
</distributionManagement>
二、maven命令
1. mvn clean 清理项目生产的临时文件,一般为target目录
2.mvn package 打包,会在target下生成jar文件或war
3.mvn test : 运行 src/test/java下的测试用例
4.mvn install 模块安装命令 将打包的的jar/war文件复制到你的本地仓库中,供其他模块使用
5.mvn dependency:tree 查看依赖情况,
6.mvn dependency:analyze 帮助你分析依赖关系, 用来取出无用, 重复依赖的好帮手
7 mvn deploy 打包并发布到maven仓库,可以与
三、其他
maven 生命周期: https://www.jianshu.com/p/fd43b3d0fdb0