在上一章《实战maven私有仓库三部曲之一:搭建和使用》我们搭建了maven私有仓库,并体验了私有仓库缓存jar包的能力,避免了局域网内开发人员去远程中央仓库下载的痛苦等待,本章我们再来体验私有仓库的另一个功能:保存二方库;
原文地址:http://blog.csdn.net/boling_cavalry/article/details/79070744
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;
}
}
mvnDemoClient和mvndemoserver两个应用都要用UserInfo类,所以我们创建一个maven工程:mvnDemoLib,此工程中将UserInfo类做成jar包,放到私有maven仓库(这就是二方库了),这样mvnDemoClient和mvndemoserver在各自的pom.xml中依赖mvnDemoLib就都能使用UserInfo类了;
本次实战先在maven私有仓库发布jar包,再在应用中使用jar包,所有的步骤列举如下:
接下来实战每个步骤:
本次实战创建的三个maven工程,源码的git地址是:git@github.com:zq2599/blog_demos.git,用浏览器访问的地址是:https://github.com/zq2599/blog_demos,这里面有多个工程,本次用到的工程如下图红框所示:
我这里设置的密码是admin123;
打开当前电脑的maven配置文件apache-maven-3.3.3\conf\settings.xml,添加如下信息:
<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工程;
<?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私有仓库的操作已经完成,使用私有仓库的开发和构建环境都可以使用这个二方库了,接下来试试如何在工程中使用此二方库;
<dependency>
<groupId>com.bolingcavalry</groupId>
<artifactId>mvndemolib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
@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返回:
<?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>
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仓库的搭建;