上传到私服
第一步: 需要在客户端即部署 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 包放入本地仓库或私服
1. 导入本地库
随便找一个 jar 包测试,可以先 CMD进入到 jar包所在位置,运行
mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37
-Dfile= fastjson-1.1.37.jar -Dpackaging=jar
2. 导入私服
需要在 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
3. 参数说明
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 仓库可以看到这包。
下载私服jar包
5.3 在 在 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>
配置成功后通过IDEA查看有效 pom,有效 pom 是 maven 软件最终使用的 pom 内容,程
序员不直接编辑有效 pom,打开有效 pom
<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>