目录
六、基于IDEA的Maven使用
6.1 在IDEA中关联Maven
maven配置 |
---|
说明:
IDEA本身集成了Maven,考虑到IDEA和Maven版本的兼容性,Idea不建议配置比默认版本更新的版本,建议使用idea自带的maven。
6.2 使用IDEA创建Maven项目
6.2.1 Java项目
创建Java项目 |
---|
6.2.2 web项目
创建maven项目:maven-demo2
在pom.xml文件设置打包方式为war
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.wolf</groupId> <artifactId>maven-demo2</artifactId> <version>1.0.0</version> <!--设置项目打包方式为war--> <packaging>war</packaging> </project>
完成web项目结构
配置web组件—Tomcat
部署web项目
6.3 在IDEA中使用Maven进行依赖管理
6.3.1 查找依赖坐标
6.3.2 添加依赖
将依赖的坐标配置到项目的pom.xml文件dependencies标签中
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.wolf</groupId> <artifactId>maven-demo2</artifactId> <version>1.0.0</version> <!--设置项目打包方式为war--> <packaging>war</packaging> <dependencies> <!--在此位置配置项目中所需依赖的坐标 GAV--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.18</version> <scope>provided</scope> </dependency> </dependencies> </project>
6.3.3 依赖范围
在通过dependency添加依赖时,可以通过
scope
标签配置当前依赖的适用范围
test 只在项目测试阶段引入当前依赖(编译、测试)
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>
runtime 只在运行时使用(运行、测试运行)
provided 在(编译、测试、运行)
compile 在(编译、测试、运行、打包)都引入
6.4 在IDEA中使用Maven进行项目构建
6.4.1 Maven项目构建生命周期说明
clean 清理缓存 清理项目生成的缓存
validate 校验 验证项目需要是正确的(项目信息、依赖)
compile 编译 编译项目专供的源代码
test 测试 运行项目中的单元测试
package 打包 将项目编译后的代码打包成发布格式
verify 检查 对集成测试的结果进行检查、确保项目的质量是达标的
install 安装 将包安装到maven的本地仓库,以便在本地的其他项目中可以引用此项目(聚合工程)
deploy 部署 将包安装到私服的仓库,以供其他开发人员共享
6.4.2 IDEA进行生命周期管理
可视化
终端指令
选择项目名称---右键---Open in Terminal
mvn clean
七、私服
企业搭建供内部使用的一个Maven仓库
开发者需要依赖直接从私服下载
私服可以实现企业内部依赖的共享:当企业开发了一个通用插件(jar),可以发布到私服,可以连接到当前私服的其他开发者就可以共享这个插件了
7.1 私服搭建
我们可以通过专门的Maven仓库管理软件来搭建私服。例如:Apache Archiva、Nexus
7.1.1 下载Nexus
7.1.2 解压nexus
7.1.3 安装并运行nexus
进入到
nexus-2.14.11-01/bin
目录管理员身份打开cmd命令行,执行指令
7.1.4 登录Nexus
私服管理器登录地址: http://localhost:8081/nexus
私服中仓库类型
私服仓库组的配置
7.2 配置私服
7.2.1 在maven的settings.xml文件的servers
标签中
<!--配置连接私服所需的帐号和密码--> <server> <id>nexus-public</id> <!-- nexus的认证id --> <username>admin</username> <!--nexus中的用户名密码--> <password>admin123</password> </server>
7.2.2 在maven的settings.xml文件的profiles
标签中
repository 和 pluginRepository的 id子标签的值,要和上面配置的server 的id子标签的值一致
<profile> <id>nexus</id> <repositories> <repository> <id>nexus-public</id> <name>Nexus Release Snapshot Repository</name> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus-public</id> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile>
7.2.3 在maven的settings.xml文件的activeProfiles
配置激活profile
<activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles>