JavaEE- project management tools -Maven learning diary
Article Directory
- JavaEE- project management tools -Maven learning diary
Outline
This is what we usually project development to on-line process experienced
Note: Construction of the project is not reflected in the development but in the end the test server
Where the project build process is as follows
This is our project development to deployment of a build process, we need to rely on automated maven
meaning the maven : maven development and testing follow the directory structure, convenient transfer
Maven conceptual model
pom.xml: Project Object Model (Project Object Model) , each project will be handled as an object
Maven installation
- Check the JDK installation
Enter the following command cmd
Echo %JAVA_HOME%
Java -version
-
Extracting installation package (unzipped do not have Chinese)
-
Set the user environment variables -Maven_Home
- Configure the system environment variable Path
- New system environment variables MEVEN_OPTS
- verification
cmd enter the following command
mvn -v
See the following message on behalf of the installation was successful
Maven directory structure
- bin: contains a script that runs mvn
- boot: containing plexus-classworlds class loader frame
- conf: contains settings.xml configuration file
- lib: Java class libraries containing the required run maven
- Other: introduction, license, instructions, etc.
Convention over configuration
We prepared hard to follow Maven project agreement
src 源代码和测试代码的根目录
main 应用代码的源目录
java 源代码 .java文件
resources 项目的资源文件-ssm配置文件
test 测试代码的源目录
java 测试代码 测试.java
resources 测试的资源文件
target 编译后的类文件、jar文件等(项目输出位置)
pom.xml 项目配置文件
IDEA-Maven configuration
Maven is configured to at File-> Settings-> Build, Execution, Deployment-> Build Tools-> Maven, personal configuration as shown in FIG.
- Settings File the User : Maven's settings.xml designated location
- local repository: 指定Maven的本地仓库位置,是读取settings.xml自动配置的
- maven home directory:指定本地Maven的安装目录所在,因为我已经配置了MAVEN_HOME系统参数,所以直接这样配置IntelliJ IDEA 是可以找到的,但是假如你没有配置的话,这里可以选择你的Maven安装目录
- VM options for importer:可以设置导入的VM参数,一般这个都不需要主动改,除非项目真的导入太慢了我们再增大此参数
- Import Maven projects automatically:表示IntelliJ IDEA会实时监控项目的pom.xml文件进行项目变动设置,建议进行勾选
- Sources和Documentation:表示在Maven导入依赖包的时候是否自动下载源码和文档,默认是没有勾选的也不建议勾选,原因是这样可以加快项目从外网导入依赖包的速度,如果我们需要源码和文档的时候我们到时候再针对某个依赖包进行联网下载即可,IntelliJ IDEA 支持直接从公网下载源码和文档的
IDEA-Maven聚合工程搭建
File -> new ->project
添加非Web子模块
添加Web子模块
使用maven web项目模板
三维坐标与依赖
无论自己建立的maven项目还是第三方的maven依赖包都有坐标,想要查找一个依赖包就根据坐标来找
- groupId:定义当前Maven项目隶属项目,组织名称+"."+项目名称
- artifactId:项目的模块名
- version:项目版本号
以上是parent模块的三维坐标
依赖管理
在需要依赖的模块(项目)的pom.xml中配置
< dependencies>
< dependency>
要依赖的包的坐标
< /dependency>
< /dependencies>
对第三方依赖包的查找去中央仓库来查找第三方包的坐标即可
依赖管理还可以对自己的项目做依赖
只需要把被依赖项目的三位坐标配置到目标项目的pom.xml下即可
-
不需要把被依赖的工程安装到仓库
-
被依赖工程的classpath下的代码就相当于在依赖工程classpath下
依赖范围
maven项目有三套classpath
- Java主目录的classpath
- test测试目录的classpath
- 运行时的classpath
依赖范围:就是指定的一个依赖包对那些classpath有效
使用< scope>< /scope>
来配置依赖的范围
常用的依赖范围:compile,test,provided,runtime
< scope>compile< /scope>
三套classpath需要这个包
< scope>test< /scope>
只对test的classpath有效-Junit
< scope>provided< /scope>
只对test和Java主目录的classpath有效
< scope>runtime< /scope>
只对运行时的classpath有效
依赖传递
依赖传递
A<—B第一直接依赖(A依赖B)
B<—C第二直接依赖(B依赖C)
A<—C间接依赖(A就会依赖C)
依赖传递的范围传递
-代表不会被间接传递
版本冲突问题:
A对log4j1.2.9依赖,B对log4j1.2.8依赖,那么C就会依赖1.2.8
因为:当A和B存在依赖时,而且A和B中都依赖了一个依赖包,但是版本不一样,C就会依赖步长较近的版本
依赖可选问题:
<dependency>
<groupId>cn.itcast.maven</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
<optional>false</optional>
</dependency>
当optional是true这个依赖包就不会传递下去,如果是false就可以传递,默认是false
排除依赖:
通过exclusions配置要排除的依赖包的groupId和artifactId即可
如
<dependency>
<groupId>cn.itcast.maven</groupId>
<artifactId>HelloFriend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>cn.itcast.maven</groupId>
<artifactId>hello</artifactId>
</exclusion>
</exclusions>
</dependency>
仓库管理
本地仓库:
默认情况下在~/.m2/repository/文件夹就是我们的仓库,
当我们来执行maven的命令或者添加依赖包时,如果本地仓库不存在我们需要的依赖,那么maven就会到中央仓库去下载到本地仓库,如果第二次再来使用同样的依赖包就不需要下载了,本地仓库可以上传或者安装自己的依赖包,使用的命令是mvn install
本地仓库的位置可以修改,我们最好不要把它放在默认位置,修改时直接把仓库做剪切放到指定位置,修改用户范围的settings.xml,把localRepository的标签值修改成改变后的仓库的位置,然后rebuild index(重启IDEA)
苍库的位置可以修改,我们最好不要把它放在默认位置,修改时直接把仓库做剪切放到指定位置,修改用户范围的settings.xml,把localRepository的标签值修改成改变后的仓库的位置,然后rebuild index(重启IDEA)
Central warehouse:
Internet maven is a huge warehouse, and there are the vast majority of dependencies,
but there are a few dependencies are not central warehouse in this http://mvnrepository.com/ can find a central repository dependencies site coordinates, we can not upload dependencies central warehouse
PW Warehouse:
-slightly