Skip to content

Commit

Permalink
[nativeImage] support platform specific settings. Resolves #789
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Mar 4, 2022
1 parent c94a867 commit 258e305
Show file tree
Hide file tree
Showing 6 changed files with 384 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ private Artifact nativeImage(Path assembleDirectory, Path graalPath, Set<Path> j
.toAbsolutePath();

Command cmd = new Command(nativeImageExecutable.toString(), true)
.args(assembler.getArgs())
.arg("-jar")
.args(assembler.getArgs());

NativeImage.PlatformCustomizer customizer = assembler.getResolvedPlatformCustomizer();
cmd.args(customizer.getArgs());

cmd.arg("-jar")
.arg(maybeQuote(assembler.getMainJar().getEffectivePath(context, assembler).toAbsolutePath().toString()));

if (!jars.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.jreleaser.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.jreleaser.util.PlatformUtils;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -42,6 +43,9 @@ public class NativeImage extends AbstractJavaAssembler {
private final Artifact graal = new Artifact();
private final Set<Artifact> graalJdks = new LinkedHashSet<>();
private final Upx upx = new Upx();
private final Linux linux = new Linux();
private final Windows windows = new Windows();
private final Osx osx = new Osx();

private String imageName;
private String imageNameTransform;
Expand All @@ -65,6 +69,9 @@ void setAll(NativeImage nativeImage) {
setGraalJdks(nativeImage.graalJdks);
setArgs(nativeImage.args);
setUpx(nativeImage.upx);
setLinux(nativeImage.linux);
setWindows(nativeImage.windows);
setOsx(nativeImage.osx);
}

public String getResolvedImageName(JReleaserContext context) {
Expand All @@ -80,6 +87,16 @@ public String getResolvedImageNameTransform(JReleaserContext context) {
return resolveTemplate(imageNameTransform, props);
}

public PlatformCustomizer getResolvedPlatformCustomizer() {
String currentPlatform = PlatformUtils.getCurrentFull();
if (PlatformUtils.isMac(currentPlatform)) {
return getOsx();
} else if (PlatformUtils.isWindows(currentPlatform)) {
return getWindows();
}
return getLinux();
}

public String getImageName() {
return imageName;
}
Expand Down Expand Up @@ -168,6 +185,30 @@ public void setUpx(Upx upx) {
this.upx.setAll(upx);
}

public Linux getLinux() {
return linux;
}

public void setLinux(Linux linux) {
this.linux.setAll(linux);
}

public Windows getWindows() {
return windows;
}

public void setWindows(Windows windows) {
this.windows.setAll(windows);
}

public Osx getOsx() {
return osx;
}

public void setOsx(Osx osx) {
this.osx.setAll(osx);
}

@Override
protected void asMap(boolean full, Map<String, Object> props) {
super.asMap(full, props);
Expand All @@ -183,6 +224,19 @@ protected void asMap(boolean full, Map<String, Object> props) {
props.put("graalJdks", mappedJdks);
props.put("args", args);
props.put("upx", upx.asMap(full));
props.putAll(linux.asMap(full));
props.putAll(osx.asMap(full));
props.putAll(windows.asMap(full));
}

public interface PlatformCustomizer extends Domain {
String getPlatform();

List<String> getArgs();

void setArgs(List<String> args);

void addArgs(List<String> args);
}

public static class Upx implements Domain, Activatable {
Expand Down Expand Up @@ -277,4 +331,63 @@ public Map<String, Object> asMap(boolean full) {
return props;
}
}

private static abstract class AbstractPlatformCustomizer implements PlatformCustomizer {
private final List<String> args = new ArrayList<>();
private final String platform;

protected AbstractPlatformCustomizer(String platform) {
this.platform = platform;
}

void setAll(AbstractPlatformCustomizer customizer) {
setArgs(customizer.args);
}

public List<String> getArgs() {
return args;
}

public void setArgs(List<String> args) {
this.args.clear();
this.args.addAll(args);
}

public void addArgs(List<String> args) {
this.args.addAll(args);
}

@Override
public String getPlatform() {
return platform;
}

@Override
public Map<String, Object> asMap(boolean full) {
Map<String, Object> props = new LinkedHashMap<>();
props.put("args", args);

Map<String, Object> map = new LinkedHashMap<>();
map.put(platform, props);
return map;
}
}

public static class Linux extends AbstractPlatformCustomizer {
public Linux() {
super("linux");
}
}

public static class Windows extends AbstractPlatformCustomizer {
public Windows() {
super("windows");
}
}

public static class Osx extends AbstractPlatformCustomizer {
public Osx() {
super("osx");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,24 @@ interface NativeImage extends JavaAssembler {

void upx(Action<? super Upx> action)

void linux(Action<? super Linux> action)

void windows(Action<? super Windows> action)

void osx(Action<? super Osx> action)

void graalJdk(Action<? super Artifact> action)

void graal(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Artifact) Closure<Void> action)

void upx(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Upx) Closure<Void> action)

void linux(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Linux) Closure<Void> action)

void windows(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Windows) Closure<Void> action)

void osx(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Osx) Closure<Void> action)

void graalJdk(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Artifact) Closure<Void> action)

interface Upx extends Activatable {
Expand All @@ -66,4 +78,22 @@ interface NativeImage extends JavaAssembler {

void arg(String arg)
}

interface Linux {
ListProperty<String> getArgs()

void arg(String arg)
}

interface Windows {
ListProperty<String> getArgs()

void arg(String arg)
}

interface Osx {
ListProperty<String> getArgs()

void arg(String arg)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class NativeImageImpl extends AbstractJavaAssembler implements NativeImage {

private final ArtifactImpl graal
private final UpxImpl upx
private final LinuxImpl linux
private final WindowsImpl windows
private final OsxImpl osx
final NamedDomainObjectContainer<ArtifactImpl> graalJdks

@Inject
Expand All @@ -69,6 +72,9 @@ class NativeImageImpl extends AbstractJavaAssembler implements NativeImage {
graal = objects.newInstance(ArtifactImpl, objects)
graal.setName('graal')
upx = objects.newInstance(UpxImpl, objects)
linux = objects.newInstance(LinuxImpl, objects)
windows = objects.newInstance(WindowsImpl, objects)
osx = objects.newInstance(OsxImpl, objects)

graalJdks = objects.domainObjectContainer(ArtifactImpl, new NamedDomainObjectFactory<ArtifactImpl>() {
@Override
Expand Down Expand Up @@ -96,6 +102,9 @@ class NativeImageImpl extends AbstractJavaAssembler implements NativeImage {
java.isSet() ||
graal.isSet() ||
upx.isSet() ||
linux.isSet() ||
windows.isSet() ||
osx.isSet() ||
!graalJdks.isEmpty()
}

Expand Down Expand Up @@ -123,6 +132,21 @@ class NativeImageImpl extends AbstractJavaAssembler implements NativeImage {
action.execute(upx)
}

@Override
void linux(Action<? super Linux> action) {
action.execute(linux)
}

@Override
void windows(Action<? super Windows> action) {
action.execute(windows)
}

@Override
void osx(Action<? super Osx> action) {
action.execute(osx)
}

@Override
void graalJdk(Action<? super Artifact> action) {
action.execute(graalJdks.maybeCreate("graalJdk-${graalJdks.size()}".toString()))
Expand All @@ -138,6 +162,21 @@ class NativeImageImpl extends AbstractJavaAssembler implements NativeImage {
ConfigureUtil.configure(action, upx)
}

@Override
void linux(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Upx) Closure<Void> action) {
ConfigureUtil.configure(action, linux)
}

@Override
void windows(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Upx) Closure<Void> action) {
ConfigureUtil.configure(action, windows)
}

@Override
void osx(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Upx) Closure<Void> action) {
ConfigureUtil.configure(action, osx)
}

@Override
void graalJdk(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Artifact) Closure<Void> action) {
ConfigureUtil.configure(action, graalJdks.maybeCreate("graalJdk-${graalJdks.size()}".toString()))
Expand All @@ -162,6 +201,9 @@ class NativeImageImpl extends AbstractJavaAssembler implements NativeImage {
nativeImage.args = (List<String>) args.getOrElse([])
if (graal.isSet()) nativeImage.graal = graal.toModel()
if (upx.isSet()) nativeImage.upx = upx.toModel()
if (linux.isSet()) nativeImage.linux = linux.toModel()
if (windows.isSet()) nativeImage.windows = windows.toModel()
if (osx.isSet()) nativeImage.osx = osx.toModel()
for (ArtifactImpl artifact : graalJdks) {
nativeImage.addGraalJdk(artifact.toModel())
}
Expand Down Expand Up @@ -217,4 +259,88 @@ class NativeImageImpl extends AbstractJavaAssembler implements NativeImage {
upx
}
}

@CompileStatic
static class LinuxImpl implements NativeImage.Linux {
final ListProperty<String> args

@Inject
LinuxImpl(ObjectFactory objects) {
args = objects.listProperty(String).convention(Providers.notDefined())
}

@Override
void arg(String arg) {
if (isNotBlank(arg)) {
args.add(arg.trim())
}
}

@Internal
boolean isSet() {
args.present
}

org.jreleaser.model.NativeImage.Linux toModel() {
org.jreleaser.model.NativeImage.Linux linux = new org.jreleaser.model.NativeImage.Linux()
linux.args = (List<String>) args.getOrElse([])
linux
}
}

@CompileStatic
static class WindowsImpl implements NativeImage.Windows {
final ListProperty<String> args

@Inject
WindowsImpl(ObjectFactory objects) {
args = objects.listProperty(String).convention(Providers.notDefined())
}

@Override
void arg(String arg) {
if (isNotBlank(arg)) {
args.add(arg.trim())
}
}

@Internal
boolean isSet() {
args.present
}

org.jreleaser.model.NativeImage.Windows toModel() {
org.jreleaser.model.NativeImage.Windows windows = new org.jreleaser.model.NativeImage.Windows()
windows.args = (List<String>) args.getOrElse([])
windows
}
}

@CompileStatic
static class OsxImpl implements NativeImage.Osx {
final ListProperty<String> args

@Inject
OsxImpl(ObjectFactory objects) {
args = objects.listProperty(String).convention(Providers.notDefined())
}

@Override
void arg(String arg) {
if (isNotBlank(arg)) {
args.add(arg.trim())
}
}

@Internal
boolean isSet() {
args.present
}

org.jreleaser.model.NativeImage.Osx toModel() {
org.jreleaser.model.NativeImage.Osx osx = new org.jreleaser.model.NativeImage.Osx()
osx.args = (List<String>) args.getOrElse([])
osx
}
}
}

0 comments on commit 258e305

Please sign in to comment.