Search in sources :

Example 1 with NativeImage

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"));
    }
}
Also used : Archive(org.jreleaser.model.Archive) Artifact(org.jreleaser.model.Artifact) FileSet(org.jreleaser.model.FileSet) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) StringUtils.isBlank(org.jreleaser.util.StringUtils.isBlank) PlatformUtils(org.jreleaser.util.PlatformUtils) StringUtils.isNotBlank(org.jreleaser.util.StringUtils.isNotBlank) Platform(org.jreleaser.model.Platform) Active(org.jreleaser.model.Active) List(java.util.List) Errors(org.jreleaser.util.Errors) Map(java.util.Map) JReleaserContext(org.jreleaser.model.JReleaserContext) NativeImage(org.jreleaser.model.NativeImage) Optional(java.util.Optional) TemplateValidator.validateTemplate(org.jreleaser.model.validation.TemplateValidator.validateTemplate) RB(org.jreleaser.bundle.RB) Platform(org.jreleaser.model.Platform) FileSet(org.jreleaser.model.FileSet) NativeImage(org.jreleaser.model.NativeImage) List(java.util.List) Artifact(org.jreleaser.model.Artifact)

Example 2 with NativeImage

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);
    }
}
Also used : Path(java.nio.file.Path) NativeImage(org.jreleaser.model.NativeImage) Command(org.jreleaser.util.command.Command) AssemblerProcessingException(org.jreleaser.model.assembler.spi.AssemblerProcessingException) IOException(java.io.IOException)

Example 3 with NativeImage

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());
    }
}
Also used : JpackageValidator.validateJpackage(org.jreleaser.model.validation.JpackageValidator.validateJpackage) Jpackage(org.jreleaser.model.Jpackage) JpackageValidator.postValidateJpackage(org.jreleaser.model.validation.JpackageValidator.postValidateJpackage) Jlink(org.jreleaser.model.Jlink) JlinkValidator.validateJlink(org.jreleaser.model.validation.JlinkValidator.validateJlink) Archive(org.jreleaser.model.Archive) ArchiveValidator.validateArchive(org.jreleaser.model.validation.ArchiveValidator.validateArchive) NativeImageValidator.validateNativeImage(org.jreleaser.model.validation.NativeImageValidator.validateNativeImage) NativeImage(org.jreleaser.model.NativeImage) Assemble(org.jreleaser.model.Assemble) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

NativeImage (org.jreleaser.model.NativeImage)3 List (java.util.List)2 Archive (org.jreleaser.model.Archive)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)1 RB (org.jreleaser.bundle.RB)1 Active (org.jreleaser.model.Active)1 Artifact (org.jreleaser.model.Artifact)1 Assemble (org.jreleaser.model.Assemble)1 FileSet (org.jreleaser.model.FileSet)1 JReleaserContext (org.jreleaser.model.JReleaserContext)1 Jlink (org.jreleaser.model.Jlink)1 Jpackage (org.jreleaser.model.Jpackage)1 Platform (org.jreleaser.model.Platform)1 AssemblerProcessingException (org.jreleaser.model.assembler.spi.AssemblerProcessingException)1