如何从自定义插件设置 Gradle Artifactory Publish 插件默认配置/属性

Posted

技术标签:

【中文标题】如何从自定义插件设置 Gradle Artifactory Publish 插件默认配置/属性【英文标题】:How to set Gradle Artifactory Publish plugin default configuration/properties from custom plugin 【发布时间】:2016-09-06 00:47:52 【问题描述】:

我一直在尝试从我的自定义插件中设置发布(PublisherConfig)、默认值(defaultsClosure) 的属性。最好的方法是什么?

我尝试了以下方法;

方法 1:尝试设置扩展的属性 project.getExtensions().publishing.getProperties().each println it

方法 2:尝试在 Gradle Artifactory 插件上添加编译时依赖项并导入插件类... ```

        if (project.plugins.hasPlugin("com.jfrog.artifactory")) 
            println "I found jfrog.artifactory plugin"
            Plugin jfrogArtifactory = project.getPlugins().getPlugin("com.jfrog.artifactory")

            ArtifactoryPluginConvention apc = new ArtifactoryPluginConvention(project);
            project.getExtensions().add("artifactory", apc);

            apc.contextUrl = 'https://myrepo.com/artifactory/'


            PublisherConfig pc = new PublisherConfig(apc);

            pc.defaults 
                println "in my plugin pc.defaults : " + it.metaClass

                publications('mavenJava')
                publishConfigs('archives', 'published')


                properties = ['my.gitCommitUrl': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitCommitUrl, 'my.gitHash': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitHash, 'my.gitBranch': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitBranch]
                publishBuildInfo = true   //Publish build-info to Artifactory (true by default)
                publishArtifacts = true   //Publish artifacts to Artifactory (true by default)
                publishPom = true   //Publish generated POM files to Artifactory (true by default).
                publishIvy = false   //Publish generated Ivy descriptor files to Artifactory (true by default).

            

            pc.repository 
                println "in my plugin pc.repository : " + it.metaClass
                repoKey = 'my-mvn'   //The Artifactory repository key to publish to
                username = project.findProperty("artifactory_user") ?: ""          //The publisher user name
                password = project.findProperty("artifactory_api_key") ?: ""       //The publisher password
            

            apc.setPublisherConfig(pc)

        

```

编译构建成功,工件及其信息未发布。

:artifactoryPublish

构建成功

总时间:3.313 秒

【问题讨论】:

【参考方案1】:

这对我有用!!!

`

        if (project.plugins.hasPlugin("com.jfrog.artifactory")) 
            Plugin jfrogArtifactory = project.getPlugins().getPlugin("com.jfrog.artifactory")

            //maven install task is required to publishPom (using artifactory plugin)
            if (!project.plugins.hasPlugin("maven")) 
                project.apply(plugin: MavenPlugin)
            

            Closure artifactoryPublishClosure = 
                contextUrl = 'https://myrepo.com/artifactory/'

                publications('mavenJava')

                publishConfigs('archives', 'published')

                properties = ['gitCommitUrl': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitCommitUrl, 'gitHash': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitHash, 'gitBranch': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitBranch]

                publishBuildInfo = true   //Publish build-info to Artifactory (true by default)
                publishArtifacts = true   //Publish artifacts to Artifactory (true by default)
                publishPom = true   //Publish generated POM files to Artifactory (true by default).
                publishIvy = false   //Publish generated Ivy descriptor files to Artifactory (true by default).

                // Redefine basic properties of the build info object
                clientConfig.setIncludeEnvVars(true)
                clientConfig.timeout = 600 // Artifactory connection timeout (in seconds). The default timeout is 300 seconds.

                publish 
                    //A closure defining publishing information
                    repository 
                        repoKey = 'my-mvn'   //The Artifactory repository key to publish to
                        username = project.findProperty("artifactory_user") ?: ""          //The publisher user name
                        password = project.findProperty("artifactory_api_key") ?: ""       //The publisher password
                    
                

                resolve 
                    repository 
                        repoKey = 'my-mvn'  //The Artifactory (preferably virtual) repository key to resolve from
                    
                

            

            project.getTasks().findByName("artifactoryPublish").configure(artifactoryPublishClosure)
        

`

【讨论】:

以上是关于如何从自定义插件设置 Gradle Artifactory Publish 插件默认配置/属性的主要内容,如果未能解决你的问题,请参考以下文章

如何从自定义页面模板调用 WordPress 插件功能?

gradle / groovy:从自定义类调用“全局”函数

Gradle nexus 解决开发机器不连网无法下载gradle插件问题

如何使用自定义 gradle 插件而不将其发布到 maven/ivy 存储库?

Android Gradle 插件将自定义 Gradle 插件上传到自建 Maven 仓库 ⑧ ( 设置 Gradle 插件模块 | 将 Gradle 插件上传到自建 Maven 仓库 )

从自定义 mojo 访问 maven 插件的运行时配置的最佳方式?