use of org.jreleaser.model.NativeImage in project jreleaser by jreleaser.
the class NativeImageValidator method validateNativeImage.
private static void validateNativeImage(JReleaserContext context, JReleaserContext.Mode mode, NativeImage nativeImage, Errors errors) {
context.getLogger().debug("nativeImage.{}", nativeImage.getName());
if (!nativeImage.isActiveSet()) {
nativeImage.setActive(Active.NEVER);
}
if (!nativeImage.resolveEnabled(context.getModel().getProject()))
return;
if (isBlank(nativeImage.getName())) {
errors.configuration(RB.$("validation_must_not_be_blank", "nativeImage.name"));
return;
}
if (null == nativeImage.getMainJar()) {
errors.configuration(RB.$("validation_is_null", "nativeImage." + nativeImage.getName() + ".mainJar"));
return;
}
Platform platform = nativeImage.getPlatform().merge(context.getModel().getPlatform());
nativeImage.setPlatform(platform);
if (isBlank(nativeImage.getExecutable())) {
nativeImage.setExecutable(nativeImage.getName());
}
if (isBlank(nativeImage.getImageName())) {
nativeImage.setImageName(nativeImage.getExecutable() + "-" + context.getModel().getProject().getResolvedVersion());
}
int i = 0;
for (Artifact graalJdk : nativeImage.getGraalJdks()) {
validateJdk(context, mode, nativeImage, graalJdk, i++, errors);
}
// validate jdks.platform is unique
Map<String, List<Artifact>> byPlatform = nativeImage.getGraalJdks().stream().collect(groupingBy(jdk -> isBlank(jdk.getPlatform()) ? "<nil>" : jdk.getPlatform()));
if (byPlatform.containsKey("<nil>")) {
errors.configuration(RB.$("validation_nativeimage_jdk_platform", nativeImage.getName()));
}
// check platforms
byPlatform.forEach((p, jdks) -> {
if (jdks.size() > 1) {
errors.configuration(RB.$("validation_nativeimage_jdk_multiple_platforms", nativeImage.getName(), p));
}
});
if (isBlank(nativeImage.getGraal().getPath())) {
String currentPlatform = PlatformUtils.getCurrentFull();
String javaHome = System.getProperty("java.home");
if (nativeImage.getGraalJdks().isEmpty()) {
if (isBlank(javaHome)) {
// Can only happen when running as native-image, fail for now
// TODO: native-image
errors.configuration(RB.$("validation_java_home_missing"));
return;
}
// Use current
nativeImage.getGraal().setPath(javaHome);
nativeImage.getGraal().setPlatform(currentPlatform);
} else {
// find a compatible JDK in targets
Optional<Artifact> jdk = nativeImage.getGraalJdks().stream().filter(j -> PlatformUtils.isCompatible(currentPlatform, j.getPlatform())).findFirst();
if (jdk.isPresent()) {
nativeImage.setGraal(jdk.get());
} else {
if (isBlank(javaHome)) {
// Can only happen when running as native-image, fail for now
// TODO: native-image
errors.configuration(RB.$("validation_java_home_missing"));
return;
}
// Can't tell if the current JDK will work but might as well use it
nativeImage.getGraal().setPath(javaHome);
nativeImage.getGraal().setPlatform(currentPlatform);
}
}
}
if (isBlank(nativeImage.getMainJar().getPath())) {
errors.configuration(RB.$("validation_must_not_be_null", "nativeImage." + nativeImage.getName() + ".mainJar.path"));
}
if (null == nativeImage.getArchiveFormat()) {
nativeImage.setArchiveFormat(Archive.Format.ZIP);
}
validateGlobs(context, nativeImage.getJars(), "nativeImage." + nativeImage.getName() + ".jars", errors);
if (mode == JReleaserContext.Mode.ASSEMBLE) {
validateTemplate(context, nativeImage, errors);
}
if (!nativeImage.getFileSets().isEmpty()) {
i = 0;
for (FileSet fileSet : nativeImage.getFileSets()) {
validateFileSet(context, mode, nativeImage, fileSet, i++, errors);
}
}
NativeImage.Upx upx = nativeImage.getUpx();
if (!upx.isActiveSet()) {
upx.setActive(Active.NEVER);
}
if (!upx.resolveEnabled(context.getModel().getProject()))
return;
if (isBlank(upx.getVersion())) {
errors.configuration(RB.$("validation_is_missing", "nativeImage." + nativeImage.getName() + ".upx.version"));
}
}
use of org.jreleaser.model.NativeImage in project jreleaser by jreleaser.
the class NativeImageAssemblerProcessor method nativeImage.
private Artifact nativeImage(Path assembleDirectory, Path graalPath, Set<Path> jars, String imageName) throws AssemblerProcessingException {
String platform = assembler.getGraal().getPlatform();
String platformReplaced = assembler.getPlatform().applyReplacements(platform);
String finalImageName = imageName + "-" + platformReplaced;
String executable = assembler.getExecutable();
if (PlatformUtils.isWindows()) {
executable += ".exe";
}
context.getLogger().info("- {}", finalImageName);
Path image = assembleDirectory.resolve(executable).toAbsolutePath();
try {
if (Files.exists(image)) {
Files.deleteIfExists(image);
}
} catch (IOException e) {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_delete_image", executable), e);
}
assembler.getArgs().stream().filter(arg -> arg.startsWith("-H:Name")).findFirst().ifPresent(assembler.getArgs()::remove);
Path nativeImageExecutable = graalPath.resolve("bin").resolve(PlatformUtils.isWindows() ? "native-image.cmd" : "native-image").toAbsolutePath();
Command cmd = new Command(nativeImageExecutable.toString(), true).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()) {
cmd.arg("-cp").arg(jars.stream().map(Path::toAbsolutePath).map(Path::toString).map(this::maybeQuote).collect(Collectors.joining(File.pathSeparator)));
}
cmd.arg("-H:Name=" + assembler.getExecutable());
context.getLogger().debug(String.join(" ", cmd.getArgs()));
executeCommand(image.getParent(), cmd);
if (assembler.getUpx().isEnabled()) {
upx(image);
}
try {
Path tempDirectory = Files.createTempDirectory("jreleaser");
Path distDirectory = tempDirectory.resolve(finalImageName);
Files.createDirectories(distDirectory);
Path binDirectory = distDirectory.resolve("bin");
Files.createDirectories(binDirectory);
Files.copy(image, binDirectory.resolve(image.getFileName()));
FileUtils.copyFiles(context.getLogger(), context.getBasedir(), distDirectory, path -> path.getFileName().startsWith("LICENSE"));
copyFiles(context, distDirectory);
copyFileSets(context, distDirectory);
Path imageArchive = assembleDirectory.resolve(finalImageName + "." + assembler.getArchiveFormat().extension());
switch(assembler.getArchiveFormat()) {
case ZIP:
FileUtils.zip(tempDirectory, imageArchive);
break;
case TAR:
FileUtils.tar(tempDirectory, imageArchive);
break;
case TGZ:
case TAR_GZ:
FileUtils.tgz(tempDirectory, imageArchive);
break;
case TXZ:
case TAR_XZ:
FileUtils.xz(tempDirectory, imageArchive);
break;
case TBZ2:
case TAR_BZ2:
FileUtils.bz2(tempDirectory, imageArchive);
}
context.getLogger().debug("- {}", imageArchive.getFileName());
return Artifact.of(imageArchive, platform);
} catch (IOException e) {
throw new AssemblerProcessingException(RB.$("ERROR_unexpected_error"), e);
}
}
use of org.jreleaser.model.NativeImage in project jreleaser by jreleaser.
the class AssemblersValidator method validateAssemblers.
public static void validateAssemblers(JReleaserContext context, JReleaserContext.Mode mode, Errors errors) {
if (mode == JReleaserContext.Mode.CHANGELOG) {
return;
}
context.getLogger().debug("assemble");
Assemble assemble = context.getModel().getAssemble();
validateArchive(context, mode, errors);
validateJlink(context, mode, errors);
validateJpackage(context, mode, errors);
validateNativeImage(context, mode, errors);
// validate unique distribution names between exported assemblers
Map<String, List<String>> byDistributionName = new LinkedHashMap<>();
for (Archive archive : assemble.getActiveArchives()) {
List<String> types = byDistributionName.computeIfAbsent(archive.getName(), k -> new ArrayList<>());
types.add(archive.getType());
}
for (Jlink jlink : assemble.getActiveJlinks()) {
List<String> types = byDistributionName.computeIfAbsent(jlink.getName(), k -> new ArrayList<>());
if (jlink.isExported())
types.add(jlink.getType());
}
for (Jpackage jpackage : assemble.getActiveJpackages()) {
List<String> types = byDistributionName.computeIfAbsent(jpackage.getName(), k -> new ArrayList<>());
if (jpackage.isExported())
types.add(jpackage.getType());
}
for (NativeImage nativeImage : assemble.getActiveNativeImages()) {
List<String> types = byDistributionName.computeIfAbsent(nativeImage.getName(), k -> new ArrayList<>());
if (nativeImage.isExported())
types.add(nativeImage.getType());
}
byDistributionName.forEach((name, types) -> {
if (types.size() > 1) {
errors.configuration(RB.$("validation_multiple_assemblers", "distribution." + name, types));
assemble.setEnabled(false);
}
});
if (!assemble.isEnabledSet()) {
assemble.setEnabled(!assemble.getActiveArchives().isEmpty() || !assemble.getActiveJlinks().isEmpty() || !assemble.getActiveJpackages().isEmpty() || !assemble.getActiveNativeImages().isEmpty());
}
}
Aggregations