Skip to content

Commit 211d4ab

Browse files
committed
Add includeLocales configuration property
1 parent 904223d commit 211d4ab

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ jlinkApplication {
6868
excludeFiles = ['/**/legal/**', '/**/man/**']
6969
excludeJmodSection = ['man', 'headers']
7070
excludeResources = ['/**/com/example/demo/DemoApplication.class']
71+
includeLocales = ['en-CA']
7172
}
7273
7374
```

src/main/java/com/github/iherasymenko/jlink/JlinkApplicationPlugin.java

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public void apply(Project project) {
8080
task.getExcludeFiles().convention(jlinkApplication.getExcludeFiles());
8181
task.getExcludeJmodSection().convention(jlinkApplication.getExcludeJmodSection());
8282
task.getExcludeResources().convention(jlinkApplication.getExcludeResources());
83+
task.getIncludeLocales().convention(jlinkApplication.getIncludeLocales());
8384
};
8485

8586
TaskProvider<JlinkImageTask> imageTask = tasks.register("image", JlinkImageTask.class, task -> {

src/main/java/com/github/iherasymenko/jlink/JlinkApplicationPluginExtension.java

+2
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ public abstract class JlinkApplicationPluginExtension {
5252

5353
public abstract ListProperty<String> getExcludeResources();
5454

55+
public abstract ListProperty<String> getIncludeLocales();
56+
5557
}

src/main/java/com/github/iherasymenko/jlink/JlinkImageTask.java

+7
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public JlinkImageTask() {
125125
@Input
126126
public abstract ListProperty<String> getExcludeResources();
127127

128+
@Input
129+
public abstract ListProperty<String> getIncludeLocales();
130+
128131
@Inject
129132
protected abstract FileSystemOperations getFileSystemOperations();
130133

@@ -194,6 +197,10 @@ public void execute() throws IOException {
194197
if (!excludeResources.isEmpty()) {
195198
args.addAll(List.of("--exclude-resources", excludeResources));
196199
}
200+
String includeLocales = String.join(",", getIncludeLocales().get());
201+
if (!includeLocales.isEmpty()) {
202+
args.addAll(List.of("--include-locales", includeLocales));
203+
}
197204
getFileSystemOperations().delete(spec -> spec.delete(getOutputFolder().get()));
198205
invokeJlink(args);
199206
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Copyright 2023 Ihor Herasymenko.
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
package com.github.iherasymenko.jlink.test;
17+
18+
import com.github.iherasymenko.jlink.test.fixtures.Text;
19+
import org.assertj.core.api.InstanceOfAssertFactories;
20+
import org.gradle.testkit.runner.BuildResult;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.io.IOException;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
class IncludeLocalesPluginFunctionalTest extends AbstractTestBase {
28+
29+
@Test
30+
void can_include_locales() throws IOException {
31+
build.buildFile = """
32+
plugins {
33+
id 'java'
34+
id 'com.github.iherasymenko.jlink'
35+
}
36+
37+
group = 'com.example'
38+
version = '0.0.1-SNAPSHOT'
39+
40+
java {
41+
toolchain {
42+
languageVersion = JavaLanguageVersion.of(System.getenv().getOrDefault('TESTING_AGAINST_JDK', '21'))
43+
vendor = JvmVendorSpec.AZUL
44+
}
45+
}
46+
47+
jlinkApplication {
48+
mainClass = 'com.example.demo.DemoApplication'
49+
mainModule = 'demo.main'
50+
includeLocales = ['en-CA']
51+
}
52+
""";
53+
build.settingsFile = """
54+
rootProject.name = 'demo'
55+
dependencyResolutionManagement {
56+
repositories {
57+
mavenCentral()
58+
}
59+
}
60+
""";
61+
build.mainClass = """
62+
package com.example.demo;
63+
64+
import java.util.Locale;
65+
66+
public class DemoApplication {
67+
public static void main(String[] args) {
68+
for (Locale locale : Locale.getAvailableLocales()) {
69+
System.out.println(locale);
70+
}
71+
}
72+
}
73+
""";
74+
build.moduleInfo = """
75+
module demo.main {
76+
requires jdk.localedata;
77+
}
78+
""";
79+
80+
BuildResult buildResult = build.runner("imageRun").build();
81+
82+
String[] taskOutput = Text.linesBetweenTags(buildResult.getOutput(), "> Task :imageRun", "BUILD SUCCESSFUL");
83+
assertThat(taskOutput)
84+
.hasSizeLessThan(10)
85+
.contains("en", "en_CA", "en_US", "en_US_POSIX");
86+
}
87+
88+
@Test
89+
void many_locales_are_available_by_default() throws IOException {
90+
build.buildFile = """
91+
plugins {
92+
id 'java'
93+
id 'com.github.iherasymenko.jlink'
94+
}
95+
96+
group = 'com.example'
97+
version = '0.0.1-SNAPSHOT'
98+
99+
java {
100+
toolchain {
101+
languageVersion = JavaLanguageVersion.of(System.getenv().getOrDefault('TESTING_AGAINST_JDK', '21'))
102+
vendor = JvmVendorSpec.AZUL
103+
}
104+
}
105+
106+
jlinkApplication {
107+
mainClass = 'com.example.demo.DemoApplication'
108+
mainModule = 'demo.main'
109+
}
110+
""";
111+
build.settingsFile = """
112+
rootProject.name = 'demo'
113+
dependencyResolutionManagement {
114+
repositories {
115+
mavenCentral()
116+
}
117+
}
118+
""";
119+
build.mainClass = """
120+
package com.example.demo;
121+
122+
import java.util.Arrays;
123+
import java.util.Locale;
124+
import java.util.Comparator;
125+
126+
public class DemoApplication {
127+
public static void main(String[] args) {
128+
Locale[] availableLocales = Locale.getAvailableLocales();
129+
Arrays.sort(availableLocales, Comparator.comparing(Locale::toString));
130+
for (Locale locale : availableLocales) {
131+
System.out.println(locale);
132+
}
133+
}
134+
}
135+
""";
136+
build.moduleInfo = """
137+
module demo.main {
138+
requires jdk.localedata;
139+
}
140+
""";
141+
142+
BuildResult buildResult = build.runner("imageRun").build();
143+
144+
String[] taskOutput = Text.linesBetweenTags(buildResult.getOutput(), "> Task :imageRun", "BUILD SUCCESSFUL");
145+
assertThat(taskOutput)
146+
.hasSizeGreaterThan(100)
147+
.contains("uk_UA", "en_CA", "fr_CA");
148+
}
149+
150+
}

0 commit comments

Comments
 (0)