Skip to content

Commit

Permalink
Add endian configuration property
Browse files Browse the repository at this point in the history
  • Loading branch information
iherasymenko committed Nov 28, 2023
1 parent 1d787c9 commit 813fda8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ jlinkApplication {
]
limitModules = ['com.zaxxer.hikari']
vm = 'server'
endian = 'little'
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void apply(Project project) {
task.getStripNativeCommands().convention(jlinkApplication.getStripNativeCommands());
task.getLimitModules().convention(jlinkApplication.getLimitModules());
task.getVm().convention(jlinkApplication.getVm());
task.getEndian().convention(jlinkApplication.getEndian());
};

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

public abstract Property<String> getVm();

public abstract Property<String> getEndian();

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public JlinkImageTask() {
@Optional
public abstract Property<String> getVm();

@Input
@Optional
public abstract Property<String> getEndian();

@Inject
protected abstract FileSystemOperations getFileSystemOperations();

Expand Down Expand Up @@ -225,6 +229,9 @@ public void execute() throws IOException {
if (getVm().isPresent()) {
args.addAll(List.of("--vm", getVm().get()));
}
if (getEndian().isPresent()) {
args.addAll(List.of("--endian", getEndian().get()));
}
getFileSystemOperations().delete(spec -> spec.delete(getOutput().get()));
RegularFile jlink = getJavaLauncher()
.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -440,4 +442,116 @@ public static void main(String[] args) {
);
}

@Test
void can_specify_byte_order_little_endian() throws IOException {
build.buildFile = """
plugins {
id 'java'
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
}
}
jlinkApplication {
mainClass = 'com.example.demo.DemoApplication'
mainModule = 'demo.main'
endian = 'little'
}
""";
build.settingsFile = """
rootProject.name = 'demo'
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}
""";
build.mainClass = """
package com.example.demo;
public class DemoApplication {
public static void main(String[] args) {
}
}
""";
build.moduleInfo = """
module demo.main {
}
""";

build.runner("image").build();

try (InputStream fis = Files.newInputStream(build.projectDir.resolve("build/images/demo/lib/modules"))) {
byte[] actual = fis.readNBytes(4);
assertThat(actual).isEqualTo(new byte[]{(byte) 0xDA, (byte) 0xDA, (byte) 0xFE, (byte) 0xCA});
}
}

@Test
void can_specify_byte_order_big_endian() throws IOException {
build.buildFile = """
plugins {
id 'java'
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
}
}
jlinkApplication {
mainClass = 'com.example.demo.DemoApplication'
mainModule = 'demo.main'
endian = 'big'
}
""";
build.settingsFile = """
rootProject.name = 'demo'
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}
""";
build.mainClass = """
package com.example.demo;
public class DemoApplication {
public static void main(String[] args) {
}
}
""";
build.moduleInfo = """
module demo.main {
}
""";

build.runner("image").build();

try (InputStream fis = Files.newInputStream(build.projectDir.resolve("build/images/demo/lib/modules"))) {
byte[] actual = fis.readNBytes(4);
assertThat(actual).isEqualTo(new byte[]{(byte) 0xCA, (byte) 0xFE, (byte) 0xDA, (byte) 0xDA});
}
}

}

0 comments on commit 813fda8

Please sign in to comment.