Gradle:使用' maven-publish'自定义独立插件中的插件

时间:2016-08-03 15:57:16

标签: gradle groovy maven-publish

在我的第三个问题之后几乎感觉像是垃圾邮件,因为我的问题看起来很微不足道。但是我在Gradle Docs中找不到任何有关我的案例的帮助,而且有关stackoverflow的其他问题也没有帮助。

这次:我无法在自己的独立自定义插件中使用maven-publish插件。到目前为止我的插件的代码:

package com.sample.gradle;

import org.gradle.api.Project;
import org.gradle.api.Plugin;

public class SampleGradlePlugin implements Plugin<Project> {
    public void apply(Project project) {

        project.apply plugin: 'application'
        project.apply plugin: 'maven-publish'
        project.apply plugin: 'maven'

        project.publishing.publications {
            maven(MavenPublication) {
                groupId project.group
                artifactId project.name
                version=project.version
                from components.java
            }
        }

        project.uploadArchives {
            repositories {
                mavenDeployer {
                    repository(url: project.Repo_Upload_Internal){
                        authentication(userName: project.Repo_Upload_User, password: project.Repo_Upload_Pass)
                    }        
                    snapshotRepository(url: project.Repo_Upload_Snapshot){
                        authentication(userName: project.Repo_Upload_User, password: project.Repo_Upload_Pass)
                    }
                }
            }
        }
        //this try/catch is not important for my question
        try{
            project.repositories {
                maven {
                    url project.Repo_Gp_Internal
                    credentials {
                        username project.Repo_Gp_User;
                    }
                }
            }
        }
        catch(MissingPropertyException e){
            println "Info: '"+e.getProperty()+"' not declared in \\"+project.name+"\\gradle.properties"
        }
    }
}

当我在我的一个项目中使用我的插件时,publishing闭包是带来这个错误的重要部分:

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\quastc\Desktop\Gradle_Projects\Application_RootProject\buil
d.gradle' line: 3

* What went wrong:
A problem occurred evaluating root project 'Application_RootProject'.
> Failed to apply plugin [id 'sample.sample-groovy-plugin']
   > Cannot create a Publication named 'MavenPublication' because this container
 does not support creating elements by name alone. Please specify which subtype
of Publication to create. Known subtypes are: MavenPublication

我的错误在哪里?我忘记了什么?

如果有人回答,请告诉我你是如何得到你的知识的。

4 个答案:

答案 0 :(得分:1)

在我的情况下,我修复了添加此内容的错误消息:

import org.gradle.api.publish.maven.MavenPublication

然后面对其他问题,最后结束使用nebula-publishing-plugin,解决所有问题

答案 1 :(得分:1)

这对我有用,gradle 4.10和maven-publish。

在我的自定义插件.groovy中:

import org.gradle.api.publish.maven.MavenPublication

...

project.extensions.configure PublishingExtension, new ClosureBackedAction( {
                publications{
                    mycustompub(MavenPublication) {
                        groupId = project.group
                        artifactId = <artifact name>
                        artifact source: <path to artifact>, extension: 'zip'
                    }
            }})

答案 2 :(得分:0)

找到一个适合我的解决方案。我不再使用maven-publish插件了。只是maven插件。然后我将这段代码放在我自己的插件的末尾:

    project.configurations {
        deployerJars 
    }
    project.dependencies {
        deployerJars "org.apache.maven.wagon:wagon-http:2.2"
    }   
    project.uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: project.Repo_Upload_Internal){
                    authentication(userName: project.Repo_Upload_User, password: project.Repo_Upload_Pass)
                }        
                snapshotRepository(url: project.Repo_Upload_Snapshot){
                    authentication(userName: project.Repo_Upload_User, password: project.Repo_Upload_Pass)
                }
            }
        }
    }

此外:

我还没有找到一种方法来抑制zip和tar文件的上传。我仍然需要在几个build.gradle文件中使用一些复制和粘贴代码来完成这项工作。这是代码:

configurations.archives.with {
    artifacts.remove artifacts.find { it.archiveTask.is distZip }
    artifacts.remove artifacts.find { it.archiveTask.is distTar }
}

我无法将该代码更改为在我的自定义插件中工作。

答案 3 :(得分:0)

传递给project.publishing.publications的闭包在执行时在其上下文中没有project。这意味着即使您将MavenPublication应用于maven-pluginproject也不在关闭的上下文中。

如果您在构建脚本中编写插件,默认情况下它将具有project作为上下文,但在将其作为独立插件编写时,上下文会有所不同。

您可以setDelegate(project)在闭包上设置上下文,然后传递它:

def publicationsClosure =  {
    maven(MavenPublication) {
        groupId project.group
        artifactId project.name
        version=project.version
        from components.java
    }
}
publicationsClosure.setDelegate(project)
project.publishing.publications(publicationsClosure)

我在插件中遇到了同样的错误,并最终意识到我在阅读本文时发生了什么:http://trickyandroid.com/gradle-tip-2-understanding-syntax/