Search in sources :

Example 1 with Jlink

use of org.jreleaser.model.Jlink in project jreleaser by jreleaser.

the class JpackageValidator method validateJpackage.

private static void validateJpackage(JReleaserContext context, JReleaserContext.Mode mode, Jpackage jpackage, Errors errors) {
    context.getLogger().debug("jpackage.{}", jpackage.getName());
    if (!jpackage.isActiveSet()) {
        jpackage.setActive(Active.NEVER);
    }
    Project project = context.getModel().getProject();
    if (!jpackage.resolveEnabled(project))
        return;
    Jpackage.PlatformPackager packager = jpackage.getResolvedPlatformPackager();
    Jpackage.ApplicationPackage applicationPackage = jpackage.getApplicationPackage();
    packager.enable();
    if (isNotBlank(jpackage.getJlink())) {
        Jlink jlink = context.getModel().getAssemble().findJlink(jpackage.getJlink());
        Path baseOutputDirectory = context.getAssembleDirectory().resolve(jlink.getName()).resolve(jlink.getType());
        String imageName = jlink.getResolvedImageName(context);
        List<Artifact> candidateRuntimeImages = new ArrayList<>();
        for (Artifact targetJdk : jlink.getTargetJdks()) {
            if (!context.isPlatformSelected(targetJdk))
                continue;
            String platform = targetJdk.getPlatform();
            Path path = baseOutputDirectory.resolve("work-" + platform).resolve(imageName + "-" + platform).toAbsolutePath();
            candidateRuntimeImages.add(Artifact.of(path, platform));
        }
        if (jpackage.getRuntimeImages().size() > 0 && jpackage.getRuntimeImages().size() != candidateRuntimeImages.size()) {
            errors.configuration(RB.$("validation_jpackage_jlink_application", jpackage.getName()));
        }
        int count = 0;
        for (Artifact runtimeImage : jpackage.getRuntimeImages()) {
            Path rp = runtimeImage.getResolvedPath(context, jpackage);
            Path tp = runtimeImage.getResolvedTransform(context, jpackage);
            Path path = tp != null ? tp : rp;
            if (candidateRuntimeImages.stream().anyMatch(a -> a.getPath().equals(path.toString()))) {
                count++;
            }
        }
        if (jpackage.getRuntimeImages().size() > 0 && count != candidateRuntimeImages.size()) {
            errors.configuration(RB.$("validation_jpackage_jlink_application", jpackage.getName()));
        }
        jpackage.setJava(jlink.getJava());
        jpackage.setMainJar(jlink.getMainJar());
        jpackage.setJars(jlink.getJars());
        packager.setJdk(jlink.getJdk());
        if (isBlank(jpackage.getExecutable())) {
            jpackage.setExecutable(jlink.getExecutable());
        }
        for (Artifact runtimeImage : candidateRuntimeImages) {
            runtimeImage.activate();
            jpackage.addRuntimeImage(runtimeImage);
        }
    }
    context.getLogger().debug("jpackage.{}.java", jpackage.getName());
    if (!validateJava(context, jpackage, errors)) {
        return;
    }
    if (isBlank(jpackage.getExecutable())) {
        jpackage.setExecutable(jpackage.getName());
    }
    if (jpackage.getRuntimeImages().size() == 0) {
        errors.configuration(RB.$("validation_jpackage_runtime_images_missing", jpackage.getName()));
        return;
    }
    int i = 0;
    for (Artifact runtimeImage : jpackage.getRuntimeImages()) {
        validateRuntimeImage(context, mode, jpackage, runtimeImage, i++, errors);
    }
    // validate jdks.platform is unique
    Map<String, List<Artifact>> byPlatform = jpackage.getRuntimeImages().stream().collect(groupingBy(ri -> isBlank(ri.getPlatform()) ? "<nil>" : ri.getPlatform()));
    if (byPlatform.containsKey("<nil>")) {
        errors.configuration(RB.$("validation_jpackage_runtime_image_platform", jpackage.getName()));
    }
    // check platforms
    byPlatform.forEach((p, jdks) -> {
        if (jdks.size() > 1) {
            errors.configuration(RB.$("validation_jpackage_runtime_image_multiple_platforms", jpackage.getName(), p));
        }
    });
    if (isBlank(packager.getJdk().getPath())) {
        String javaHome = System.getProperty("java.home");
        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;
        }
        packager.getJdk().setPath(javaHome);
        packager.getJdk().setPlatform(PlatformUtils.getCurrentFull());
    }
    if (packager.getTypes().isEmpty()) {
        packager.setTypes(singletonList(packager.getValidTypes().get(0)));
    }
    if (isBlank(applicationPackage.getAppName())) {
        applicationPackage.setAppName(jpackage.getName());
    }
    if (isBlank(applicationPackage.getAppVersion())) {
        applicationPackage.setAppVersion(project.getResolvedVersion());
    }
    // validate appVersion
    String appVersion = applicationPackage.getResolvedAppVersion(context, jpackage);
    try {
        SemVer v = SemVer.of(appVersion);
        if (isNotBlank(v.getBuild()) && isNotBlank(v.getTag()) && v.getMajor() <= 0) {
            errors.configuration(RB.$("validation_jpackage_invalid_appversion", appVersion));
        }
    } catch (IllegalArgumentException e) {
        // can't use this value
        errors.configuration(RB.$("validation_jpackage_invalid_appversion", appVersion));
    }
    if (isBlank(applicationPackage.getVendor())) {
        applicationPackage.setVendor(project.getVendor());
    }
    if (isBlank(applicationPackage.getVendor())) {
        errors.configuration(RB.$("validation_jpackage_missing_vendor", jpackage.getName()));
    }
    if (isBlank(applicationPackage.getCopyright())) {
        applicationPackage.setCopyright(project.getCopyright());
    }
    if (mode == JReleaserContext.Mode.ASSEMBLE) {
        validateTemplate(context, jpackage, errors);
    }
    if (isBlank(packager.getAppName())) {
        packager.setAppName(jpackage.getApplicationPackage().getAppName());
    }
    if (packager instanceof Jpackage.Linux) {
        validateLinux(context, jpackage, (Jpackage.Linux) packager, errors);
    }
    if (packager instanceof Jpackage.Osx) {
        validateOsx(context, jpackage, (Jpackage.Osx) packager, errors);
    }
    if (packager instanceof Jpackage.Windows) {
        validateWindows(context, jpackage, (Jpackage.Windows) packager, errors);
    }
}
Also used : Jpackage(org.jreleaser.model.Jpackage) Path(java.nio.file.Path) Artifact(org.jreleaser.model.Artifact) Jlink(org.jreleaser.model.Jlink) 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) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Project(org.jreleaser.model.Project) Active(org.jreleaser.model.Active) List(java.util.List) Errors(org.jreleaser.util.Errors) Map(java.util.Map) JReleaserContext(org.jreleaser.model.JReleaserContext) TemplateValidator.validateTemplate(org.jreleaser.model.validation.TemplateValidator.validateTemplate) SemVer(org.jreleaser.util.SemVer) Pattern(java.util.regex.Pattern) Jpackage(org.jreleaser.model.Jpackage) RB(org.jreleaser.bundle.RB) Path(java.nio.file.Path) Jlink(org.jreleaser.model.Jlink) ArrayList(java.util.ArrayList) SemVer(org.jreleaser.util.SemVer) Artifact(org.jreleaser.model.Artifact) Project(org.jreleaser.model.Project) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List)

Example 2 with Jlink

use of org.jreleaser.model.Jlink in project jreleaser by jreleaser.

the class JlinkValidator method validateJlink.

private static void validateJlink(JReleaserContext context, JReleaserContext.Mode mode, Jlink jlink, Errors errors) {
    context.getLogger().debug("jlink.{}", jlink.getName());
    if (!jlink.isActiveSet()) {
        jlink.setActive(Active.NEVER);
    }
    if (!jlink.resolveEnabled(context.getModel().getProject()))
        return;
    if (isBlank(jlink.getName())) {
        errors.configuration(RB.$("validation_must_not_be_blank", "jlink.name"));
        return;
    }
    context.getLogger().debug("jlink.{}.java", jlink.getName());
    if (!validateJava(context, jlink, errors)) {
        return;
    }
    Platform platform = jlink.getPlatform().merge(context.getModel().getPlatform());
    jlink.setPlatform(platform);
    if (isBlank(jlink.getImageName())) {
        jlink.setImageName(jlink.getJava().getGroupId() + "." + jlink.getJava().getArtifactId() + "-" + context.getModel().getProject().getResolvedVersion());
    }
    if (isBlank(jlink.getExecutable())) {
        jlink.setExecutable(jlink.getName());
    }
    int i = 0;
    for (Artifact targetJdk : jlink.getTargetJdks()) {
        validateJdk(context, mode, jlink, targetJdk, i++, errors);
    }
    // validate jdks.platform is unique
    Map<String, List<Artifact>> byPlatform = jlink.getTargetJdks().stream().collect(groupingBy(jdk -> isBlank(jdk.getPlatform()) ? "<nil>" : jdk.getPlatform()));
    if (byPlatform.containsKey("<nil>")) {
        errors.configuration(RB.$("validation_jlink_jdk_platform", jlink.getName()));
    }
    // check platforms
    byPlatform.forEach((p, jdks) -> {
        if (jdks.size() > 1) {
            errors.configuration(RB.$("validation_jlink_jdk_multiple_platforms", jlink.getName(), p));
        }
    });
    if (isBlank(jlink.getJdk().getPath())) {
        String currentPlatform = PlatformUtils.getCurrentFull();
        String javaHome = System.getProperty("java.home");
        if (jlink.getTargetJdks().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
            jlink.getJdk().setPath(javaHome);
            jlink.getJdk().setPlatform(currentPlatform);
            jlink.addTargetJdk(jlink.getJdk());
        } else {
            // find a compatible JDK in targets
            Optional<Artifact> jdk = jlink.getTargetJdks().stream().filter(j -> PlatformUtils.isCompatible(currentPlatform, j.getPlatform())).findFirst();
            if (jdk.isPresent()) {
                jlink.setJdk(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
                jlink.getJdk().setPath(javaHome);
                jlink.getJdk().setPlatform(currentPlatform);
            }
        }
    }
    if (jlink.getArgs().isEmpty()) {
        jlink.getArgs().add("--no-header-files");
        jlink.getArgs().add("--no-man-pages");
        jlink.getArgs().add("--compress=2");
        jlink.getArgs().add("--strip-debug");
    }
    if (null == jlink.getMainJar()) {
        errors.configuration(RB.$("validation_is_null", "jlink." + jlink.getName() + ".mainJar"));
        return;
    }
    if (isBlank(jlink.getMainJar().getPath())) {
        errors.configuration(RB.$("validation_must_not_be_null", "jlink." + jlink.getName() + ".mainJar.path"));
    }
    validateGlobs(context, jlink.getJars(), "jlink." + jlink.getName() + ".jars", errors);
    if (mode == JReleaserContext.Mode.ASSEMBLE) {
        validateTemplate(context, jlink, errors);
    }
    if (!jlink.getFileSets().isEmpty()) {
        i = 0;
        for (FileSet fileSet : jlink.getFileSets()) {
            validateFileSet(context, mode, jlink, fileSet, i++, errors);
        }
    }
}
Also used : Artifact(org.jreleaser.model.Artifact) FileSet(org.jreleaser.model.FileSet) Jlink(org.jreleaser.model.Jlink) 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) Project(org.jreleaser.model.Project) Active(org.jreleaser.model.Active) List(java.util.List) Errors(org.jreleaser.util.Errors) Map(java.util.Map) JReleaserContext(org.jreleaser.model.JReleaserContext) 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) List(java.util.List) Artifact(org.jreleaser.model.Artifact)

Example 3 with Jlink

use of org.jreleaser.model.Jlink 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

List (java.util.List)3 Jlink (org.jreleaser.model.Jlink)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)2 RB (org.jreleaser.bundle.RB)2 Active (org.jreleaser.model.Active)2 Artifact (org.jreleaser.model.Artifact)2 JReleaserContext (org.jreleaser.model.JReleaserContext)2 Jpackage (org.jreleaser.model.Jpackage)2 Project (org.jreleaser.model.Project)2 TemplateValidator.validateTemplate (org.jreleaser.model.validation.TemplateValidator.validateTemplate)2 Errors (org.jreleaser.util.Errors)2 PlatformUtils (org.jreleaser.util.PlatformUtils)2 StringUtils.isBlank (org.jreleaser.util.StringUtils.isBlank)2 StringUtils.isNotBlank (org.jreleaser.util.StringUtils.isNotBlank)2 Path (java.nio.file.Path)1 Collections.singletonList (java.util.Collections.singletonList)1 LinkedHashMap (java.util.LinkedHashMap)1 Optional (java.util.Optional)1