直接从我的自定义插件配置gradle“maven-publish”插件

idfiyjo8  于 2023-06-23  发布在  Maven
关注(0)|答案(2)|浏览(158)

我是新的Gradle,并希望创建自定义Gradle插件,应用maven-publish插件。此外,我的插件应该配置maven-publish插件,以便另一个插件用户不应该做任何事情。我的插件将自动配置maven-publish。我试图找到任何相同的教程,但没有找到。如何从我的自定义插件配置maven-publish gradle插件?

xggvc2p6

xggvc2p61#

从自定义插件配置其他插件是非常常见的。你应该能够引用任何自定义插件的例子。具体针对maven-publish,我创建了以下示例:

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;

import java.net.URI;

public class MyPlugin implements Plugin<Project> {

    @Override
    public void apply(Project project) {
        project.getPluginManager().apply(MavenPublishPlugin.class);

        project.getExtensions().configure(PublishingExtension.class, publishing -> {
            publishing.repositories(repositories -> {
                repositories.maven(maven -> {
                   maven.setUrl(URI.create("https://my-publishing-repo.com"));
                });
            });
            publishing.publications(publications -> {
                publications.create("mavenJava", MavenPublication.class, mavenJava -> {
                    mavenJava.artifact(project.getTasks().named("bootJar"));
                });
            });
        });
    }

}

这相当于Gradle构建文件(KotlinDSL)中的以下内容:

plugins {
    `maven-publish`
}

publishing {
    repositories {
        maven {
            url = uri("https://my-publishing-repo.com")
        }
    }
    publications {
        create<MavenPublication>("mavenJava") {
            artifact(tasks.named("bootJar").get())
        }
    }
}

请参阅我的指导中的以下指南:

q8l4jmvw

q8l4jmvw2#

@Cisco的回答对我很有帮助,但我仍然很难将apply方法翻译成使用kotlin-dsl。最后我得到了这个

class MyPlugin : Plugin<Project> {
    override fun apply(project: Project) {
    
        project.pluginManager.apply(MavenPublishPlugin::class.java)
        project.extensions.configure<PublishingExtension> {
            repositories {
                repositories.maven {
                    url = URI.create("https://my-publishing-repo.com")
                }
            }
            publications {
                publications.create(
                    "mavenJava",
                    MavenPublication::class.java
                ) {
                    artifact(project.tasks.named("bootJar"))
                }
            }
        }
    }
}

相关问题