Docker安装教程
docker run -d -p 8081:8081 -p 8082:8082 -p 8088:8088 -p 8090:8090 --name nexus sonatype/nexus3
firewall-cmd --zone=public --add-port=8081/tcp --permanent #添加放行端口
firewall-cmd --reload #重新加载配置
docker exec -it nexus /bin/bash #登录到nexus
vi nexus-data/admin.password #查看密码
复制仓库的地址,在项目中进行引用。
新建一个项目测试项目对仓库的地址进行测试
Gradle项目:
新建一个Gradle项目,使用刚刚安装的Nexus地址。将默认的本地仓库注释掉,然后添加私有的仓库地址,添加一个测试的依赖 compile(‘net.sf.oval:oval:1.87’),刷新项目去下载依赖
私有仓库已经去下载了Jar。
在项目中添加一个简单的ip 工具类,用来测试打包上传到本地仓库。
在gradle文件里面配置需要上传Jar的信息
build.gradle文件
//项目插件
plugins {
id 'java'
id 'maven'
}
//项目信息
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'
//Jar仓库
repositories {
// mavenLocal() //maven本地仓库
maven {
url "http://192.168.0.116:8081/repository/maven-public/"
credentials { //如果项目仓库设置了密码。可以在这添加配置
username 'admin'
password '123456'
}
}
}
//项目依赖
dependencies {
compile('org.apache.tomcat.embed:tomcat-embed-core:9.0.36')
testCompile group: 'junit', name: 'junit', version: '4.12'
}
configurations {
deployerJars
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
options.addStringOption('charSet', 'UTF-8')
}
artifacts {
archives sourcesJar
archives javadocJar
}
//需要上传的地址
uploadArchives {
repositories {
mavenDeployer {
//releases仓库的地址
repository(url: "http://192.168.0.116:8081/repository/maven-releases/") {
authentication(userName: "admin", password: "123456")
}
//snapshots仓库地址
snapshotRepository(url: "http://192.168.0.116:8081/repository/maven-snapshots/") {
authentication(userName: "admin", password: "123456")
}
}
}
}
IPUtil文件
package org.example.util;
import javax.servlet.http.HttpServletRequest;
/**
* ip 工具类
*/
public class IPUtil {
/**
* ip 转 long
*
* @param ipAddress
* @return
*/
public static long ipToLong(String ipAddress) {
try {
long[] ip = new long[4];
//先找到IP地址字符串中.的位置
int position1 = ipAddress.indexOf(".");
int position2 = ipAddress.indexOf(".", position1 + 1);
int position3 = ipAddress.indexOf(".", position2 + 1);
//将每个.之间的字符串转换成整型
ip[0] = Long.parseLong(ipAddress.substring(0, position1));
ip[1] = Long.parseLong(ipAddress.substring(position1 + 1, position2));
ip[2] = Long.parseLong(ipAddress.substring(position2 + 1, position3));
ip[3] = Long.parseLong(ipAddress.substring(position3 + 1));
return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
} catch (Exception e) {
e.printStackTrace();
return 0l;
}
}
/**
* long 转 ip
*
* @param ipAddress
* @return
*/
public static String longToIp(long ipAddress) {
try {
StringBuffer sb = new StringBuffer("");
//直接右移24位
sb.append(String.valueOf((ipAddress >>> 24)));
sb.append(".");
//将高8位置0,然后右移16位
sb.append(String.valueOf((ipAddress & 0x00FFFFFF) >>> 16));
sb.append(".");
//将高16位置0,然后右移8位
sb.append(String.valueOf((ipAddress & 0x0000FFFF) >>> 8));
sb.append(".");
//将高24位置0
sb.append(String.valueOf((ipAddress & 0x000000FF)));
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 获取请求的ip
*
* @param request
* @return
*/
public static String getIPAddress(HttpServletRequest request) {
String ip = null;
//X-Forwarded-For:Squid 服务代理
String ipAddresses = request.getHeader("X-Forwarded-For");
if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
//Proxy-Client-IP:apache 服务代理
ipAddresses = request.getHeader("Proxy-Client-IP");
}
if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
//WL-Proxy-Client-IP:weblogic 服务代理
ipAddresses = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
//HTTP_CLIENT_IP:有些代理服务器
ipAddresses = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
//X-Real-IP:nginx服务代理
ipAddresses = request.getHeader("X-Real-IP");
}
//有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
if (ipAddresses != null && ipAddresses.length() != 0) {
ip = ipAddresses.split(",")[0];
}
//还是不能获取到,最后再通过request.getRemoteAddr();获取
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
将项目打包上传,可以在终端使用命令上传。
gradle uploadArchives
因为Jar版本是1.0-SNAPSHOT 所以会上传到本地仓库的maven-snapshots下面。
将Jar版本修改成releases版本
build.gradle文件
plugins {
id 'java'
id 'maven'
}
group 'org.example'
version '1.0-RELEASES'
sourceCompatibility = 1.8
[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'
repositories {
// mavenLocal() //maven本地仓库
maven {
url "http://192.168.0.116:8081/repository/maven-public/"
credentials { //如果项目仓库设置了密码。可以在这添加配置
username 'admin'
password '123456'
}
}
}
dependencies {
compile('org.apache.tomcat.embed:tomcat-embed-core:9.0.36')
testCompile group: 'junit', name: 'junit', version: '4.12'
}
configurations {
deployerJars
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
options.addStringOption('charSet', 'UTF-8')
}
artifacts {
archives sourcesJar
archives javadocJar
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "http://192.168.0.116:8081/repository/maven-releases/") {
authentication(userName: "admin", password: "123456")
}
snapshotRepository(url: "http://192.168.0.116:8081/repository/maven-snapshots/") {
authentication(userName: "admin", password: "123456")
}
}
}
}
使用命令上传Jar包,这时候Jar就就会发布到maven-releases仓库