Skip to content

Commit 0ad3dc6

Browse files
authored
Make the plugin work (#4)
Makes the plugin functionally usable and provide sample usages
1 parent ef4cd6c commit 0ad3dc6

File tree

33 files changed

+1246
-181
lines changed

33 files changed

+1246
-181
lines changed

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: java
2+
jdk:
3+
- openjdk10
4+
- openjdk11
5+
6+
script: ./gradlew check --console=plain --stacktrace

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# simple-jlink
2+
3+
A simple Gradle plugin for generating native Java runtime images with the jlink tool introduced in Java 9.
4+
Your applications can be modular, unmodular, and use modular or unomdular libraries. The
5+
plugin will automatically determine which modules to include in the generated image.
6+
7+
Note: this plugin must be run on JDK 10 or higher. JDK 9 is not supported.
8+
9+
See [the jlink Reference Page](https://docs.oracle.com/javase/10/tools/jlink.htm) for details on the
10+
jlink tool and its options, most of which are exposed to plugin users.
11+
12+
```kotlin
13+
plugins {
14+
application
15+
id("org.gradleweaver.plugins.jlink")
16+
}
17+
18+
jlink {
19+
"configuration name" {
20+
applicationJar.set(provider { tasks.getByName<Jar>("jar").archivePath }) // No default value
21+
22+
// jlink executable options. See the reference page for details
23+
bindServices.set(false)
24+
compressionLevel.set(JLinkTask.CompressionLevel.NONE)
25+
endianess.set(JLinkTask.Endianness.SYSTEM_DEFAULT)
26+
ignoreSigningInformation.set(false)
27+
excludeHeaderFiles.set(false)
28+
excludeManPages.set(false)
29+
stripDebug.set(false)
30+
optimizeClassForName.set(false)
31+
32+
// Extra modules to link that are not discovered by jdeps
33+
extraModules.addAll("module1", "module2", ...)
34+
35+
// Single method to exclude header files, man pages, and debug symbols
36+
// Also sets the compression level to ZIP (maximum)
37+
useMinimalImage()
38+
39+
// Generate a launcher script
40+
launcher {
41+
launcherName.set("LauncherName")
42+
vmOptions.addAll("-Xms512m", "-Xmx4G")
43+
44+
// Only required if the app and all its dependencies are modular
45+
applicationModuleName.set("com.example.app")
46+
mainClassName.set("com.example.app.Main")
47+
}
48+
}
49+
}
50+
```
51+
52+
See the [samples](samples) directory for more examples on the jlink plugin in action.
53+
54+
## Unavailable Options
55+
56+
`--disable-plugin pluginname`
57+
`--limit-modules`
58+
`--save-opts`
59+
`@filename`

build.gradle.kts

+13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import org.gradleweaver.plugins.jlink.JLinkTask
2+
13
buildscript {
24
dependencies {
35
classpath("org.gradleweaver.plugins:simple-jlink:+")
46
}
57
}
8+
plugins {
9+
`lifecycle-base`
10+
}
611

712
allprojects {
813
repositories {
@@ -16,4 +21,12 @@ project(":samples") {
1621
}
1722
}
1823

24+
tasks.named<Task>("check") {
25+
dependsOn(gradle.includedBuild("simple-jlink").task(":check"))
26+
dependsOn(findProject(":samples:javafx-app")!!.tasks.withType<JLinkTask>())
27+
dependsOn(findProject(":samples:simple-jar")!!.tasks.withType<JLinkTask>())
28+
dependsOn(findProject(":samples:groovy-dsl")!!.tasks.withType<JLinkTask>())
29+
dependsOn(findProject(":samples:modular-app")!!.tasks.withType<JLinkTask>())
30+
}
31+
1932
apply(from = "gradle/wrapper.gradle.kts")

plugin/build.gradle.kts

+11-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
// Makes sure to update the kotlin version below in the test resources as well.
88
kotlin("jvm") version "1.2.51"
99
`maven-publish`
10-
id ("com.gradle.plugin-publish") version "0.9.10"
10+
id("com.gradle.plugin-publish") version "0.9.10"
1111
}
1212

1313
group = "org.gradleweaver.plugins"
@@ -20,7 +20,6 @@ repositories {
2020

2121
dependencies {
2222
compileOnly(gradleApi())
23-
compile(kotlin("stdlib-jdk8"))
2423
testCompileOnly(gradleTestKit())
2524

2625
fun junitJupiter(name: String, version: String = "5.2.0") =
@@ -49,39 +48,33 @@ publishing {
4948
}
5049

5150

52-
configure<JavaPluginConvention> {
51+
java {
5352
sourceCompatibility = JavaVersion.VERSION_1_8
5453
}
5554

56-
tasks.withType<KotlinCompile> {
55+
tasks.withType<KotlinCompile>().configureEach {
5756
kotlinOptions.jvmTarget = "1.8"
5857
}
5958

60-
tasks.withType<Test> {
59+
tasks.withType<Test>().configureEach {
6160
useJUnitPlatform()
6261
}
6362

64-
gradlePlugin {
65-
plugins {
66-
register("jlink-plugin") {
67-
id = "${project.group}.${project.name}"
68-
implementationClass = "org.gradleweaver.plugins.jlink.JLinkPlugin"
69-
description = "A Gradle plugin for handling platform-specific dependencies and releases."
70-
}
71-
}
63+
val registeredPlugin = gradlePlugin.plugins.register("jlink-plugin") {
64+
id = "${project.group}.${project.name}"
65+
implementationClass = "org.gradleweaver.plugins.jlink.JLinkPlugin"
66+
description = "A Gradle plugin for handling platform-specific dependencies and releases."
7267
}
7368

7469
pluginBundle {
75-
val plugin = gradlePlugin.plugins["jlink-plugin"]
76-
7770
website = "https://github.com/gradleweaver/jlink-plugin"
7871
vcsUrl = "https://github.com/gradleweaver/jlink-plugin"
7972
tags = listOf("jlink")
8073

8174
plugins {
8275
create("jlink-plugin") {
83-
id = plugin.id
84-
displayName = plugin.displayName
76+
id = registeredPlugin.get().id
77+
displayName = registeredPlugin.get().displayName
8578
}
8679
}
8780
}
@@ -94,7 +87,7 @@ tasks {
9487
val writeTestProperties by creating(WriteProperties::class) {
9588
outputFile = processTestResources.destinationDir.resolve("test.properties")
9689
property("version", version)
97-
property("kotlinVersion", "1.2.51")
90+
property("kotlinVersion", KotlinVersion.CURRENT)
9891
}
9992
processTestResources.dependsOn(writeTestProperties)
10093
"test" {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.gradleweaver.plugins.jlink
2+
3+
/**
4+
* Options for the levels of compression available for generated runtime images.
5+
*
6+
* @param jlinkValue the integer value corresponding to the compression level expected by `jlink`
7+
*/
8+
enum class CompressionLevel(val jlinkValue: Int) {
9+
/**
10+
* Do no compression on the generated image.
11+
*/
12+
NONE(0),
13+
14+
/**
15+
* Share constant string objects.
16+
*/
17+
CONSTANT_STRING_SHARING(1),
18+
19+
/**
20+
* ZIP compression on the generated image.
21+
*/
22+
ZIP(2)
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.gradleweaver.plugins.jlink
2+
3+
/**
4+
* Options for the available byte orders of generated runtime images.
5+
*/
6+
enum class Endianness {
7+
/**
8+
* Use the endianness of the build system.
9+
*/
10+
SYSTEM_DEFAULT,
11+
12+
/**
13+
* Force little-endian byte order in the generated image.
14+
*/
15+
LITTLE,
16+
17+
/**
18+
* Force big-endian byte order in the generated image.
19+
*/
20+
BIG
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.gradleweaver.plugins.jlink
2+
3+
import org.gradle.api.model.ObjectFactory
4+
import org.gradle.api.provider.ListProperty
5+
import org.gradle.api.provider.Property
6+
7+
internal inline fun <reified T : Any> ObjectFactory.property(): Property<T> = property(T::class.java)
8+
internal inline fun <reified T : Any> ObjectFactory.property(initialValue: T): Property<T> {
9+
val prop = property(T::class.java)
10+
prop.set(initialValue)
11+
return prop
12+
}
13+
14+
internal inline fun <reified T : Any> ObjectFactory.listProperty(): ListProperty<T> = listProperty(T::class.java)
15+
16+
internal val Property<*>.isNotPresent
17+
get() = !isPresent
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gradleweaver.plugins.jlink
22

3-
import org.gradle.api.NamedDomainObjectCollection
3+
import org.gradle.api.Action
4+
import org.gradle.api.NamedDomainObjectContainer
45
import org.gradle.api.Project
56
import javax.inject.Inject
67

@@ -9,7 +10,39 @@ open class JLinkExtension
910
internal constructor(
1011
project: Project
1112
) {
12-
val configure: NamedDomainObjectCollection<JLinkOptions> = project.container(JLinkOptions::class.java) { name ->
13-
JLinkOptions(name = name)
13+
val jlinkConfigurations: NamedDomainObjectContainer<JLinkOptions> = project.container(JLinkOptions::class.java) { name ->
14+
JLinkOptions(project = project, name = name)
1415
}
15-
}
16+
17+
/**
18+
* Registers a new jlink configuration.
19+
*
20+
* ```
21+
* jlink {
22+
* "example" {
23+
* // options...
24+
* }
25+
* }
26+
* ```
27+
*
28+
* is equivalent to
29+
* ```
30+
* jlink {
31+
* register("example") {
32+
* // options...
33+
* }
34+
* }
35+
* ```
36+
*/
37+
operator fun String.invoke(configurationAction: Action<in JLinkOptions>) = register(this, configurationAction)
38+
39+
/**
40+
* Registers a new jlink configuration.
41+
*
42+
* @param name the name of the configuration
43+
* @param configurationAction the action to use to set up the configuration
44+
*/
45+
fun register(name: String, configurationAction: Action<in JLinkOptions>)
46+
= jlinkConfigurations.register(name, configurationAction)
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.gradleweaver.plugins.jlink
2+
3+
import org.gradle.api.Project
4+
5+
class JLinkLauncherOptions(project: Project) {
6+
7+
private val objectFactory = project.objects
8+
9+
/**
10+
* Runtime options for the JVM.
11+
*/
12+
val vmOptions = objectFactory.listProperty<String>()
13+
14+
/**
15+
* The name of the generated launcher script. If not set, the launcher name will be the name of the
16+
* corresponding project.
17+
*/
18+
val launcherName = objectFactory.property(project.name)
19+
20+
// For Groovy DSL support
21+
fun setLauncherName(launcherName: String) {
22+
this.launcherName.set(launcherName)
23+
}
24+
25+
fun setVmOptions(options: Iterable<String>) {
26+
vmOptions.set(options.toList())
27+
}
28+
29+
}

0 commit comments

Comments
 (0)