Skip to content

Commit 8448534

Browse files
committed
Extract OSGi setup into separate build script
1 parent 692b606 commit 8448534

File tree

2 files changed

+107
-103
lines changed

2 files changed

+107
-103
lines changed

buildSrc/src/main/kotlin/java-library-conventions.gradle.kts

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import aQute.bnd.gradle.BundleTaskConvention
2-
import aQute.bnd.gradle.FileSetRepositoryConvention
3-
import aQute.bnd.gradle.Resolve
4-
51
plugins {
62
`java-library`
73
eclipse
@@ -83,6 +79,7 @@ tasks.checkstyleMain {
8379
if (project in mavenizedProjects) {
8480

8581
apply(plugin = "publishing-conventions")
82+
apply(plugin = "osgi-conventions")
8683

8784
java {
8885
withJavadocJar()
@@ -157,105 +154,6 @@ if (project in mavenizedProjects) {
157154
}
158155
}
159156

160-
// This task enances `jar` and `shadowJar` tasks with the bnd
161-
// `BundleTaskConvention` convention which allows for generating OSGi
162-
// metadata into the jar
163-
tasks.withType<Jar>().matching {
164-
task: Jar -> task.name == "jar" || task.name == "shadowJar"
165-
}.configureEach {
166-
val btc = BundleTaskConvention(this)
167-
168-
// These are bnd instructions necessary for generating OSGi metadata.
169-
// We've generalized these so that they are widely applicable limiting
170-
// module configurations to special cases.
171-
btc.setBnd("""
172-
# These are the general rules for package imports.
173-
Import-Package: \
174-
!org.apiguardian.api,\
175-
org.junit.platform.commons.logging;status=INTERNAL,\
176-
kotlin.*;resolution:="optional",\
177-
*
178-
179-
# This tells bnd not to complain if a module doesn't actually import
180-
# the kotlin packages, but enough modules do to make it a default.
181-
-fixupmessages.kotlin.import: "Unused Import-Package instructions: \\[kotlin.*\\]";is:=ignore
182-
183-
# This tells bnd to ignore classes it files in `META-INF/versions/`
184-
# because bnd doesn't yet support multi-release jars.
185-
-fixupmessages.wrong.dir: "Classes found in the wrong directory: \\{META-INF/versions/...";is:=ignore
186-
187-
# Don't scan for Class.forName package imports.
188-
# See https://bnd.bndtools.org/instructions/noclassforname.html
189-
-noclassforname: true
190-
191-
# Don't add all the extra headers bnd normally adds.
192-
# See https://bnd.bndtools.org/instructions/noextraheaders.html
193-
-noextraheaders: true
194-
195-
# Don't add the Private-Package header.
196-
# See https://bnd.bndtools.org/instructions/removeheaders.html
197-
-removeheaders: Private-Package
198-
199-
# Add the custom buildSrc/src/main/kotlin/APIGuardianAnnotations.kt
200-
# plugin to bnd
201-
-plugin.apiguardian.annotations: ${APIGuardianAnnotations::class.qualifiedName}
202-
203-
# Instruct the APIGuardianAnnotations how to operate.
204-
# See https://bnd.bndtools.org/instructions/export-apiguardian.html
205-
-export-apiguardian: *;version=${project.version}
206-
""")
207-
208-
// Add the convention to the jar task
209-
convention.plugins.put("bundle", btc)
210-
211-
doLast {
212-
// Do the actual work putting OSGi stuff in the jar.
213-
btc.buildBundle()
214-
}
215-
216-
finalizedBy("verifyOSGi")
217-
}
218-
219-
// Bnd's Resolve task uses a properties file for it's configuration. This
220-
// task writes out the properties necessary for it to verify the OSGi
221-
// metadata.
222-
tasks.register<WriteProperties>("verifyOSGiProperties") {
223-
setOutputFile("${buildDir}/verifyOSGiProperties.bndrun")
224-
property("-standalone", "true")
225-
property("-runee", "JavaSE-${Versions.jvmTarget}")
226-
property("-runrequires", "osgi.identity;filter:='(osgi.identity=${project.name})'")
227-
property("-runsystempackages", "jdk.internal.misc,sun.misc")
228-
}
229-
230-
// Bnd's Resolve task is what verifies that a jar can be used in OSGi and
231-
// that it's metadata is valid. If the metadata is invalid this task will
232-
// fail.
233-
tasks.register<Resolve>("verifyOSGi") {
234-
dependsOn("verifyOSGiProperties")
235-
setBndrun("${buildDir}/verifyOSGiProperties.bndrun")
236-
setReportOptional(false)
237-
withConvention(FileSetRepositoryConvention::class) {
238-
239-
// By default bnd will use jars found in:
240-
// 1. project.sourceSets.main.runtimeClasspath
241-
// 2. project.configurations.archives.artifacts.files
242-
// to validate the metadata.
243-
// This adds jars defined in `testRuntimeClasses` also so that bnd
244-
// can use them to validate the metadata without causing those to
245-
// end up in the dependencies of those projects.
246-
bundles(sourceSets["test"].runtimeClasspath)
247-
}
248-
}
249-
250-
// The ${project.description}, for some odd reason, is only available
251-
// afterEvaluate.
252-
afterEvaluate {
253-
tasks.withType<Jar>().configureEach {
254-
convention.findPlugin(BundleTaskConvention::class.java)
255-
?.bnd("Bundle-Name: ${project.description}")
256-
}
257-
}
258-
259157
} else {
260158
tasks {
261159
jar {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import aQute.bnd.gradle.BundleTaskConvention
2+
import aQute.bnd.gradle.FileSetRepositoryConvention
3+
import aQute.bnd.gradle.Resolve
4+
5+
plugins {
6+
`java-library`
7+
}
8+
9+
// This task enhances `jar` and `shadowJar` tasks with the bnd
10+
// `BundleTaskConvention` convention which allows for generating OSGi
11+
// metadata into the jar
12+
tasks.withType<Jar>().matching {
13+
task: Jar -> task.name == "jar" || task.name == "shadowJar"
14+
}.configureEach {
15+
val btc = BundleTaskConvention(this)
16+
17+
// These are bnd instructions necessary for generating OSGi metadata.
18+
// We've generalized these so that they are widely applicable limiting
19+
// module configurations to special cases.
20+
btc.setBnd("""
21+
# These are the general rules for package imports.
22+
Import-Package: \
23+
!org.apiguardian.api,\
24+
org.junit.platform.commons.logging;status=INTERNAL,\
25+
kotlin.*;resolution:="optional",\
26+
*
27+
28+
# This tells bnd not to complain if a module doesn't actually import
29+
# the kotlin packages, but enough modules do to make it a default.
30+
-fixupmessages.kotlin.import: "Unused Import-Package instructions: \\[kotlin.*\\]";is:=ignore
31+
32+
# This tells bnd to ignore classes it files in `META-INF/versions/`
33+
# because bnd doesn't yet support multi-release jars.
34+
-fixupmessages.wrong.dir: "Classes found in the wrong directory: \\{META-INF/versions/...";is:=ignore
35+
36+
# Don't scan for Class.forName package imports.
37+
# See https://bnd.bndtools.org/instructions/noclassforname.html
38+
-noclassforname: true
39+
40+
# Don't add all the extra headers bnd normally adds.
41+
# See https://bnd.bndtools.org/instructions/noextraheaders.html
42+
-noextraheaders: true
43+
44+
# Don't add the Private-Package header.
45+
# See https://bnd.bndtools.org/instructions/removeheaders.html
46+
-removeheaders: Private-Package
47+
48+
# Add the custom buildSrc/src/main/kotlin/APIGuardianAnnotations.kt
49+
# plugin to bnd
50+
-plugin.apiguardian.annotations: ${APIGuardianAnnotations::class.qualifiedName}
51+
52+
# Instruct the APIGuardianAnnotations how to operate.
53+
# See https://bnd.bndtools.org/instructions/export-apiguardian.html
54+
-export-apiguardian: *;version=${project.version}
55+
""")
56+
57+
// Add the convention to the jar task
58+
convention.plugins["bundle"] = btc
59+
60+
doLast {
61+
// Do the actual work putting OSGi stuff in the jar.
62+
btc.buildBundle()
63+
}
64+
65+
finalizedBy("verifyOSGi")
66+
}
67+
68+
// Bnd's Resolve task uses a properties file for it's configuration. This
69+
// task writes out the properties necessary for it to verify the OSGi
70+
// metadata.
71+
tasks.register<WriteProperties>("verifyOSGiProperties") {
72+
setOutputFile("${buildDir}/verifyOSGiProperties.bndrun")
73+
property("-standalone", "true")
74+
property("-runee", "JavaSE-${Versions.jvmTarget}")
75+
property("-runrequires", "osgi.identity;filter:='(osgi.identity=${project.name})'")
76+
property("-runsystempackages", "jdk.internal.misc,sun.misc")
77+
}
78+
79+
// Bnd's Resolve task is what verifies that a jar can be used in OSGi and
80+
// that it's metadata is valid. If the metadata is invalid this task will
81+
// fail.
82+
tasks.register<Resolve>("verifyOSGi") {
83+
dependsOn("verifyOSGiProperties")
84+
setBndrun("${buildDir}/verifyOSGiProperties.bndrun")
85+
isReportOptional = false
86+
withConvention(FileSetRepositoryConvention::class) {
87+
88+
// By default bnd will use jars found in:
89+
// 1. project.sourceSets.main.runtimeClasspath
90+
// 2. project.configurations.archives.artifacts.files
91+
// to validate the metadata.
92+
// This adds jars defined in `testRuntimeClasses` also so that bnd
93+
// can use them to validate the metadata without causing those to
94+
// end up in the dependencies of those projects.
95+
bundles(sourceSets["test"].runtimeClasspath)
96+
}
97+
}
98+
99+
// The ${project.description}, for some odd reason, is only available
100+
// afterEvaluate.
101+
afterEvaluate {
102+
tasks.withType<Jar>().configureEach {
103+
convention.findPlugin(BundleTaskConvention::class.java)
104+
?.bnd("Bundle-Name: ${project.description}")
105+
}
106+
}

0 commit comments

Comments
 (0)