aboutsummaryrefslogtreecommitdiff
path: root/build-logic/src/main/kotlin/org/jetbrains/conventions/maven-cli-setup.gradle.kts
blob: ec59da7bfac167ce7e9a13e345d5e66a58941c13 (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
package org.jetbrains.conventions

import org.gradle.kotlin.dsl.support.serviceOf

/**
 * Utility for downloading and installing a Maven binary.
 *
 * Provides the `setupMavenProperties` extension that contains the default versions and locations
 * of the Maven binary.
 *
 * The task [installMavenBinary] will download and unzip the Maven bianry.
 */

plugins {
    base
}

abstract class SetupMavenProperties {
    abstract val mavenVersion: Property<String>
    abstract val mavenPluginToolsVersion: Property<String>
    abstract val mavenBuildDir: DirectoryProperty

    /** Directory that will contain the unpacked Apache Maven dependency */
    abstract val mavenInstallDir: DirectoryProperty

    /**
     * Path to the Maven executable.
     *
     * This should be different per OS:
     *
     * * Windows: `$mavenInstallDir/bin/mvn.cmd`
     * * Unix: `$mavenInstallDir/bin/mvn`
     */
    abstract val mvn: RegularFileProperty
}

val setupMavenProperties =
    extensions.create("setupMavenProperties", SetupMavenProperties::class).apply {
        mavenVersion.convention(providers.gradleProperty("mavenVersion"))
        mavenPluginToolsVersion.convention(providers.gradleProperty("mavenPluginToolsVersion"))

        mavenBuildDir.convention(layout.buildDirectory.dir("maven"))
        mavenInstallDir.convention(layout.buildDirectory.dir("apache-maven"))

        val isWindowsProvider =
            providers.systemProperty("os.name").map { "win" in it.toLowerCase() }

        mvn.convention(
            providers.zip(mavenInstallDir, isWindowsProvider) { mavenInstallDir, isWindows ->
                mavenInstallDir.file(
                    when {
                        isWindows -> "bin/mvn.cmd"
                        else -> "bin/mvn"
                    }
                )
            }
        )
    }

val mavenBinary by configurations.registering {
    description = "used to download the Maven binary"
    isCanBeResolved = true
    isCanBeConsumed = false
    isVisible = false

    defaultDependencies {
        addLater(setupMavenProperties.mavenVersion.map { mavenVersion ->
            project.dependencies.create(
                group = "org.apache.maven",
                name = "apache-maven",
                version = mavenVersion,
                classifier = "bin",
                ext = "zip"
            )
        })
    }
}

tasks.clean {
    delete(setupMavenProperties.mavenBuildDir)
    delete(setupMavenProperties.mavenInstallDir)
}

val installMavenBinary by tasks.registering(Sync::class) {
    val archives = serviceOf<ArchiveOperations>()
    from(
        mavenBinary.flatMap { conf ->
            @Suppress("UnstableApiUsage")
            val resolvedArtifacts = conf.incoming.artifacts.resolvedArtifacts

            resolvedArtifacts.map { artifacts ->
                artifacts.map { archives.zipTree(it.file) }
            }
        }
    ) {
        eachFile {
            // drop the first directory inside the zipped Maven bin (apache-maven-$version)
            relativePath = RelativePath(true, *relativePath.segments.drop(1).toTypedArray())
        }
        includeEmptyDirs = false
    }
    into(setupMavenProperties.mavenInstallDir)
}