maven项⽬的创建与配置、maven命令⾏操作、参数说明、依
赖管理
⼀、建maven项⽬(eclip ee版本)
选这两项就可以建maven项⽬了
⼆、设置maven
⼀是修改l(maven的conf⽬录)
修改本地仓库地址:<localRepository>d:/.m2/repository</localRepository>
在mirrors加上阿⾥云的镜像:
三、查询maven包:
找到相关包,⽐如junit:
四、建⽴测试类⼩技巧:
选中主类,建⽴junit类:
广东科技贸易职业学院
然后拖到test⽬录下。
五、maven命令
1,mvn -v 查看版本信息,
2,mvn compile 编译 在项⽬根⽬录下
3,mvn test运⾏测试⽂件
4,mvn package 对项⽬进⾏打包
5,mvn clean 删除target⽬录
6,mvn install 安装项⽬jar包到本地仓库
7,archetype 插件:创建符合maven规定的项⽬⾻架
mvn archetype:generate 会在空的⽬录中建⽴maven项⽬
Choo a number or apply filter,7建⽴quick start项⽬
然后填⼊group-id等,也可以如下⽅式直接指定
D:\workspace\m3>mvn archetype:generate -DgroupId=commaven -DartifactId=
maven-test01 -Dversion=0.0.1-snapshot -Dpackage=st
六、参数说明(l)
modelVersion:当前pom的版本号
groupId:组织名,公司名⽹址反过来写+项⽬名称
artifactId:项⽬名+模块名
name:也是项⽬名,如果不填,默认是artifactId
version:版本号
0.0.1-SNAPSHOT 第⼀个数表⽰⼤版本好,第⼆个数表⽰分⽀版本号,第三个数表⽰⼩版本号
SNAPSHOT :快照版本,开发阶段
alpha:内部测试版本
beta:公测版本
relea:稳定版本
package:代码的包路径
事业线是哪一条GA:正式发布
Dependency scope is ud to limit the transitivity of a dependency, and also to affect the classpath ud for various build tasks.
There are 6 scopes available:
compile 默认范围,整个编译测试运⾏阶段有效
This is the default scope, ud if none is specified. Compile dependencies are available in all classpaths of a project.
怎样炒虾好吃又简单Furthermore, tho dependencies are propagated to dependent projects.
provided 编译测试 有效
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterpri Edition, you would t the dependency on the Servlet API and related Java EE APIs to scope provided becau the web container provides tho class. This scope is only available on the compilation and test classpath, and is not transitive.
runtime 运⾏ 测试有效
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
test 只在测试阶段 有效
This scope indicates that the dependency is not required for normal u of the application, and is only available for the test compilation and execution phas. This scope is not transitive.
system 跟本机系统相关联 可⼀致性差
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
import 导⼊的范围,在 dependencyManagement中,表⽰从其他的pom中导⼊dependency的配置作⽤
This scope is only supported on a dependency of type pom in the <dependencyManagement> ction. It indicates the dependency to be replaced with the effective list of dependencies in the specified
POM's <dependencyManagement> ction. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
这个scope在继承⽗⼯程事是很有⽤的啦,⽐如你⽤了parent继承⼀个⽗⼯程,但是dependency不写版本号就报错,这时,加上type为pom,scope为import就可以了(在另⼀篇spring boot整合测试中会进⾏说明)。
packaging :有jar、war、zip、pom等⽅式打包
url:项⽬地址
description:项⽬描述
optiona :设置依赖是否可选
exclusions:排除依赖传递列表
dependencyManagement:进⾏依赖管理
string
plugins:插件管理
parent:⽗级
modules、module:模块
scope:依赖作⽤域范围
七,maven的⽣命周期
clean compile test package intall
完成的项⽬构建过程
清理 编译 测试 打包 继承测试 --部署到服务器
Clean 清理项⽬
pre-clean:执⾏清理前的⼯作
Clean:清理上⼀次构建产⽣的所有⽂件
Post-clean:执⾏清理后的⼯作
Default 构建项⽬(核⼼)
Compile \ test 、 package 、instal
site ⽣成站点 进⾏访问
Pre-site:项⽬⽣成站点的⼯作
Site:⽣成⽹站⽂件
Post-site:⽣成后的⼯作
Site-deploy:发布到服务器
⼋、maven的依赖冲突及管理
1,谁离得近,谁优先,
a)son ->mother -> father -> x-jar包
如果father和mother都有x.jar,则继承mother的包
b)son ->mother ->x.jar包
2,谁先声明谁优先
谁在前,⽤谁的依赖
son ->mother ->x.jar包
vc的功效son ->father ->x.jar包
九、maven的聚合和继承
广告时间1,maven的聚合,就是多模块的组合。聚合的项⽬的pom注意配置两个地⽅:
<!-- 聚合项⽬需要⽤pom -->
<packaging>pom</packaging>
<!-- 模块部分 -->
<modules>
<module>../yilai-mother</module>
<module>../father</module>
<module>../son</module>
</modules>
配置完后,我个⼈的感觉就是⼀导包全都能导进来我是追梦人
2,依赖的继承,如下语句即为继承:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
⽤eclip依次点开,会找到⼀堆的依赖版本。个⼈感觉parent的主要作⽤就在于统⼀版本信息。继承的依赖可以从⽗依赖的<dependencyManagement>版本信息
⼗、profile设置
有时候,我们在开发和部署的时候,有很多配置⽂件数据是不⼀样的,⽐如连接mysql,连接redis,⼀些properties⽂件等等
每次部署或者开发都要改配置⽂件太⿇烦了,这个时候,就需要⽤到maven的profile配置了
配置内容⼤概如下:
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prd</id>
<properties>
<env>prd</env>
</properties>
</profile>
</profiles>
其中id代表这个环境的唯⼀标识,下⾯会⽤到
properties下我们我们⾃⼰⾃定义了标签env,内容分别是dev和prd。
activeByDefault=true代表如果不指定某个固定id的profile,那么就使⽤这个环境
2,下⾯是我们resources下的⽬录,有两个⽬录,dev和prd,在开发时,我们使⽤dev下的配置⽂件,部署时候使⽤prd下的配置⽂件
3配置l,如果直接install,那么就会找到默认的id为dev的这个profile,然后会在⾥⾯找env的节点的值,
自我介绍女生接着就会执⾏替换,相当于将src/main/resources/dev这个⽂件夹下的所有的配置⽂件打包到class根⽬录下。
<build>
<finalName>springmvc2</finalName>
<resources>
<resource>
<directory>src/main/resources/${env}</directory>
</resource>
</resources>
</build>