如何插件以编程方式配置maven-publish发布并允许build.gradle修改它 [英] How can plugin programmatically configure maven-publish publishing and allow build.gradle to modify it

查看:979
本文介绍了如何插件以编程方式配置maven-publish发布并允许build.gradle修改它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为parent的插件中设置了项目范围设置,该插件尝试应用maven-publish插件,然后以编程方式配置发布扩展。这似乎工作,但是当我将这个插件应用于build.gradle脚本时,我无法配置发布扩展来设置项目特定的发布。



我收到错误:

 访问后无法配置'发布'扩展名。 

我的目的是在父插件中设置发布库,然后让每个build.gradle脚本添加适当的出版物。

有没有办法做到这一点?



目前,ParentPlugin.groovy如下所示:

  def void apply(Project project){
project.getProject()。apply plugin:'maven-publish'

def publishingExtension = project.extensions.findByName('publishing')

publishingExtension.with {
repositories {
maven {
mavenLocal ()

credentials {
username getPropertyWithDefault(project.getProject(),'publishUserName','dummy')
密码getPropertyWithDefault(project.getProject(),'publishPassword', 'dummy')
}

}
}
}
}

我的客户端build.gradle在尝试配置发布扩展时失败。

  apply plugin :'parent'

发布{
publications {
mavenJava(MavenPublication){
groupId'agroup'
artifactId'anartifactid'
version '1.0.0-SNAPSHOT'

from components.java
}
}
}

这可能吗?我还有另外一种方法可以解决这个问题吗?为了解决这个问题,我编写了另一个插件,它可以延迟修改出版物,同时避免阅读该扩展名会将其置于已配置状态。这个插件被称为nebula-publishing-plugin,懒区块的代码可以在 github repo 。它看起来像这样:

  / ** 
*所有Maven出版物
* /
def withMavenPublication(Closure withPubClos​​ure){
//新的发布插件方式在结果发布中指定工件
def addArtifactClosure = {

//等待我们的插件被应用。
project.plugins.withType(PublishingPlugin){PublishingPlugin publishingPlugin - >
DefaultPublishingExtension publishingExtension = project.getExtensions()。getByType(DefaultPublishingExtension)
publishingExtension.publications.withType(MavenPublication,withPubClos​​ure)
}
}

/ /有可能我们在别人的afterEvaluate中运行,这意味着我们需要立即运行这个
if(project.getState()。execution){
addArtifactClosure.call()
}其他{
project.afterEvaluate addArtifactClosure
}
}

您然后像这样调用它:

  withMavenPublication {MavenPublication t  - > 
def webComponent = project.components.getByName('web')
// TODO以某种方式包含deps
t.from(webComponent)
}

这个插件可以在jcenter()中作为'com.netflix.nebula:nebula-publishing-plugin:1.9.1' p>

I have project wide settings in a plugin, called parent, that attempts to apply the maven-publish plugin and then programmatically configure the publishing extension. This seems to work but when I apply this plugin in a build.gradle script I can not configure publishing extension to set the project specific publications.

I receive the error:

  Cannot configure the 'publishing' extension after it has been accessed.

My intent was to set up the publishing repository in the parent plugin and then let each build.gradle script add the appropriate publications.

Is there a way to do this?

Currently ParentPlugin.groovy looks like:

def void apply(Project project) {
    project.getProject().apply plugin: 'maven-publish'

     def publishingExtension = project.extensions.findByName('publishing')

    publishingExtension.with {
        repositories {
            maven {
                mavenLocal()

                credentials {
                    username getPropertyWithDefault(project.getProject(), 'publishUserName', 'dummy')
                    password getPropertyWithDefault(project.getProject(), 'publishPassword', 'dummy')
                }

            }
        }
    }
}

My client build.gradle fails when it tries to configure the publishing extension.

apply plugin: 'parent'

publishing {
        publications {
            mavenJava(MavenPublication) {
                groupId 'agroup'
                artifactId 'anartifactid'
                version '1.0.0-SNAPSHOT'

                from components.java
            }
        }
}

Is this possible? Is there another way I should be approaching this?

解决方案

To deal with this, I wrote another plugin, which can delay modifications to the publication while also avoid a "reading" of the extension, which would put it in the "configured" state. The plugin is called nebula-publishing-plugin, the code for the "lazy" block can be found in the github repo. It looks like this:

/**
 * All Maven Publications
 */
def withMavenPublication(Closure withPubClosure) {
    // New publish plugin way to specify artifacts in resulting publication
    def addArtifactClosure = {

        // Wait for our plugin to be applied.
        project.plugins.withType(PublishingPlugin) { PublishingPlugin publishingPlugin ->
            DefaultPublishingExtension publishingExtension = project.getExtensions().getByType(DefaultPublishingExtension)
            publishingExtension.publications.withType(MavenPublication, withPubClosure)
        }
    }

    // It's possible that we're running in someone else's afterEvaluate, which means we need to run this immediately
    if (project.getState().executed) {
        addArtifactClosure.call()
    } else {
        project.afterEvaluate addArtifactClosure
    }
}

You would then call it like this:

withMavenPublication { MavenPublication t ->
        def webComponent = project.components.getByName('web')
        // TODO Include deps somehow
        t.from(webComponent)
    }

The plugin is available in jcenter() as 'com.netflix.nebula:nebula-publishing-plugin:1.9.1'.

这篇关于如何插件以编程方式配置maven-publish发布并允许build.gradle修改它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆