(1)由于安全问题,有个项目需要在内网开发(无法连接外网),因此无法下载相关的 maven 依赖,导致项目无法编译启动。之前我介绍了通过在内网搭建 Maven 私服来解决这个问题:
使用Nexus搭建Maven私服教程(附:配置并使用私服教程)
或者
无网络搭建linux私服
(2)如果觉得搭建私服麻烦的话,还有更简单的方法,就是直接将在外网下载好的 maven 依赖拷贝到内网使用。下面通过样例进行演示。
1、Nexus服务启动以后,我们通过浏览器访问http://IP:端口,进去界面后点击右上角登录按钮:
2、首次登录后会提示密码保存在/usr/local/sonatype-work/nexus3/admin.password 文件中,我们查看服务器上这个文件内容,然后作为密码登录,然后修改密码
3、登录界面,说明如下
因为没有外面,所以只能在有网的情况下将工程所要的包都下载下来(一般外面直接配置maven,然后得到本地的仓库即可),然后传递到内网中。
当前将外网的本地仓库copy到nexus的服务器上,然后运行一下脚本即可批量上传jar包
#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
while getopts ":r:u:p:" opt; do
case $opt in
r) REPO_URL="$OPTARG"
;;
u) USERNAME="$OPTARG"
;;
p) PASSWORD="$OPTARG"
;;
esac
done
find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;
运行如下命令进行执行,但是该脚本需要放到repository文件夹中
#权限添加
chmod a+x mavenimport.sh
#执行
./mavenimport.sh -u admin -p admin123 -r http://ip:8081/repository/maven-releases/
使用git工具,在git Bash here中运行 ./mavenimport.sh -u admin -p admin123 -r http://ip:8081/repository/maven-releases/ 能达到同样的效果
当前只记录下,未验证通过
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
/**
* 上传依赖到 Maven 私服
* * @author liuzenghui
* @since
public class Deploy
/**
* mvn -s F:\.m2\settings.xml
* org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy-file
* -Durl=http://IP:PORT/nexus/content/repositories/thirdpart
* -DrepositoryId=thirdpart
* -Dfile=antlr-2.7.2.jar
* -DpomFile=antlr-2.7.2.pom
* -Dpackaging=jar
* -DgeneratePom=false
* -Dsources=./path/to/artifact-name-1.0-sources.jar
* -Djavadoc=./path/to/artifact-name-1.0-javadoc.jar
*/
public static final String BASE_CMD = "cmd /c mvn " +
"-s F:\\.m2\\settings.xml " +
"deploy:deploy-file " +
"-Durl=http://IP:PORT/nexus/content/repositories/thirdpart " +
"-DrepositoryId=thirdpart " +
"-DgeneratePom=false";
public static final Pattern DATE_PATTERN = Pattern.compile("-[\\d]{8}\\.[\\d]{6}-");
public static final Runtime CMD = Runtime.getRuntime();
public static final Writer ERROR;
public static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(10);
static {
Writer err = null;
try {
err = new OutputStreamWriter(new FileOutputStream("deploy-error.log"), "utf-8");
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
ERROR = err;
}
public static void main(String[] args) {
deploy(new File("F:\\.m2\\repository").listFiles());
// if(checkArgs(args)){
// File file = new File(args[0]);
// deploy(file.listFiles());
// }
EXECUTOR_SERVICE.shutdown();
try {
ERROR.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void error(String error){
try {
System.err.println(error);
ERROR.write(error + "\n");
ERROR.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean checkArgs(String[] args){
if (args.length != 1) {
System.out.println("用法如: java -jar Deploy D:\\some\\path\\");
return false;
}
File file = new File(args[0]);
if (!file.exists()) {
System.out.println(args[0] + " 目录不存在!");
return false;
}
if (!file.isDirectory()) {
System.out.println("必须指定为目录!");
return false;
}
return true;
}
public static void deploy(File[] files) {
if (files.length == 0) {
//ignore
} else if (files[0].isDirectory()) {
for (File file : files) {
if (file.isDirectory()) {
deploy(file.listFiles());
}
}
} else if (files[0].isFile()) {
File pom = null;
File jar = null;
File source = null;
File javadoc = null;
//忽略日期快照版本,如 xxx-mySql-2.2.6-20170714.095105-1.jar
for (File file : files) {
String name = file.getName();
if(DATE_PATTERN.matcher(name).find()){
//skip
} else if (name.endsWith(".pom")) {
pom = file;
} else if (name.endsWith("-javadoc.jar")) {
javadoc = file;
} else if (name.endsWith("-sources.jar")) {
source = file;
} else if (name.endsWith(".jar")) {
jar = file;
}
}
if(pom != null){
if(jar != null){
deploy(pom, jar, source, javadoc);
} else if(packingIsPom(pom)){
deployPom(pom);
}
}
}
}
public static boolean packingIsPom(File pom){
BufferedReader reader = null;
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(pom)));
String line;
while((line = reader.readLine()) != null){
if(line.trim().indexOf("pom ")!=-1){
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try{reader.close();}catch(Exception e){}
}
return false;
}
public static void deployPom(final File pom) {
EXECUTOR_SERVICE.execute(new Runnable() {
@Override
public void run() {
StringBuffer cmd = new StringBuffer(BASE_CMD);
cmd.append(" -DpomFile=").append(pom.getName());
cmd.append(" -Dfile=").append(pom.getName());
try {
final Process proc = CMD.exec(cmd.toString(), null, pom.getParentFile());
InputStream inputStream = proc.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
String line;
StringBuffer logBuffer = new StringBuffer();
logBuffer.append("\n\n\n==================================\n");
while((line = reader.readLine()) != null){
if (line.startsWith("[INFO]") || line.startsWith("Upload")) {
logBuffer.append(Thread.currentThread().getName() + " : " + line + "\n");
}
}
System.out.println(logBuffer);
int result = proc.waitFor();
if(result != 0){
error("上传失败:" + pom.getAbsolutePath());
}
} catch (IOException e) {
error("上传失败:" + pom.getAbsolutePath());
e.printStackTrace();
} catch (InterruptedException e) {
error("上传失败:" + pom.getAbsolutePath());
e.printStackTrace();
}
}
});
}
public static void deploy(final File pom, final File jar, final File source, final File javadoc) {
EXECUTOR_SERVICE.execute(new Runnable() {
@Override
public void run() {
StringBuffer cmd = new StringBuffer(BASE_CMD);
cmd.append(" -DpomFile=").append(pom.getName());
if(jar != null){
//当有bundle类型时,下面的配置可以保证上传的jar包后缀为.jar
cmd.append(" -Dpackaging=jar -Dfile=").append(jar.getName());
} else {
cmd.append(" -Dfile=").append(pom.getName());
}
if(source != null){
cmd.append(" -Dsources=").append(source.getName());
}
if(javadoc != null){
cmd.append(" -Djavadoc=").append(javadoc.getName());
}
try {
final Process proc = CMD.exec(cmd.toString(), null, pom.getParentFile());
InputStream inputStream = proc.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
String line;
StringBuffer logBuffer = new StringBuffer();
logBuffer.append("\n\n\n=======================================\n");
while((line = reader.readLine()) != null){
if (line.startsWith("[INFO]") || line.startsWith("Upload")) {
logBuffer.append(Thread.currentThread().getName() + " : " + line + "\n");
}
}
System.out.println(logBuffer);
int result = proc.waitFor();
if(result != 0){
error("上传失败:" + pom.getAbsolutePath());
}
} catch (IOException e) {
error("上传失败:" + pom.getAbsolutePath());
e.printStackTrace();
} catch (InterruptedException e) {
error("上传失败:" + pom.getAbsolutePath());
}
}
});
}
}
idea直接运行即可
使用有两个方式
当我们在 maven 使用 maven-public 仓库地址的时候,会按照如下顺序访问:本地仓库 --> 私服 maven-releases --> 私服 maven-snapshots --> 远程阿里云 maven 仓库 --> 远程中央仓库。
配置文件吐下,配置后不需要再配置pom.xml文件,即可通过私服下载依赖包
<mirrors>
<mirror>
<id>maven-publicid>
<name>maven-publicname>
<mirrorOf>*mirrorOf>
<url>http://192.168.60.133:8081/repository/maven-public/url>
mirror>
mirrors>
阿里云maven仓库
<mirror>
<id>nexus-aliyunid>
<name>Nexus aliyunname>
<mirrorOf>*mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/publicurl>
mirror>
pom.xml 文件配置样例如下。如果我们配置了 pom.xml,则以 pom.xml 为准。
<repositories>
<repository>
<id>maven-nexusid>
<name>maven-nexusname>
<url>http://192.168.60.133:8081/repository/maven-public/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
repositories>
如果没有私服,我们同样也可以配置阿里云 maven 仓库:
<repositories>
<repository>
<id>maven-aliyunid>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
<updatePolicy>alwaysupdatePolicy>
<checksumPolicy>failchecksumPolicy>
snapshots>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-nexusid>
<name>maven-nexusname>
<url>http://10.172.0.201:8081/nexus/repository/maven-public/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
pluginRepository>
pluginRepositories>
1、首先修改 setting.xml 文件,指定 releases 和 snapshots server 的用户名和密码:
<servers>
<server>
<id>releasesid>
<username>adminusername>
<password>123password>
server>
<server>
<id>snapshotsid>
<username>adminusername>
<password>123password>
server>
servers>
2、接着在项目的 pom.xml 文件中加入 distributionManagement 节点:
注意:repository 里的 id 需要和上一步里的 server id 名称保持一致。
<distributionManagement>
<repository>
<id>releasesid>
<name>Releasesname>
<url>http://192.168.60.133:8081/repository/maven-releases/url>
repository>
<snapshotRepository>
<id>snapshotsid>
<name>Snapshotname>
<url>http://192.168.60.133:8081/repository/maven-snapshots/url>
snapshotRepository>
distributionManagement>
资料
[1]: https://www.hangge.com/blog/cache/detail_2844.html
[2]: https://blog.csdn.net/m0_44968817/article/details/127306177
[3]: https://blog.csdn.net/AI_STUDENT_QYB/article/details/121178262
[4]: https://blog.51cto.com/isea533/5594504