2
* Copyright (C) 2021 Square, Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* https://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
import aQute.bnd.gradle.BundleTaskExtension
18
import org.gradle.api.Project
19
import org.gradle.api.artifacts.VersionCatalogsExtension
20
import org.gradle.api.plugins.ExtensionAware
21
import org.gradle.api.tasks.SourceSetContainer
22
import org.gradle.api.tasks.bundling.Jar
23
import org.gradle.kotlin.dsl.dependencies
24
import org.gradle.kotlin.dsl.findByType
25
import org.gradle.kotlin.dsl.get
26
import org.gradle.kotlin.dsl.getByName
28
fun Project.applyOsgi(vararg bndProperties: String) {
29
plugins.withId("org.jetbrains.kotlin.jvm") {
30
applyOsgi("jar", "osgiApi", bndProperties)
34
private fun Project.applyOsgi(
36
osgiApiConfigurationName: String,
37
bndProperties: Array<out String>,
39
val osgi = project.sourceSets.create("osgi")
40
val osgiApi = project.configurations.getByName(osgiApiConfigurationName)
42
extensions.getByType(VersionCatalogsExtension::class.java).named("libs")
43
.findLibrary("kotlin.stdlib.osgi").get().get()
45
project.dependencies {
49
val jarTask = tasks.getByName<Jar>(jarTaskName)
51
jarTask.extensions.findByType() ?: jarTask.extensions.create(
52
BundleTaskExtension.NAME,
53
BundleTaskExtension::class.java,
57
setClasspath(osgi.compileClasspath + sourceSets["main"].compileClasspath)
60
// Call the convention when the task has finished, to modify the jar to contain OSGi metadata.
62
bundleExtension.buildAction().execute(this)
66
val Project.sourceSets: SourceSetContainer
67
get() = (this as ExtensionAware).extensions["sourceSets"] as SourceSetContainer