我有一个非Java项目,它生成一个版本化的构建工件,我想将它上传到Nexus存储库.因为项目不是Java,所以它不使用Maven进行构建.我宁愿不介绍Maven/POM文件只是为了让文件进入Nexus.
博客上的Nexus REST API链接最终都在登录墙上,没有"创建用户"链接,我可以看到.
那么,在没有Maven的情况下,将构建工件上传到Nexus存储库的最佳(或任何合理)方法是什么?"bash + curl"会很棒,甚至是Python脚本.
Mar*_*nor 97
您是否考虑过使用Maven命令行上传文件?
mvn deploy:deploy-file \
-Durl=$REPO_URL \
-DrepositoryId=$REPO_ID \
-DgroupId=org.myorg \
-DartifactId=myproj \
-Dversion=1.2.3 \
-Dpackaging=zip \
-Dfile=myproj.zip
Run Code Online (Sandbox Code Playgroud)
这将自动为工件生成Maven POM.
以下Sonatype文章指出"deploy-file"maven插件是最简单的解决方案,但它也提供了一些使用curl的示例:
Ed *_*d I 62
使用curl:
curl -v \
-F "r=releases" \
-F "g=com.acme.widgets" \
-F "a=widget" \
-F "v=0.1-1" \
-F "p=tar.gz" \
-F "file=@./widget-0.1-1.tar.gz" \
-u myuser:mypassword \
http://localhost:8081/nexus/service/local/artifact/maven/content
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看参数的含义:https://support.sonatype.com/entries/22189106-How-can-I-programatically-upload-an-artifact-into-Nexus-
为了获得此工作的权限,我在管理GUI中创建了一个新角色,并为该角色添加了两个权限:工件下载和工件上载.标准"Repo:所有Maven存储库(完全控制)" - 角色是不够的.您不会在与Nexus服务器捆绑在一起的REST API文档中找到它,因此这些参数将来可能会发生变化.
在一个Sonatype JIRA问题上,有人提到他们"将在即将发布的版本中对其进行大修(以及它的文档生成方式),很可能在今年晚些时候".
无需使用这些命令..您可以直接使用nexus Web界面,以便使用GAV参数上传您的JAR.
所以很简单.
你可以绝对不使用MAVEN相关的任何东西.我个人使用NING HttpClient(v1.8.16,支持java6).
无论出于何种原因,Sonatype的使得它令人难以置信的困难,找出正确的URL,标题和有效载荷应该是; 我不得不嗅到流量并猜测......那里有一些几乎没有用的博客/文档,但它或者是无关的oss.sonatype.org
,或者它是基于XML的(我发现它甚至不起作用).他们自己的垃圾文件,恕我直言,希望未来的寻求者可以找到这个答案有用.非常感谢/sf/answers/2339009641/的帖子,因为它帮了很多忙.
如果您在其他地方发布oss.sonatype.org
,只需将其替换为正确的主机.
这是我为完成此任务而编写的(CC0许可的)代码.哪里profile
是你的Sonatype的/承上启下配置文件ID(如4364f3bbaf163
)和repo
(如comdorkbox-1003
)从当您上传最初的POM /瓶的响应进行解析.
关闭回购:
/**
* Closes the repo and (the server) will verify everything is correct.
* @throws IOException
*/
private static
String closeRepo(final String authInfo, final String profile, final String repo, final String nameAndVersion) throws IOException {
String repoInfo = "{'data':{'stagedRepositoryId':'" + repo + "','description':'Closing " + nameAndVersion + "'}}";
RequestBuilder builder = new RequestBuilder("POST");
Request request = builder.setUrl("https://oss.sonatype.org/service/local/staging/profiles/" + profile + "/finish")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Basic " + authInfo)
.setBody(repoInfo.getBytes(OS.UTF_8))
.build();
return sendHttpRequest(request);
}
Run Code Online (Sandbox Code Playgroud)
促进回购:
/**
* Promotes (ie: release) the repo. Make sure to drop when done
* @throws IOException
*/
private static
String promoteRepo(final String authInfo, final String profile, final String repo, final String nameAndVersion) throws IOException {
String repoInfo = "{'data':{'stagedRepositoryId':'" + repo + "','description':'Promoting " + nameAndVersion + "'}}";
RequestBuilder builder = new RequestBuilder("POST");
Request request = builder.setUrl("https://oss.sonatype.org/service/local/staging/profiles/" + profile + "/promote")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Basic " + authInfo)
.setBody(repoInfo.getBytes(OS.UTF_8))
.build();
return sendHttpRequest(request);
}
Run Code Online (Sandbox Code Playgroud)
删除回购:
/**
* Drops the repo
* @throws IOException
*/
private static
String dropRepo(final String authInfo, final String profile, final String repo, final String nameAndVersion) throws IOException {
String repoInfo = "{'data':{'stagedRepositoryId':'" + repo + "','description':'Dropping " + nameAndVersion + "'}}";
RequestBuilder builder = new RequestBuilder("POST");
Request request = builder.setUrl("https://oss.sonatype.org/service/local/staging/profiles/" + profile + "/drop")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Basic " + authInfo)
.setBody(repoInfo.getBytes(OS.UTF_8))
.build();
return sendHttpRequest(request);
}
Run Code Online (Sandbox Code Playgroud)
删除签名粪便:
/**
* Deletes the extra .asc.md5 and .asc.sh1 'turds' that show-up when you upload the signature file. And yes, 'turds' is from sonatype
* themselves. See: https://issues.sonatype.org/browse/NEXUS-4906
* @throws IOException
*/
private static
void deleteSignatureTurds(final String authInfo, final String repo, final String groupId_asPath, final String name,
final String version, final File signatureFile)
throws IOException {
String delURL = "https://oss.sonatype.org/service/local/repositories/" + repo + "/content/" +
groupId_asPath + "/" + name + "/" + version + "/" + signatureFile.getName();
RequestBuilder builder;
Request request;
builder = new RequestBuilder("DELETE");
request = builder.setUrl(delURL + ".sha1")
.addHeader("Authorization", "Basic " + authInfo)
.build();
sendHttpRequest(request);
builder = new RequestBuilder("DELETE");
request = builder.setUrl(delURL + ".md5")
.addHeader("Authorization", "Basic " + authInfo)
.build();
sendHttpRequest(request);
}
Run Code Online (Sandbox Code Playgroud)
文件上传:
public
String upload(final File file, final String extension, String classification) throws IOException {
final RequestBuilder builder = new RequestBuilder("POST");
final RequestBuilder requestBuilder = builder.setUrl(uploadURL);
requestBuilder.addHeader("Authorization", "Basic " + authInfo)
.addBodyPart(new StringPart("r", repo))
.addBodyPart(new StringPart("g", groupId))
.addBodyPart(new StringPart("a", name))
.addBodyPart(new StringPart("v", version))
.addBodyPart(new StringPart("p", "jar"))
.addBodyPart(new StringPart("e", extension))
.addBodyPart(new StringPart("desc", description));
if (classification != null) {
requestBuilder.addBodyPart(new StringPart("c", classification));
}
requestBuilder.addBodyPart(new FilePart("file", file));
final Request request = requestBuilder.build();
return sendHttpRequest(request);
}
Run Code Online (Sandbox Code Playgroud)
EDIT1:
如何获取回购的活动/状态
/**
* Gets the activity information for a repo. If there is a failure during verification/finish -- this will provide what it was.
* @throws IOException
*/
private static
String activityForRepo(final String authInfo, final String repo) throws IOException {
RequestBuilder builder = new RequestBuilder("GET");
Request request = builder.setUrl("https://oss.sonatype.org/service/local/staging/repository/" + repo + "/activity")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Basic " + authInfo)
.build();
return sendHttpRequest(request);
}
Run Code Online (Sandbox Code Playgroud)
您需要针对Nexus进行的呼叫是REST api呼叫.
maven-nexus-plugin是一个Maven插件,可用于进行这些调用.您可以创建具有必要属性的虚拟pom,并通过Maven插件进行调用.
就像是:
mvn -DserverAuthId=sonatype-nexus-staging -Dauto=true nexus:staging-close
Run Code Online (Sandbox Code Playgroud)
假设:
最终,所有这一切都是在Nexus中创建REST调用.有一个完整的Nexus REST api,但我很幸运找到了它的文档,而不是付费墙.您可以打开上面插件的调试模式,然后通过使用来计算出来-Dnexus.verboseDebug=true -X
.
您理论上也可以进入用户界面,打开Firebug Net面板,并观察/服务POST并在那里推断出路径.
您还可以使用curl 来使用直接部署方法。您的文件不需要 pom,但它也不会生成,因此如果您想要一个,则必须单独上传。
这是命令:
version=1.2.3
artifact="myartifact"
repoId=yourrepository
groupId=org.myorg
REPO_URL=http://localhost:8081/nexus
curl -u nexususername:nexuspassword --upload-file filename.tgz $REPO_URL/content/repositories/$repoId/$groupId/$artifact/$version/$artifact-$version.tgz
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
120385 次 |
最近记录: |