1. 将 Jar 包安装到 maven:
# win 下使用 ^ 进行换行
mvn install:install-file -Dfile=iot-tcp.jar ^
-DgroupId=com.ccc ^
-DartifactId=iot-jar ^
-Dversion=1.0.0-SNAPSHOT ^
-Dpackaging=jar
- 调用了 maven 的插件:install,执行的 goal:install-file
- 后面的参数 -D 有三类(5 种):
- gav(groupId、artifactId、version)
- packaging
- file
2. maven 私服部署:
a. 准备工作(setting.xml 文件设置)
- 在 maven 的安装目录下,配置 conf/settings.xml 文件。在 servers 标签下加入我们的server地址,id 自定义一个(后面需引用 id)
- 账号和密码为:http://${maven部署地址}/nexus/下的登录密码,maven 安装后默认为 admin/admin123
<servers>
<server>
<!-- distributionManagement里repository的id必须等于这里定义的id -->
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<!-- distributionManagement里snapshotRepository的id必须等于这里定义的id -->
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
b. Jar 包直接部署:
mvn deploy:deploy-file -DgroupId=com.ccc -DartifactId=iot-tcp -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -Dfile=iot-tcp.jar -Durl=http://ip:port/nexus/content/repositories/snapshots -DrepositoryId=snapshots
- repositoryId 与 自定义的 id 一致
- 与本地安装(install)相比,deploy 增加了两个参数
- url // 部署地址,类似于:
/nexus/content/repositories/{snapshots}
,snapshots 可替换为 releases 或其他。可以登录我们的 maven 部署地址查看 Repository Path - repositoryId // 引用的仓库 id,在 setting 里面,包含了用户名和密码
- url // 部署地址,类似于:
- 其他参数:
- gav(groupId、artifactId、version)
- packaging
- file
c. pom 文件部署
- 在我们需要部署的项目 pom 文件上第二层级里加上:distributionManagement
<project>
......
</dependencies>
<distributionManagement>
<!-- 如果版本号不带 -SNAPSHOT 就默认使用正式仓库 -->
<repository>
<!-- 等于server的id -->
<id>releases</id>
<name>deploy-releases</name>
<url>http://${maven部署地址}/nexus/content/repositories/releases</url>
</repository>
<!-- 版本号带 -SNAPSHOT 使用快照仓库 -->
<snapshotRepository>
<!-- 等于server的id -->
<id>snapshots</id>
<name>deploy-snapshots</name>
<url>http://${maven部署地址}/nexus/content/repositories/snaps</url>
</snapshotRepository>
<relocation>
<!-- 上传时重命名新的group ID -->
<groupId />
<!-- 上传时重命名新的artifact ID -->
<artifactId />
<!-- 上传时重命名新的版本号 -->
<version />
<!-- 显示给用户关于移动的额外信息,例如原因。 -->
<message />
</relocation>
</distributionManagement>
<build>
......
</project>
- 在项目路径下使用命令:
mvn deploy -Dmaven.test.skip=true
- 原理和 jar 包部署一样,通过 url,然后使用 repositoryId 来找到仓库用户名和密码,然后将项目部署上去。
- 仅需三个参数
- name
- url
- repository.id
- pom 文件部署,不需要 file(jar包地址)、gav 坐标 和 package,这些在 pom 文件里都有。
d. 版本后缀 -SNAPSHOT
和 -RELEASE
的区别
-
Maven 会根据模块的版本号( pom 的 version 标签)是否带有
-SNAPSHOT
来判断是快照版本还是正式版本。 -
SNAPSHOT 在编译打包时,Maven 会自动从服务器上下载最新的快照版本。
-
而 RELEASE 正式版在不更改版本号的情况下,编译时如果本地已经存在该版本的模块则不会主动去服务器上下载。
-
正式版 RELEASE 不能传到 SNAPSHOT 仓库。
-
快照版 SNAPSHOT 不能传到 RELEASE 仓库。
-
-SNAPSHOT
必须全大写。
3. 项目引用私有仓库
- 同 pom 文件部署,url 也是一致的。
<project>
......
</dependencies>
<!-- 引用私有仓库 -->
<repositories>
<repository>
<id>nexus-snapshots</id>
<url>http://${maven部署地址}/nexus/content/repositories/snaps</url>
</repository>
<repository>
<id>nexus-repository</id>
<url>http://${maven部署地址}/nexus/content/repositories/releases</url>
</repository>
</repositories>
<!-- 引用私有插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>nexus-snapshots</id>
<url>http://${maven部署地址}/nexus/content/repositories/snaps</url>
</pluginRepository>
<pluginRepository>
<id>nexus-repository</id>
<url>http://${maven部署地址}/nexus/content/repositories/releases</url>
</pluginRepository>
</pluginRepositories>
<build>
......
</project>