认识 maven
-
什么是maven?
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information
软件项目管理工具,基于POM管理,构建,发布项目
-
优势
a. **约定优于配 ** b. 简单 c. 测试支持 d. 构建简单 e. CI f. 插件丰富
-
下载和安装
超级pom位置 :\lib\maven-model-builder-***.jar/org/apache/maven/model/pom.xml
maven 运行一个mvn命令时,首先会找 ~/.m2/setting.xml,如果不存在会找安装目录下的conf/setting.xml
3.配置settiing.xml
-
: 本地库位置
-
-
<mirrors> <mirror> <id>maven-public</id> <mirrorOf>aliyun,central,spring,jboss,thirdparty</mirrorOf> <url>https://maven.aliyun.com/repository/public</url> </mirror> </mirrors>
-
-
-
项目结构
my-app |-- pom.xml `-- src |-- main | `-- resource | `-- java | `-- com | `-- mycompany | `-- app | `-- App.java `-- test `-- java `-- com `-- mycompany `-- app `-- AppTest.java
-
依赖仲裁
- 最短路径原则 2.加载顺序原则
-
: 排除传递的依赖jar包
-
scope
- compile: 编译需要,会打包部署
- test :测试依赖, 不会打包部署 --> junit
- provided: 编译需要,不会打包 —> tomcat servelt api
- runtime: 运行时,编译不需要,会打包—> mysql-connector
- system: 本地的一些jar —> 短信jar
- import :This scope is only supported on a dependency of type
pom
in the<dependencyManagement>
section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s<dependencyManagement>
section. Since they are replaced, dependencies with a scope ofimport
do not actually participate in limiting the transitivity of a dependency.
-
scope的依赖传递
-
若 A.jar —>B(scope=test) --> C(scope:compile) ,若D依赖A , 则D不会依赖B,C
-
间接传递jar的scope
- 左侧栏scope为直接依赖项目的scope, 上栏为依赖项目传递依赖的scope, 结果为传递到当前项目的最终scope,
-
代表不会传递依赖
compile provided runtime test compile compile(*) - runtime - provided provided - provided - runtime runtime - runtime - test test - test -
-
-
生命周期
一个生命周期是由多个phase构成,一个phase有多个gloals组成
-
lifecycle
- clean
- phase: pre-clean --> clean --> post-clean
- default
- validate: validate the project is correct and all necessary information is available(验证工程是否正确并且必要的信息可用)
- compile: compile the source code of the project (编译项目的源代码)
- package: take the compiled code and package it in its distributable format, such as a JAR. (编译和打包生成特殊格式的文件)
- verify :run any checks to verify the package is valid and meets quality criteria (检查和验证包的有效性和是否符合质量标准)
- test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed (用一个单元测试框架测试编译后的代码。测试代码不需要被打包和发布)
- install: install the package into the local repository, for use as a dependency in other projects locally (安装包到本地仓库,作为其他工程的依赖)
- deploy:done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. (集成发布环境的操作,复制包到远程仓库)
- …
- site
4. pre-site
5. site
6. post-site
7. site-deploy
- clean
-
-
命令
mvn clean dependency:copy-dependencies package
这行命令会产生以下效果
-
清理 target下编码的文件
-
将项目依赖的jar文件 复制到 相应工程 target/dependency/ 目录下
-
打包相应类型的工程到 target下 ,打包文件命名格式: g r o u p I d − {groupId}- groupId−{artifactId}-${version}.jar/war
mvn dependency:tree
查看maven的依赖关系树
-
-
插件
-
推荐使用的插件
-
findbugs : 静态代码检查
- 根据字节码操作
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plugin</artifactId> <version>3.0.0</version> <configuration> <!-- <configLocation>${basedir}/springside-findbugs.xml</configLocation> --> <threshold>High</threshold> <effort>Default</effort> <findbugsXmlOutput>true</findbugsXmlOutput> <!-- findbugs xml输出路径--> <findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory> </configuration> </plugin>
-
versions :统一修改版本号
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.3</version> </plugin>
-
source: 生成源码jar文件
<plugin> <artifactId>maven-source-plugin</artifactId> <version>2.3</version> <executions> <execution> <id>attach-sources</id> <phase>install</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin>
assembly: 帮助项目打包 war zip
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution><!-- 配置执行器 --> <id>make-assembly</id> <phase>package</phase><!-- 绑定到package生命周期阶段上 --> (1) <goals> <goal>single</goal><!-- 只运行一次 --> </goals> (2) <configuration> <finalName>${project.name}</finalName> <!--主类入口等--> ... <descriptor>src/main/assembly/assembly.xml</descriptor><!--配置描述文件路径--> </configuration> </execution> </executions>
-
-