Maven小节
Maven与包管理
Maven的仓库
三种仓库:
- 本地仓库,自己维护
本地仓库配置,修改settings.xml文件 - 远程仓库(私服),公司维护
- 中央仓库,Maven团队维护
三种仓库的关系
依赖管理
- 依赖范围
type | 对主程序是否有效 | 对测试程序是否有效 | 是否参与打包与部署 | 典型例子 |
compile | Y | Y | Y | struts2-core |
test | N | Y | N | junit |
provided | Y | Y | N | jsp、servlet |
runtime | N | Y | Y | 数据库驱动包 |
- 依赖传递
A->B
B->C
则A->C - 依赖冲突解决方法
- 路径近者优先原则
自己添加jar包 - 第一声明者优先原则
在pom文件的位置在前的优先 - 排除原则
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
在exclusion标签中声明要排除的jar包id(不用写版本)
- 版本锁定
<!-- 设置变量保存版本 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<hibernate.version>5.0.7.Final</hibernate.version>
<struts.version>2.3.24</struts.version>
</properties>
<!-- 锁定版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
Pom文件
项目对象模型
<groupId>cn.itcast</groupId> 组织名称/公司名称/组名称
<artifactId>ssh</artifactId> 项目名称/模块名称
<version>0.0.1-SNAPSHOT</version> 版本号
<packaging>war</packaging> 打包方式
<name></name> 项目名称(可选)
<description></description> 项目描述(可选)
定义变量
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<hibernate.version>5.0.7.Final</hibernate.version>
<struts.version>2.3.24</struts.version>
</properties>
版本锁定
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>(引用变量)
</dependency>
</dependencies>
</dependencyManagement>
添加依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
添加插件
<!-- jdk版本 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>(配置)
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- maven内置 的tomcat6插件 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<!-- 可以灵活配置工程路径 -->
<path>/ssh</path>
<!-- 可以灵活配置端口号 -->
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
内置命令(常用)
命令 | 功能 |
clean | 清理编译的项目 |
compile | 编译项目(仅 main) |
test | (测试项目)编译并运行text目录代码 |
package | 打包 |
install | 把项目发布到本地仓库 |
tomcat:run | 一键启动 |
deploy | 把项目发布到私服 |
生命周期
Clean
Clean Lifecycle 进行构建之前的清理工作。
- pre-clean 执行一些需要在clean之前完成的工作
- clean 移除所有上一次构建生成的文件
- post-clean 执行一些需要在clean之后立刻完成的工作
Default
是整个构建的核心部分,包含了编译,测试,打包,部署等
- compile 编译项目的源代码
- process-resources 复制并处理资源文件,至目标目录,准备打包
- process-test-resources 复制并处理资源文件,至目标测试目录
- test-compile 编译测试源代码
- test 使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署
- package 接受编译好的代码,打包成可发布的格式,如 JAR
- install 将包安装至本地仓库,以让其它项目依赖。
- deploy 将最终的包复制到远程的仓库,以让其它开发人员与项目共享
Site
生成项目报告
- pre-site 执行一些需要在生成站点文档之前完成的工作
- site 生成项目的站点文档
- post-site 执行一些需要在生成站点文档之后完成的工作,并且为部署做准备
- site-deploy 将生成的站点文档部署到特定的服务器
插件
添加插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>(配置)
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</build>
Eclise集成
Nexus私服
安装nexus
启动服务
登录nexus
用户名/密码 admin/admin123仓库类型
Virtual 虚拟仓库
Proxy 代理仓库
Hosted 宿主仓库 本地仓库
Group 组
- 私服的使用(上传)
第一步: 需要在客户端即部署dao工程的电脑上配置 maven环境,并修改 settings.xml 文件,配置连接私服的用户和密码 。
此用户名和密码用于私服校验,因为私服需要知道上传都 的账号和密码 是否和私服中的账号和密码 一致。
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
第二步: 配置项目pom.xml
配置私服仓库的地址,本公司的自己的jar包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为release则上传到私服的release仓库,如果版本为snapshot则上传到私服的snapshot仓库
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
注意:pom.xml这里 和 settings.xml 配置 对应!
第三步:执行deploy命令发布到私服
- 私服的使用(下载)
第一步 修改settings.xml
<profile>
<!--profile的id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库id,repositories可以配置多个仓库,保证id不重复-->
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载releases构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>