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

Gradle: Using 'maven-publish' plugin in custom standalone plugin

问完第三个问题后感觉就像在发垃圾邮件,因为我的问题看起来太琐碎了。但是我在 Gradle 文档中找不到任何帮助我的案例,而且关于 Whosebug 的其他问题也没有帮助。

这一次:我无法在自己的独立自定义插件中使用 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

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

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

找到了一个适合我的解决方案。我不再使用 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 }
}

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

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

如果您在构建脚本中编写插件,默认情况下它将具有 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/

在我的例子中,我修复了错误消息并添加了这个:

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

但后来又遇到其他问题,最后还是用nebula-publishing-plugin,解决了一切

这对我有用,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'
                    }
            }})