【问题标题】:groovy upload jar to nexusgroovy上传jar到nexus
【发布时间】:2013-05-31 18:34:48
【问题描述】:

我有一些 jar 文件(自定义)需要从 Groovy 脚本发布到 Sonatype Nexus 存储库。

我的 jar 位于 Groovy 脚本工作的机器上的某个路径中(例如:c:\temp\module.jar)。

我的 Nexus 回购网址是 http://:/nexus/content/repositories/

在这个 repo 中,我的文件夹结构如下:folder1->folder2->folder3

在发布我的 jar 时,我需要在 folder3 中创建:

  1. 带有模块修订版的新目录(我的 Groovy 脚本知道此修订版)
  2. 上传jar到这个目录
  3. 为上传的 jar 创建 pom、md5 和 sha1 文件

经过几天的调查,我仍然不知道如何创建这样的脚本,但是这种方式看起来很清楚,而不是直接上传。

我找到了http://groovy.codehaus.org/Using+Ant+Libraries+with+AntBuilder 和其他一些东西 (stackoverflow non script solution)。

我知道如何在我的 Groovy 脚本中创建 ivy.xml,但我不明白如何即时创建 build.xml 和 ivysetting.xml 并设置整个系统工作。

能否请您帮忙理解一下 Groovy 的方式?

更新: 我发现以下命令对我来说很好用:

curl -v -F r=thirdparty -F hasPom=false -F e=jar -F g=<my_groupId> -F a=<my_artifactId> -F v=<my_artifactVersion> -F p=jar -F file=@module.jar -u admin:admin123 http://<my_nexusServer>:8081/nexus/service/local/repositories

据我了解,curl 向 Nexus 服务执行 POST 请求。我说的对吗?

现在我正在尝试使用 Groovy HTTPBuilder 构建 HTTP POST 请求。

我应该如何将 curl 命令参数转换为 Groovy 的 HTTPBuilder 请求?

【问题讨论】:

    标签: http groovy upload ivy nexus


    【解决方案1】:

    Ivy 是一个开源库,因此,一种方法是直接调用类。这种方法的问题在于很少有关于如何invoke ivy programmatically 的示例。

    由于 groovy 对生成 XML 有很好的支持,我喜欢用稍微笨拙的方法来创建我理解为 ivy 用户的文件。

    以下示例旨在将文件发布到Nexus,生成 ivy 和 ivysettings 文件:

    import groovy.xml.NamespaceBuilder
    import groovy.xml.MarkupBuilder
    
    // Methods
    // =======
    def generateIvyFile(String fileName) {
        def file = new File(fileName)
    
        file.withWriter { writer ->
            xml = new MarkupBuilder(writer)
    
            xml."ivy-module"(version:"2.0") {
                info(organisation:"org.dummy", module:"dummy")
                publications() {
                    artifact(name:"dummy", type:"pom")
                    artifact(name:"dummy", type:"jar")
                }
            }
        }
    
        return file
    }
    
    def generateSettingsFile(String fileName) {
        def file = new File(fileName)
    
        file.withWriter { writer ->
            xml = new MarkupBuilder(writer)
    
            xml.ivysettings() {
                settings(defaultResolver:"central")
                credentials(host:"myrepo.com" ,realm:"Sonatype Nexus Repository Manager", username:"deployment", passwd:"deployment123")
                resolvers() {
                    ibiblio(name:"central", m2compatible:true)
                    ibiblio(name:"myrepo", root:"http://myrepo.com/nexus", m2compatible:true)
                }
            }
        }
    
        return file
    }
    
    // Main program
    // ============
    def ant = new AntBuilder()
    def ivy = NamespaceBuilder.newInstance(ant, 'antlib:org.apache.ivy.ant')
    
    generateSettingsFile("ivysettings.xml").deleteOnExit()
    generateIvyFile("ivy.xml").deleteOnExit()
    
    ivy.resolve()
    ivy.publish(resolver:"myrepo", pubrevision:"1.0", publishivy:false) {
        artifacts(pattern:"build/poms/[artifact].[ext]")
        artifacts(pattern:"build/jars/[artifact].[ext]")
    }
    

    注意事项:

    • 更复杂?也许......但是,如果您不生成 ivy 文件(使用它来管理您的依赖项),您可以轻松地调用 makepom 任务以在上传到 Nexus 之前生成 Maven POM 文件。
    • Nexus 的 REST API 工作正常。我觉得它们有点神秘,当然使用它们的解决方案不能支持多个存储库管理器(Nexus 不是唯一可用的存储库管理器技术)。
    • “deleteOnExit”文件方法调用可确保正确清理工作文件。

    【讨论】:

    • 马克,你能给我一个使用 HTTPBuilder 上传工件的示例吗?它对我来说看起来很轻,因为我只需要上传 1 个文件。
    • doco 不错。 groovy.codehaus.org/HTTP+Builder 将“POST”替换为“GET”方法,您应该是金子。我认为唯一的问题是 POM 文件的生成。我不认为 N​​exus 会为你这样做吗?
    【解决方案2】:

    找到了一种使用 groovy HttpBuilder 的方法。

    基于来自sonatype 和其他一些来源的信息。

    这适用于 http-builder 版本 0.7.2(不适用于早期版本) 并且还需要一个额外的依赖:'org.apache.httpcomponents:httpmime:4.2.1'

    该示例还针对 nexus 使用基本身份验证。

    import groovyx.net.http.Method
    import groovyx.net.http.ContentType;
    import org.apache.http.HttpRequest
    import org.apache.http.HttpRequestInterceptor
    import org.apache.http.entity.mime.MultipartEntity
    import org.apache.http.entity.mime.content.FileBody
    import org.apache.http.entity.mime.content.StringBody
    import org.apache.http.protocol.HttpContext
    import groovyx.net.http.HttpResponseException;
    
    class NexusUpload {
      def uploadArtifact(Map artifact, File fileToUpload, String user, String password) {
        def path = "/service/local/artifact/maven/content"
        HTTPBuilder http = new HTTPBuilder("http://my-nexus.org/")
        String basicAuthString = "Basic " + "$user:$password".bytes.encodeBase64().toString()
    
        http.client.addRequestInterceptor(new HttpRequestInterceptor() {
          void process(HttpRequest httpRequest, HttpContext httpContext) {
            httpRequest.addHeader('Authorization', basicAuthString)
          }
        })
        try {
          http.request(Method.POST, ContentType.ANY) { req ->
            uri.path = path
    
          MultipartEntity entity = new MultipartEntity()
          entity.addPart("hasPom", new StringBody("false"))
          entity.addPart("file", new FileBody(fileToUpload))
          entity.addPart("a", new StringBody("my-artifact-id"))
          entity.addPart("g", new StringBody("my-group-id"))
          entity.addPart("r", new StringBody("my-repository"))
          entity.addPart("v", new StringBody("my-version"))
          req.entity = entity
    
          response.success = { resp, reader ->
            if(resp.status == 201) {
              println "success!"
            }
          }
        }
    
        } catch (HttpResponseException e) {
          e.printStackTrace()
        }
      }
    }
    

    `

    【讨论】:

      猜你喜欢
      • 2015-07-26
      • 2016-11-30
      • 1970-01-01
      • 2012-04-24
      • 2019-12-07
      • 1970-01-01
      • 2015-12-29
      • 2017-01-29
      • 2021-02-27
      相关资源
      最近更新 更多