内容简介:实战maven私有仓库三部曲之二:上传到私有仓库
在上一章 《实战maven私有仓库三部曲之一:搭建和使用》 我们搭建了maven私有仓库,并体验了私有仓库缓存jar包的能力,避免了局域网内开发人员去远程中央仓库下载的痛苦等待,本章我们再来体验私有仓库的另一个功能:保存二方库;
原文地址: http://blog.csdn.net/boling_cavalry/article/details/79070744
使用场景
- mvndemoserver是个web应用,提供http接口getuserinfo,返回用户信息;
- mvnDemoClient是个 java 应用,调用mvndemoserver的getuserinfo接口查用户信息;
- 用户信息对应的java类:UserInfo.java,代码如下:
public class UserInfo { /** * 用户名 */ private String name; /** * 用户年龄 */ private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
- mvndemoserver的getuserinfo接口,返回的数据是将UserInfo对象序列化成的json对象;
- mvnDemoClient收到mvndemoserver返回的json字符串后,反序列化成UserInfo对象;
UserInfo.java放在哪里合适?
mvnDemoClient和mvndemoserver两个应用都要用UserInfo类,所以我们创建一个maven工程:mvnDemoLib,此工程中将UserInfo类做成jar包,放到私有maven仓库(这就是二方库了),这样mvnDemoClient和mvndemoserver在各自的pom.xml中依赖mvnDemoLib就都能使用UserInfo类了;
实战步骤梳理
本次实战先在maven私有仓库发布jar包,再在应用中使用jar包,所有的步骤列举如下:
1. 修改私有仓库的配置信息,允许上传jar包;
2. 修改当前电脑的maven配置文件apache-maven-3.3.3\conf\settings.xml;
3. 创建mvnDemoLib工程,里面有UserInfo.java,执行mvn命令打包并上传到maven私有仓库;
4. 创建mvndemoserver工程,pom.xml中添加mvnDemoLib的依赖,开发http服务的controller;
5. 创建mvnDemoClient工程,pom.xml中添加mvnDemoLib的依赖,发起http请求,将得到的响应转成UserInfo对象;
接下来实战每个步骤:
实战源码下载
本次实战创建的三个maven工程,源码的git地址是:git@github.com:zq2599/blog_demos.git,用浏览器访问的地址是: https://github.com/zq2599/blog_demos ,这里面有多个工程,本次用到的工程如下图红框所示:
修改私有仓库的配置信息,允许上传jar包
- 登录nexus,我搭建私有仓库的机器IP是192.168.119.155,所以访问地址是 http://192.168.119.155:8081/nexus ,然后点击右上角的”Log In”登录;
- 设置好deploy用户的密码,操作如下图所示:
我这里设置的密码是admin123; - 设置为允许上传release的jar包,操作如下图:
- 设置为允许上传snapshots的jar包,操作如下图:
修改当前电脑的maven配置文件
打开当前电脑的maven配置文件apache-maven-3.3.3\conf\settings.xml,添加如下信息:
1. 找到servers节点,添加如下两个子节点:<server> <id>bolingcavalry-nexus-releases</id> <username>deployment</username> <password>admin123</password> </server> <server> <id>bolingcavalry-nexus-snapshots</id> <username>deployment</username> <password>admin123</password> </server>
以上配置了两个server的用户名和密码信息 ,接下来需要身份验证的时候,都可以通过bolingcavalry-nexus-releases和bolingcavalry-nexus-snapshots这两个id来使用对应的用户名和密码;
2. 找到mirrors节点,添加如下两个子节点:<mirror> <id>bolingcavalry-nexus-releases</id> <mirrorOf>*</mirrorOf> <url>http://192.168.119.155:8081/nexus/content/groups/public</url> </mirror> <mirror> <id>bolingcavalry-nexus-snapshots</id> <mirrorOf>*</mirrorOf> <url>http://192.168.119.155:8081/nexus/content/groups/public-snapshots</url> </mirror>
以上配置了不同的仓库地址;
3. 找到profile节点下面的repositories节点,添加如下两个子节点:
<repository> <id>bolingcavalry-nexus-releases</id> <url>http://nexus-releases</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>bolingcavalry-nexus-snapshots</id> <url>http://nexus-snapshots</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository>
以上配置release和snapshots的部署时,使用哪个仓库和server的配置信息;
4. 找到profile节点下面的pluginRepositories节点,添加如下两个子节点:
<pluginRepository> <id>bolingcavalry-nexus-releases</id> <url>http://nexus-releases</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> <pluginRepository> <id>bolingcavalry-nexus-snapshots</id> <url>http://nexus-snapshots</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository>
以上配置release和snapshots的部署时的插件仓库配置;
至此,本地maven配置就完成了,接下来我们开始创建三个maven工程;
创建mvnDemoLib工程
- 创建一个maven工程mvnDemoLib,pom.xml内容如下:
<?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.bolingcavalry</groupId> <artifactId>mvndemolib</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <distributionManagement> <repository> <id>bolingcavalry-nexus-releases</id> <name>Nexus Release Repository</name> <url>http://192.168.119.155:8081/nexus/content/repositories/releases</url> </repository> <snapshotRepository> <id>bolingcavalry-nexus-snapshots</id> <name>Nexus Snapshot Repository</name> <url>http://192.168.119.155:8081/nexus/content/repositories/snapshots</url> </snapshotRepository> </distributionManagement> </project>
如上所示,mvnDemoLib是个普通的java工程,repository和snapshotRepository定义了deploy的时候用到的服务器信息,id和我们前面在maven配置信息中增加的id一致;
2. 工程的源码只有一个文件:UserInfo.java;
3. 在pom.xml所在文件夹下执行命令:
mvn clean deploy -U
看到类似如下的输出信息:
..... http://192.168.119.155:8081/nexus/content/repositories/snapshots/com/bolingcavalry/mvndemolib/1.0-SNAPSHOT/mvndemolib-1.0-20180119.044938-1.jar Uploaded: http://192.168.119.155:8081/nexus/content/repositories/snapshots/com/bolingcavalry/mvndemolib/1.0-SNAPSHOT/mvndemolib-1.0-20180119.044938-1.jar (3 KB at 30.4 KB/sec) Uploading: http://192.168.119.155:8081/nexus/content/repositories/snapshots/com/bolingcavalry/mvndemolib/1.0-SNAPSHOT/mvndemolib-1.0-20180119.044938-1.pom Uploaded: http://192.168.119.155:8081/nexus/content/repositories/snapshots/com/bolingcavalry/mvndemolib/1.0-SNAPSHOT/mvndemolib-1.0-20180119.044938-1.pom (1000 B at 17.4 KB/sec) Downloading: http://192.168.119.155:8081/nexus/content/repositories/snapshots/com/bolingcavalry/mvndemolib/maven-metadata.xml Uploading: http://192.168.119.155:8081/nexus/content/repositories/snapshots/com/bolingcavalry/mvndemolib/1.0-SNAPSHOT/maven-metadata.xml Uploaded: http://192.168.119.155:8081/nexus/content/repositories/snapshots/com/bolingcavalry/mvndemolib/1.0-SNAPSHOT/maven-metadata.xml (771 B at 17.1 KB/sec) Uploading: http://192.168.119.155:8081/nexus/content/repositories/snapshots/com/bolingcavalry/mvndemolib/maven-metadata.xml Uploaded: http://192.168.119.155:8081/nexus/content/repositories/snapshots/com/bolingcavalry/mvndemolib/maven-metadata.xml (285 B at 9.6 KB/sec) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 28.919 s [INFO] Finished at: 2018-01-19T12:49:38+08:00 [INFO] Final Memory: 18M/211M [INFO] ------------------------------------------------------------------------
如果提示失败,请看具体的错误码,我之前由于在settings.xml中将密码填写错误,导致此处返回的错误码为401;
4. 在nexus的网页上已经可以上传成功的jar包了,如下图:
至此,我们上传二方库到maven私有仓库的操作已经完成,使用私有仓库的开发和构建环境都可以使用这个二方库了,接下来试试如何在工程中使用此二方库;
创建mvndemoserver工程
- 创建一个springboot的web工程mvndemoserver,里面有个controller,提供一个http接口服务,pom.xml中添加二方库的依赖,如下:
<dependency> <groupId>com.bolingcavalry</groupId> <artifactId>mvndemolib</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
- 创建一个controller,提供http服务,会用到UserInfo类,代码如下:
@RequestMapping(value = "/getuserinfo/{name}", method = RequestMethod.GET) @ResponseBody public UserInfo getuserinfo(@PathVariable("name") final String name) { UserInfo userInfo = new UserInfo(); userInfo.setName(name); userInfo.setAge(name.length()); return userInfo; }
服务启动后,在浏览器输入地址: http://localhost:8080/getuserinfo/tom ,可以看到以下json返回:
创建mvnDemoClient工程
- 我们再创建mvnDemoClient工程,访问 http://localhost:8080/getuserinfo/tom ,将取得的json转成UserInfo对象;
- mvnDemoClinet工程的pom.xml如下,除了mvndemolib,还依赖了httpclient和fastjson:
<?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.bolingcavalry</groupId> <artifactId>mvnDemoClient</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.bolingcavalry</groupId> <artifactId>mvndemolib</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.23</version> </dependency> </dependencies> </project>
- 发起http请求,将字符串转成UserInfo对象的代码如下:
public static void main(String[] args) throws Exception{ CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet("http://localhost:8080/getuserinfo/jerry"); //response HttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); //收到原始的响应 String rawStr = EntityUtils.toString(entity,"UTF-8"); //打印原始的字符串 System.out.println("raw string : " + rawStr); //将字符串转成UserInfo对象 UserInfo userInfo = JSONObject.parseObject(rawStr, UserInfo.class); System.out.println("userName : " + userInfo.getName() + ", userAge : " + userInfo.getAge()); }
- 直接运行这个类,得到的结果如下:
raw string : {"name":"jerry","age":5} userName : jerry, userAge : 5
以上就是将二方库工程上传到maven私有仓库、并在其他工程中使用此二方库的全部过程,希望能对您的开发有所帮助,接下来的实战,我们将在 Docker 下简单快捷的完成maven仓库的搭建;
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 实战maven私有仓库三部曲之三:Docker下搭建maven私有仓库
- docker私有仓库搭建
- 企业私有Harbor仓库
- Docker 私有仓库搭建
- Docker私有仓库搭建
- rust的私有代码仓库
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Squid: The Definitive Guide
Duane Wessels / O'Reilly Media / 2004 / $44.95 US, $65.95 CA, £31.95 UK
Squid is the most popular Web caching software in use today, and it works on a variety of platforms including Linux, FreeBSD, and Windows. Squid improves network performance by reducing the amount of......一起来看看 《Squid: The Definitive Guide》 这本书的介绍吧!