import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
/**
* 上传依赖到 Maven 私服
* 遍历本地仓库下所有jar包和pom.xml,将它们上传到maven私服
* @author wzq
* @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 String BASE_CMD = "";
public static final Pattern DATE_PATTERN = Pattern.compile("-[\\d]{8}\\.[\\d]{6}-");
public static final Runtime CMD = Runtime.getRuntime();
public static final Writer ERROR;
//1:上传release,2:上传snapshot maven的setting.xml也要修改activeProfiles版本为release或snapshot,没有办法同时上传两个不同的版本,需要各上传一次
public static String model = "2";
//使用指定xml配置进行,可以不使用,使用默认的
private static String xmlLocation = "E:\\wzq\\software\\apache-maven-3.5.3\\conf\\settings.xml";
//私服地址分为发布版本和快照版本(快照版本是高频更新版本,maven每次构建都会去检查快照版本)
private static String releaseUrl = "http://127.0.0.1:28081/repository/nexus-release/";
private static String snapshotUrl = "http://127.0.0.1:28081/repository/nexus-snapshot/";
//仓库id 和setting.xml中server保持一致,会自动寻找指定的账户密码,maven私服中负责上传的账户和密码和默认管理员admin不一样,需要重新创建,否则无法上传
private static String releaseRepositoryId = "nexus-release";
private static String snapshotRepositoryId = "nexus-snapshot";
//是否默认创建pom.xml文件,从本地上传不需要自动创建pom.xml,有些pom.xml引入了一些依赖,自动创建会丢掉这部分依赖,所以不默认创建
private static String generatePom ="false";
//本地上传仓库地址,需要和setting.xml中本地仓库地址不一致,否则上传会报错,将本地仓库复制重命名即可
private static String locationRepository ="D:\\repository2";
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) {
if("1".equals(model)){
BASE_CMD = "cmd /c mvn " +
"-s "+xmlLocation+" " +
"deploy:deploy-file " +
"-Durl="+releaseUrl+" " +
"-DrepositoryId="+releaseRepositoryId+" " +
"-DgeneratePom="+generatePom+"";
}else if("2".equals(model)){
BASE_CMD = "cmd /c mvn " +
"-s "+xmlLocation+" " +
"deploy:deploy-file " +
"-Durl="+snapshotUrl+" " +
"-DrepositoryId="+snapshotRepositoryId+" " +
"-DgeneratePom="+generatePom+"";
}
deploy(new File(locationRepository).listFiles(),model);
// 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 void deploy(File[] files,String model) {
if (files.length == 0) {
//ignore
} else if (files[0].isDirectory()) {
for (File file : files) {
if (file.isDirectory()) {
deploy(file.listFiles(),model);
}
}
} else if (files[0].isFile()) {
File pom = null;
File jar = null;
File source = null;
File javadoc = null;
for (File file : files) {
String name = file.getName();
//1.跳过快照,2.跳过处快照外其他
if("1".equals(model)){
if(name.contains("SNAPSHOT")){
continue;
}
}else if("2".equals(model)){
if(!name.contains("SNAPSHOT")){
continue;
}
}
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 {
reader =
new BufferedReader(new InputStreamReader(new FileInputStream(pom)));
String line;
while((line = reader.readLine()) != null){
if(line.trim().indexOf("<packaging>pom</packaging>")!=-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.getAbsolutePath());
cmd.append(" -Dfile=").append(pom.getAbsolutePath());
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){
评论0