系列文章目录

在Linux下搭建自己的私有maven库并部署和发布自定义jar依赖和自定义maven插件(一)搭建SonatypeNexus私有库



前言

上一节中我们分享了Sonatype Nexus私有库的搭建过程,今天给大家分享,如果将我们自己开发的jar文件部署到自己的私有库中。


一、部署已有的jar文件

在有些情况下,我们在做对接开发时,可能对方公司只会给我们发来一个jar文件,我们可以把jar文件安装到自己电脑的本地库里,但是当别的同事要使用时,又需要发给别的同事。特别是在多人开发的项目中,这种操作会很影响项目的开发进度,如果我们有自己的私有库,我们只需要把现有的jar文件部署到私有库中即可,最常用的有2中方式,
下面分解具体的操作方式。

在Linux下搭建自己的私有maven库并部署和发布自定义jar依赖和自定义maven插件(二)发布自己开发的jar包-LMLPHP
填写相关信息即可

这种方式不需要登录到后台,直接在本地使用mvn命令即可,对程序员来说这种操作更方便快捷,具体步骤如下:

<server>
  <id>user-release-nexus</id>
  <username>admin</username>
  <password>123456</password>
</server>
mvn deploy:deploy-file -DgroupId=com.test -DartifactId=csdn -Dversion=1.0.0 -Dpackaging=jar -Dfile=core-1.0.0.jar -Durl=http://192.168.101.170:8081/repository/maven-releases/ -DrepositoryId=user-release-nexus

执行命令完成后,我们登录Nexus验证一下是否上传成功。
在Linux下搭建自己的私有maven库并部署和发布自定义jar依赖和自定义maven插件(二)发布自己开发的jar包-LMLPHP
说明我们已经上传成功了,这样在其他项目里面我们就可以加入下面配置来使用这个jar文件了:

<dependency>
    <groupId>com.test</groupId>
    <artifactId>csdn</artifactId>
    <version>1.0.0</version>
</dependency>

二、部署自己的项目代码

首先我们准备一个普通的maven项目,pom.xml文件如下所示:

<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.test</groupId>
	<artifactId>test-nexus-jar</artifactId>
	<version>1.0.134</version>
	<packaging>jar</packaging>

	<name>test-nexus-jar</name>
	<description>test nexus jar</description>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>
	</dependencies>

	<distributionManagement>
		<repository>
			<id>user-release-nexus</id>
			<name>User Project Release</name>
			<url>http://192.168.101.170:8081/repository/maven-releases/</url>
		</repository>
	</distributionManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<version>2.9.1</version>
				<executions>
					<execution>
						<id>attach-javadocs</id>
						<goals>
							<goal>jar</goal>
						</goals>
						<configuration>
							<additionalparam>-Xdoclint:none</additionalparam>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-source-plugin</artifactId>
				<version>3.0.1</version>
				<executions>
					<execution>
						<id>attach-sources</id>
						<phase>verify</phase>
						<goals>
							<goal>jar-no-fork</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

其中distributionManagement标签定义了我们要上传的仓库地址,下面我们随意定义一个工具类,并定义几个方法,大体如下:

package com.test.csdn;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProviderUtils {
    public static final char UNDERLINE='_';
    public static String underlineToCamel(String param){
        if (StringUtils.isEmpty(param)){
            return "";
        }
        StringBuilder sb=new StringBuilder(param);
        Matcher mc= Pattern.compile("_").matcher(param);
        int i=0;
        while (mc.find()){
            int position=mc.end()-(i++);
            sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase());
        }
        return sb.toString();
    }
    public static String camelToUnderline(String param){
        if (StringUtils.isEmpty(param)){
            return "";
        }
        int len=param.length();
        StringBuilder sb=new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c=param.charAt(i);
            if (Character.isUpperCase(c)){
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

现在,我们来打包并部署:

mvn clean -DskipTests deploy

出现如下结果:

[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ test-nexus-jar ---
Uploading to user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/1.0.134/test-nexus-jar-1.0.134.jar
Uploaded to user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/1.0.134/test-nexus-jar-1.0.134.jar (3.3 kB at 2.7 kB/s)
Uploading to user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/1.0.134/test-nexus-jar-1.0.134.pom
Uploaded to user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/1.0.134/test-nexus-jar-1.0.134.pom (2.1 kB at 12 kB/s)
Downloading from user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/maven-metadata.xml
Uploading to user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/maven-metadata.xml
Uploaded to user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/maven-metadata.xml (306 B at 2.2 kB/s)
Uploading to user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/1.0.134/test-nexus-jar-1.0.134-javadoc.jar
Uploaded to user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/1.0.134/test-nexus-jar-1.0.134-javadoc.jar (25 kB at 174 kB/s)
Uploading to user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/1.0.134/test-nexus-jar-1.0.134-sources.jar
Uploaded to user-release-nexus: http://192.168.101.170:8081/repository/maven-releases/com/test/test-nexus-jar/1.0.134/test-nexus-jar-1.0.134-sources.jar (2.9 kB at 22 kB/s)

说明我们已经部署成功了,到nexus后台检查:
在Linux下搭建自己的私有maven库并部署和发布自定义jar依赖和自定义maven插件(二)发布自己开发的jar包-LMLPHP

发现我们刚才的项目也发布成功了,而且这里还生成了javadoc文件,还有源码sources文件也一并上传了。这样如果其他项目要使用我们的jar,在dependency标签里直接引用我们的jar包即可。


总结

<snapshotRepository>
	<id>user-release-nexus</id>
	<name>User Project SNAPSHOTS</name>
	<url>http://192.168.101.170:8081/repository/maven-snapshots/</url>
</snapshotRepository>
02-05 18:02