项目管理工具maven

项目管理工具maven

maven基础

Maven 的好处
  1. 省磁盘空间 可以一键构建
  2. 可以跨平台
  3. 应用在大型项目时可以提高开发效率
安装配置 maven

下载解压即可

三种仓库
  1. 本地仓库

  2. 远程仓库(私服)

  3. 中央仓库

常见的命令

Compile
Test
Package
Install
Deploy
Clean

坐标的书写规范

groupId 公司或组织域名的倒序
artifactId 项目名或模块名
version 版本号

如何添加坐标

1、在本地仓库中搜索
2、互联网上搜,推荐网址 http://www.mvnrepository.com/

http://search.maven.org/

依赖范围

Compile
Test
Runtime
Provided

详细maven基础:https://blog.csdn.net/weixin_44722917/article/details/109661151

依赖冲突

spring-webmvc 依赖 spirng-beans-4.2.4,spring-context 依赖 spring-beans-5.0.2,但是发现spirng-beans-4.2.4 加入到工程中,而我们希望 spring-beans-5.0.2 加入工程。这就造成了依赖冲突。

依赖调解原则
  1. 第一声明者优先原则

    在 pom 文件定义依赖,先声明的依赖为准。

  2. 路径近者优先原则

    spring-contex 和 spring-webmvc 都会传递过来 spirng-beans,那如果直接把 spring-beans 的依赖直接写到文件中,那么项目就不会再使用其他依赖传递来的 spring-beans,因为自己直接在 中定义 spring-beans 要比其他依赖传递过来的路径要近。

排除依赖

在依赖 spring-webmvc 的设置中添加排除依赖,排除 spring-beans,

下边的配置表示:依赖 spring-webmvc,但排除 spring-webmvc 所依赖的 spring-beans。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.2.4.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>

锁定版本

面对众多的依赖,有一种方法不用考虑依赖路径、声明优化等因素可以采用直接锁定版本的方法确定依赖构件的版本,版本锁定后则不考虑依赖的声明顺序或依赖的路径,以锁定的版本的为准添加到工程中,此方法在企业开发中常用。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>
</dependencyManagement>

还可以把版本号提取出来,使用<properties>标签设置成变量。

<properties>
    <spring.version>5.0.2.RELEASE</spring.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

注意:在工程中锁定依赖的版本并不代表在工程中添加了依赖,如果工程需要添加锁定版本的依赖则需要单独添加<dependencies></dependencies>标签,

上边添加的依赖并没有指定版本,原因是已在<dependencyManagement>中锁定了版本,所以在<dependency>下不需要再指定版本。

分模块构建工程

理解继承和聚合

继承是为了消除重复,如果将 dao、service、web 分开创建独立的工程则每个工程的 pom.xml文件中的内容存在重复,比如:设置编译版本、锁定 spring 的版本的等,可以将这些重复的配置提取出来在父工程的 pom.xml 中定义。

项目开发通常是分组分模块开发,每个模块开发完成要运行整个工程需要将每个模块聚合在一起运行,比如:dao、service、web 三个工程最终会打一个独立的 war 运行。

依赖范围对传递依赖的影响

是因为依赖会有依赖范围,依赖范围对传递依赖也有影响,例如有 A、B、C,A 依赖 B、B依赖 C,C 可能是 A 的传递依赖,如下图:

在这里插入图片描述

最左边一列为直接依赖,理解为 A 依赖 B 的范围,最顶层一行为传递依赖,理解为 B依赖 C 的范围,行与列的交叉即为 A 传递依赖 C 的范围。

maven 私服

正式开发,不同的项目组开发不同的工程。

ssm_dao 工程开发完毕,发布到私服。

ssm_service 从私服下载 dao

公司在自己的局域网内搭建自己的远程仓库服务器,称为私服,私服服务器即是公司内部的 maven 远程仓库,每个员工的电脑上安装 maven 软件并且连接私服服务器,员工将自己开发的项目打成 jar 并发布到私服服务器,其它项目组从私服服务器下载所依赖的构件(jar)。

私服还充当一个代理服务器,当私服上没有 jar 包会从互联网中央仓库自动下载,

搭建私服环境
下载 nexus

Nexus 是 Maven 仓库管理器,通过 nexus 可以搭建 maven 仓库,同时 nexus 还提供强大的仓库管理功能,构件搜索功能等。

下载 Nexus, 下载地址:http://www.sonatype.org/nexus/archived/

安装 nexus

解压 nexus-2.12.0-01-bundle.zip,将它解压在 F 盘,进入 bin 目录

cmd 进入 bin 目录,执行 nexus.bat install

安装成功在服务中查看有 nexus 服务

卸载 nexus

cmd 进入 nexus 的 bin 目录,执行:nexus.bat uninstall

查看 window 服务列表 nexus 已被删除。

启动 nexus

方法 1:cmd 进入 bin 目录,执行 nexus.bat start

方法 2:直接启动 nexus 服务

查看 nexus 的配置文件 conf/nexus.properties

# Jetty section 
application-port=8081 # nexus 的访问端口配置
application-host=0.0.0.0 # nexus 主机监听配置(不用修改) 
nexus-webapp=${bundleBasedir}/nexus # nexus 工程目录
nexus-webapp-context-path=/nexus # nexus 的 web 访问路径
# Nexus section 
nexus-work=${bundleBasedir}/../sonatype-work/nexus # nexus 仓库目录
runtime=${bundleBasedir}/nexus/WEB-INF # nexus 运行程序目录
访问:

http://localhost:8081/nexus/

使用 Nexus 内置账户 admin/admin123 登陆:

点击右上角的 Log in,输入账号和密码 登陆

仓库类型

nexus

查看 nexus 的仓库:

在这里插入图片描述

nexus 的仓库有 4 种类型:

  1. hosted,宿主仓库,部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 两部分,Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库

  2. proxy,代理仓库,用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私服自动去中央仓库下载 jar 包或者插件。

  3. group,仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓库组。

  4. virtual(虚拟):兼容 Maven1 版本的 jar 或者插件

nexus 仓库默认在 sonatype-work 目录中

central:代理仓库,代理中央仓库

apache-snapshots:代理仓库
存储 snapshots 构件,代理地址 https://repository.apache.org/snapshots/

central-m1:virtual 类型仓库,兼容 Maven1 版本的 jar 或者插件

releases:本地仓库,存储 releases构件。

snapshots:本地仓库,存储 snapshots构件。

thirdparty:第三方仓库

public:仓库组

将项目发布到私服

企业中多个团队协作开发通常会将一些公用的组件、开发模块等发布到私服供其它团队或模块开发人员使用。

本例子假设多团队分别开发 ssm_dao、ssm_service、ssm_web,某个团队开发完在ssm_dao 会将 ssm_dao 发布到私服供 ssm_service 团队使用,本例子会将 ssm_dao 工程打成jar 包发布到私服。

配置

第一步:需要在客户端即部署 ssm_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>

releases 连接发布版本项目仓库

snapshots 连接测试版本项目仓库

第二步: 配置项目 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 这里<id> 和 settings.xml 配置 <id> 对应!

测试

将项目 dao 工程打成 jar 包发布到私服:

1、首先启动 nexus

2、对 ssm_dao 工程执行 deploy 命令

根据本项目pom.xml中version定义决定发布到哪个仓库,如果version定义为snapshot,执行 deploy后查看 nexus 的 snapshot仓库,如果 version定义为 release则项目将发布到 nexus的 release 仓库,本项目将发布到 snapshot 仓库

从私服下载 jar 包

没有配置 nexus 之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器,有了私服本地项目首先去本地仓库找 jar,如果没有找到则连接私服从私服下载 jar 包,如果私服没有 jar 包私服同时作为代理服务器从中央仓库下载 jar 包,这样做的好处是一方面由私服对公司项目的依赖 jar 包统一管理,一方面提高下载速度,项目连接私服下载 jar 包的速度要比项目连接中央仓库的速度快的多

管理仓库组

nexus中包括很多仓库,hosted中存放的是企业自己发布的 包及第三方公司的jar包,proxy 中存放的是中央仓库的 ,为了方便从私服下载 jar 包可以将多个仓库组成一个仓库组,每个工程需要连接私服的仓库组下载 jar 包。

在这里插入图片描述

上图中仓库组包括了本地仓库、代理仓库等。

在 setting.xml 中配置仓库

在客户端的 setting.xml 中配置私服的仓库,由于 setting.xml 中没有 repositories 的配置标签需要使用 profile 定义仓库。

<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>

使用 profile 定义仓库需要激活才可生效。

   <activeProfiles> 
<activeProfile>dev</activeProfile> 
 </activeProfiles>

配置成功后通过 eclipse 查看有效 pom,有效 pom 是 maven 软件最终使用的 pom 内容,程序员不直接编辑有效 pom,打开有效 pom

有效 pom 内容如下:

下边的 pom 内容中有两个仓库地址,maven 会先从前边的仓库的找,如果找不到 jar 包再从下边的找,从而就实现了从私服下载 jar 包。

<repositories> 
 <repository> 
 <releases> 
 <enabled>true</enabled> 
 </releases> 
 <snapshots> 
 <enabled>true</enabled> 
 </snapshots> 
 <id>public</id> 
 <name>Public Repositories</name> 
 <url>http://localhost:8081/nexus/content/groups/public/</url> 
 </repository> 
 <repository> 
 <snapshots> 
 <enabled>false</enabled> 
 </snapshots> 
 <id>central</id> 
 <name>Central Repository</name> 
 <url>https://repo.maven.apache.org/maven2</url> 
 </repository> 
 </repositories> 
 <pluginRepositories> 
 <pluginRepository> 
 <id>public</id>
 <name>Public Repositories</name> 
 <url>http://localhost:8081/nexus/content/groups/public/</url> 
 </pluginRepository> 
 <pluginRepository> 
 <releases> 
 <updatePolicy>never</updatePolicy> 
 </releases> 
 <snapshots> 
 <enabled>false</enabled> 
 </snapshots> 
 <id>central</id> 
 <name>Central Repository</name> 
 <url>https://repo.maven.apache.org/maven2</url> 
 </pluginRepository> 
 </pluginRepositories>

把第三方 jar 包放入本地仓库或私服

导入本地库

随便找一个 jar

包测试,可以先 CMD进入到 jar 包所在位置,运行

mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37-Dfile= fastjson-1.1.37 -Dpackaging=jar

导入私服

需要在 maven 软件的核心配置文件 settings.xml 中配置第三方仓库的 server 信息

 <server> 
<id>thirdparty</id> 
<username>admin</username> 
<password>admin123</password> 
</server>

才能执行一下命令

mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37

-Dpackaging=jar -Dfile=fastjson-1.1.37.jar

-Durl=http://localhost:8081/nexus/content/repositories/thirdparty/

-DrepositoryId=thirdparty

参数说明

DgroupId 和 DartifactId 构成了该 jar 包在 pom.xml 的坐标,项目就是依靠这两个属性定位。

自己起名字也行。

Dfile 表示需要上传的 jar 包的绝对路径。

Durl 私服上仓库的位置,打开 nexus——>repositories 菜单,可以看到该路径。

DrepositoryId 服务器的表示 id,在 nexus 的 configuration 可以看到。

Dversion 表示版本信息,

关于 jar 包准确的版本:

包的名字上一般会带版本号,如果没有那可以解压该包,会发现一个叫 MANIFEST.MF 的文件,

这个文件就有描述该包的版本信息。

比如 Specification-Version: 2.2 可以知道该包的版本了。

上传成功后,在 nexus 界面点击 3rd party 仓库可以看到这包。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值