声明:本文参考
https://blog.csdn.net/isea533/article/details/77197017
https://www.cnblogs.com/fengpingfan/p/5197608.html
https://www.cnblogs.com/yucy/p/7509561.html
【适用场景】:下载jar包、上传单个jar包、批量上传jar包到内网Maven 私服、发布自己的jar包到私服
【适用人群】:IT 懒人
下载单个jar包:http://mvnrepository.com/ 搜索下载 pom、jar、source、javadoc
下载多个jar包:配置一个新的本地仓库路径,在 settings.xml 中增加以下的配置(为了可以下载source和javadoc)
<server>
<id>thirdpartid>
<username>adminusername>
<password>admin123password>
server>
<profiles>
<profile>
<id>downloadSourcesid>
<properties>
<downloadSources>truedownloadSources>
<downloadJavadocs>truedownloadJavadocs>
properties>
profile>
profiles>
<activeProfiles>
<activeProfile>downloadSourcesactiveProfile>
activeProfiles>
mvn deploy:deploy-file
-Durl=file:///home/me/m2-repo
-DrepositoryId=some.repo.id
-Dfile=./path/to/artifact-name-1.0.jar
-DpomFile=./path/to/pom.xml
-Dsources=./path/to/artifact-name-1.0-sources.jar
-Djavadoc=./path/to/artifact-name-1.0-javadoc.jar
当执行命令有报错时,可以在命令的结尾加上"-X"进行调试
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
/**
* 上传依赖到 Maven 私服
*
* @author liuzenghui
* @since 2017/7/31.
*/
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());
e.printStackTrace();
}
}
});
}
}
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.135 s
[INFO] Finished at: 2016-02-18T10:23:58+08:00
[INFO] Final Memory: 19M/174M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project xeger:
Failed to deploy artifacts: Could not transfer artifact nl.flotsam:xeger:jar:1.0.2 from/to nexus-releases
(http://localhost:8081/nexus/content/repositories/releases/):
Failed to transfer file: http://localhost:8081/nexus/content/repositories/releases/nl/flotsam/xeger/1.0.2/xeger-1.0.2.jar.
Return code is: 400, ReasonPhrase: Bad Request. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
【问题原因1】部署仓库错误: Nexus 私服有三种仓库类型:Hosted、Proxy和Virtual,另外还有一个 group (仓库组)用于对多个仓库进行组合。部署的时候只能部署到 Hosted 类型的宿主仓库中,如果是其他类型就会出现这个 400 错误。
【解决方案1】修改 POM 文件中的部署仓库到对应的宿主仓库
【问题原因2】宿主仓库不允许重复部署:默认情况下重复部署构件到 Releases 仓库中也会出现 400 错误,原因是 Nexus 私服中 Releases 仓库默认的 Deployment Policy 是 “Disable Redeploy”,所以当你重复部署构件至 Releases 宿主仓库时就会出现这个 400 错误。
【解决方案2】只需要将宿主仓库的 Deployment Policy 改为 “Allow Redeploy” 即可解决
【问题原因3】配置错误:有些DrepositoryID和DrepositoryName不一致,注意要配置DrepositoryID
【解决方案3】修改配置信息
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.215 s
[INFO] Finished at: 2016-02-18T10:50:56+08:00
[INFO] Final Memory: 19M/175M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project xeger:
Failed to deploy artifacts: Could not transfer artifact nl.flotsam:xeger:jar:1.0.2 from/to nexus-releases
(http://localhost:8081/nexus/content/repositories/releases/): Failed to transfer file:
http://localhost:8081/nexus/content/repositories/releases/nl/flotsam/xeger/1.0.2/xeger-1.0.2.jar.
Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
【问题原因】是配置 Nexus 私服用户信息,或配置的账号、密码错误,均会出现 401 的错误
【解决方案】修改 maven settings.xml 配置文件,注意ID值和DrepositoryID值相同
<server>
<id>thirdpartid>
<username>adminusername>
<password>123456password>
server>
【问题原因】POM 文件中配置的 url 错误
【解决方案】修改url 地址,重新部署即可
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.969 s
[INFO] Finished at: 2016-02-18T10:47:22+08:00
[INFO] Final Memory: 18M/177M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project xeger:
Deployment failed: repository element was not specified in the POM inside distributionManagement element
or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
【问题原因】POM 文件遗漏了 Nexus 私服连接配置导致
【解决方案】在 POM 文件中添加 distributionManagement 节点或者在命令行执行时添加 -DaltDeploymentRepository=id::layout::url 参数即可解决
<project>
...
<distributionManagement>
<repository>
<id>nexus-releasesid>
<name>Nexus Releases Repository Proname>
<url>http://localhost:8081/nexus/content/repositories/releases/url>
repository>
<snapshotRepository>
<id>nexus-snapshotsid>
<name>Nexus Snapshots Repository Proname>
<url>http://localhost:8081/nexus/content/repositories/snapshots/url>
snapshotRepository>
distributionManagement>
...
project>
<server>
<id>my-deploy-releaseid>
<username>adminusername>
<password>admin123password>
server>
<server>
<id>my-deploy-snapshotid>
<username>adminusername>
<password>admin123password>
server>
<distributionManagement>
<repository>
<id>my-deploy-releaseid>
<url>http://192.168.1.123:8081/nexus/content/repositories/releases/url>
repository>
<snapshotRepository>
<id>my-deploy-snapshotid>
<url>http://192.168.1.123:8081/nexus/content/repositories/snapshots/url>
snapshotRepository>
distributionManagement>
执行命令:mvn deploy
注意:
maven会判断版本后面是否带了-SNAPSHOT,如果带了就发布到snapshots仓库,否则发布到release仓库。这里我们可以在pom.xml文件进行设置
<groupId>com.testgroupId>
<artifactId>my-testartifactId>
<packaging>jarpackaging>
<version>${project.release.version}version>
<properties>
<java.version>1.8java.version>
<project.release.version>1.0-SNAPSHOTproject.release.version>
properties>
<profiles>
<profile>
<id>productid>
<properties>
<project.release.version>1.0project.release.version>
properties>
profile>
profiles>
说明:通过占位符${project.release.version}来控制需要发布的版本,用命令mvn deploy -P product,发布my-test的1.0版本到releases库。如果使用命令mvn deploy,则默认使用 1.0-SNAPSHOT版本号,将发布my-test的1.0-SNAPSHOT版本到snapshots库。