Skip to content

Commit

Permalink
Add limitModules configuration property
Browse files Browse the repository at this point in the history
  • Loading branch information
iherasymenko committed Nov 27, 2023
1 parent d70e4c9 commit 5f32c97
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ jlinkApplication {
launcher = [
'another-demo-application': 'demo.main/com.example.demo.AnotherDemoApplication'
]
limitModules = ['com.zaxxer.hikari']
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void apply(Project project) {
task.getIncludeLocales().convention(jlinkApplication.getIncludeLocales());
task.getStripJavaDebugAttributes().convention(jlinkApplication.getStripJavaDebugAttributes());
task.getStripNativeCommands().convention(jlinkApplication.getStripNativeCommands());
task.getLimitModules().convention(jlinkApplication.getLimitModules());
};

TaskProvider<JlinkImageTask> imageTask = tasks.register("image", JlinkImageTask.class, task -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ public abstract class JlinkApplicationPluginExtension {

public abstract MapProperty<String, String> getLauncher();

public abstract ListProperty<String> getLimitModules();

}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ public JlinkImageTask() {
@Optional
public abstract Property<Boolean> getStripNativeCommands();

@Input
public abstract ListProperty<String> getLimitModules();

@Inject
protected abstract FileSystemOperations getFileSystemOperations();

Expand Down Expand Up @@ -212,6 +215,10 @@ public void execute() throws IOException {
if (getStripNativeCommands().getOrElse(false)) {
args.add("--strip-native-commands");
}
String limitModules = String.join(",", getLimitModules().get());
if (!limitModules.isEmpty()) {
args.addAll(List.of("--limit-modules", limitModules));
}
getFileSystemOperations().delete(spec -> spec.delete(getOutputFolder().get()));
invokeJlink(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.github.iherasymenko.jlink.test;

import com.github.iherasymenko.jlink.test.fixtures.Text;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.gradle.testkit.runner.BuildResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -141,6 +142,50 @@ implementation platform('org.slf4j:slf4j-bom:2.0.9')
assertThat(taskOutput[11]).isEqualTo("[email protected]");
}

@Test
void can_limit_module_universe() throws IOException {
build.buildFile = """
plugins {
id 'application'
id 'com.github.iherasymenko.jlink'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(System.getenv().getOrDefault('TESTING_AGAINST_JDK', '21'))
vendor = JvmVendorSpec.AZUL
}
}
application {
mainClass = 'com.example.demo.DemoApplication'
mainModule = 'demo.main'
}
jlinkApplication {
limitModules = ['com.zaxxer.hikari']
}
dependencies {
implementation platform('org.slf4j:slf4j-bom:2.0.9')
implementation 'com.zaxxer:HikariCP:5.1.0'
}
""";
build.moduleInfo = """
module demo.main {
requires com.zaxxer.hikari;
}
""";
BuildResult buildResult = build.runner("image")
.buildAndFail();
assertThat(buildResult)
.extracting(BuildResult::getOutput, InstanceOfAssertFactories.STRING)
.containsPattern("Module (.*) not found, required by com\\.zaxxer\\.hikari");
}

@Test
void can_list_modules_in_an_image_with_third_party_modules() throws IOException {
build.buildFile = """
Expand Down

0 comments on commit 5f32c97

Please sign in to comment.