aboutsummaryrefslogtreecommitdiff
path: root/runners/maven-plugin/build.gradle.kts
blob: 825493ad27cff0ea111763ea6d35e5a704ec11ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

import org.jetbrains.registerDokkaArtifactPublication

plugins {
    id("org.jetbrains.conventions.kotlin-jvm")
    id("org.jetbrains.conventions.maven-publish")
    id("org.jetbrains.conventions.maven-cli-setup")
}

dependencies {
    implementation(projects.core)

    implementation(libs.apacheMaven.core)
    implementation(libs.apacheMaven.pluginApi)
    implementation(libs.apacheMaven.pluginAnnotations)
    implementation(libs.apacheMaven.archiver)
}

val mavenPluginTaskGroup = "maven plugin"

val generatePom by tasks.registering(Sync::class) {
    description = "Generate pom.xml for Maven Plugin Plugin"
    group = mavenPluginTaskGroup

    val dokka_version: String by project
    inputs.property("dokka_version", dokka_version)

    val pomTemplateFile = layout.projectDirectory.file("pom.template.xml")

    val mavenVersion = mavenCliSetup.mavenVersion.orNull
    val mavenPluginToolsVersion = mavenCliSetup.mavenPluginToolsVersion.orNull

    from(pomTemplateFile) {
        rename { it.replace(".template.xml", ".xml") }

        expand(
            "mavenVersion" to mavenVersion,
            "dokka_version" to dokka_version,
            "mavenPluginToolsVersion" to mavenPluginToolsVersion,
        )
    }

    into(temporaryDir)
}

val prepareHelpMojoDir by tasks.registering(Sync::class) {
    description = "Prepare files for generating the Maven Plugin HelpMojo"
    group = mavenPluginTaskGroup

    into(layout.buildDirectory.dir("maven-help-mojo"))
    from(generatePom)
}

val helpMojo by tasks.registering(Exec::class) {
    description = "Generate the Maven Plugin HelpMojo"
    group = mavenPluginTaskGroup

    dependsOn(tasks.installMavenBinary, prepareHelpMojoDir)

    workingDir(prepareHelpMojoDir.map { it.destinationDir })
    executable(mavenCliSetup.mvn.get())
    args("-e", "-B", "org.apache.maven.plugins:maven-plugin-plugin:helpmojo")

    outputs.dir(workingDir)
}

val helpMojoSources by tasks.registering(Sync::class) {
    description = "Sync the HelpMojo source files into a SourceSet SrcDir"
    group = mavenPluginTaskGroup
    from(helpMojo) {
        eachFile {
            // drop 2 leading directories
            relativePath = RelativePath(true, *relativePath.segments.drop(2).toTypedArray())
        }
    }
    includeEmptyDirs = false
    into(temporaryDir)
    include("**/*.java")
}

val helpMojoResources by tasks.registering(Sync::class) {
    description = "Sync the HelpMojo resource files into a SourceSet SrcDir"
    group = mavenPluginTaskGroup
    from(helpMojo)
    into(temporaryDir)
    include("**/**")
    exclude("**/*.java")
}

sourceSets.main {
    // use the generated HelpMojo as compilation input, so Gradle will automatically generate the mojo
    java.srcDirs(helpMojoSources)
    resources.srcDirs(helpMojoResources)
}

val preparePluginDescriptorDir by tasks.registering(Sync::class) {
    description = "Prepare files for generating the Maven Plugin descriptor"
    group = mavenPluginTaskGroup

    into(layout.buildDirectory.dir("maven-plugin-descriptor"))

    from(tasks.compileKotlin) { into("classes/java/main") }
    from(tasks.compileJava) { into("classes/java/main") }
    from(helpMojoResources)
}

val pluginDescriptor by tasks.registering(Exec::class) {
    description = "Generate the Maven Plugin descriptor"
    group = mavenPluginTaskGroup

    dependsOn(tasks.installMavenBinary, preparePluginDescriptorDir)

    workingDir(preparePluginDescriptorDir.map { it.destinationDir })
    executable(mavenCliSetup.mvn.get())
    args("-e", "-B", "org.apache.maven.plugins:maven-plugin-plugin:descriptor")

    outputs.dir("$workingDir/classes/java/main/META-INF/maven")
}

tasks.jar {
    metaInf {
        from(pluginDescriptor) {
            into("maven")
        }
    }
    manifest {
        attributes("Class-Path" to configurations.runtimeClasspath.map { configuration ->
            configuration.resolve().joinToString(" ") { it.name }
        })
    }
}

registerDokkaArtifactPublication("dokkaMavenPlugin") {
    artifactId = "dokka-maven-plugin"
}