Search in sources :

Example 1 with Jpackage

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

the class JpackageResolver method resolveJpackageOutputs.

private static void resolveJpackageOutputs(JReleaserContext context, Jpackage jpackage, Errors errors) {
    Path baseOutputDirectory = context.getAssembleDirectory().resolve(jpackage.getName()).resolve(jpackage.getType());
    Artifact jdk = jpackage.getResolvedPlatformPackager().getJdk();
    if (!context.isPlatformSelected(jdk))
        return;
    Jpackage.PlatformPackager packager = jpackage.getResolvedPlatformPackager();
    String platform = jdk.getPlatform();
    for (String type : packager.getTypes()) {
        try {
            Optional<Path> file = listFilesAndProcess(baseOutputDirectory, files -> files.filter(path -> path.getFileName().toString().endsWith(type)).findFirst());
            if (!file.isPresent()) {
                errors.assembly(RB.$("validation_missing_assembly", jpackage.getType(), jpackage.getName(), jpackage.getName()));
            } else {
                Artifact artifact = Artifact.of(file.get(), platform);
                artifact.setExtraProperties(jpackage.getExtraProperties());
                artifact.activate();
                jpackage.addOutput(artifact);
            }
        } catch (IOException e) {
            throw new JReleaserException(RB.$("ERROR_unexpected_error"), e);
        }
    }
}
Also used : Path(java.nio.file.Path) Jpackage(org.jreleaser.model.Jpackage) IOException(java.io.IOException) Artifact(org.jreleaser.model.Artifact) JReleaserException(org.jreleaser.util.JReleaserException)

Example 2 with Jpackage

use of org.jreleaser.model.Jpackage 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 3 with Jpackage

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

the class JpackageAssemblerProcessor method doAssemble.

@Override
protected void doAssemble(Map<String, Object> props) throws AssemblerProcessingException {
    Jpackage.PlatformPackager packager = assembler.getResolvedPlatformPackager();
    // verify jdk
    Path jdkPath = packager.getJdk().getEffectivePath(context, assembler);
    SemVer jdkVersion = SemVer.of(readJavaVersion(jdkPath));
    context.getLogger().debug(RB.$("assembler.jpackage.jdk"), jdkVersion, jdkPath.toAbsolutePath().toString());
    if (jdkVersion.getMajor() < 16) {
        throw new AssemblerProcessingException(RB.$("ERROR_jpackage_minimum_jdk_required", jdkVersion.toString()));
    }
    String platform = packager.getJdk().getPlatform();
    Path assembleDirectory = (Path) props.get(Constants.KEY_DISTRIBUTION_ASSEMBLE_DIRECTORY);
    Path workDirectory = assembleDirectory.resolve("work-" + platform);
    Path inputsDirectory = workDirectory.resolve("inputs");
    Path filesDirectory = inputsDirectory.resolve("files");
    // copy files to inputs
    copyFiles(context, filesDirectory);
    copyFileSets(context, filesDirectory);
    // copy jars to inputs
    context.getLogger().debug(RB.$("assembler.copy.jars"), context.relativizeToBasedir(filesDirectory));
    copyJars(context, assembler, filesDirectory, "");
    copyJars(context, assembler, filesDirectory, platform);
    // copy icon to inputs
    copyIcon(context, assembler, packager, inputsDirectory, platform, props);
    // adjust runtime image
    if (isNotBlank(assembler.getJlink())) {
        adjustRuntimeImage(context, assembler, workDirectory, platform);
    }
    for (String type : packager.getTypes()) {
        context.getLogger().info("- " + RB.$("assembler.jpackage.type"), type);
        jpackage(context, type, workDirectory, props);
    }
}
Also used : Jpackage(org.jreleaser.model.Jpackage) Path(java.nio.file.Path) AssemblerProcessingException(org.jreleaser.model.assembler.spi.AssemblerProcessingException) SemVer(org.jreleaser.util.SemVer)

Example 4 with Jpackage

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

the class JpackageAssemblerProcessor method jpackage.

private void jpackage(JReleaserContext context, String type, Path workDirectory, Map<String, Object> props) throws AssemblerProcessingException {
    Jpackage.PlatformPackager packager = assembler.getResolvedPlatformPackager();
    Path jdkPath = packager.getJdk().getEffectivePath(context, assembler);
    String platform = packager.getJdk().getPlatform();
    String platformReplaced = assembler.getPlatform().applyReplacements(platform);
    Path assembleDirectory = workDirectory.getParent();
    Path packagerDirectory = workDirectory.resolve(type).toAbsolutePath();
    try {
        FileUtils.deleteFiles(packagerDirectory);
    } catch (IOException e) {
        throw new AssemblerProcessingException(RB.$("ERROR_assembler_delete_dir", context.relativizeToBasedir(packagerDirectory)), e);
    }
    Path inputsDirectory = workDirectory.resolve("inputs");
    Optional<Artifact> runtimeImageByPlatform = assembler.findRuntimeImageByPlatform(platform);
    if (!runtimeImageByPlatform.isPresent()) {
        throw new AssemblerProcessingException(RB.$("ERROR_jpackage_runtime_image_not_found", platform));
    }
    String moduleName = assembler.getJava().getMainModule();
    String appName = packager.getResolvedAppName(context, assembler);
    String appVersion = assembler.getApplicationPackage().getResolvedAppVersion(context, assembler);
    String vendor = assembler.getApplicationPackage().getVendor();
    String copyright = assembler.getApplicationPackage().getCopyright();
    Path jpackageExecutable = jdkPath.resolve("bin").resolve(isWindows() ? "jpackage.exe" : "jpackage").toAbsolutePath();
    Command cmd = new Command(jpackageExecutable.toAbsolutePath().toString(), true).arg("--type").arg(type).arg("--dest").arg(assembleDirectory.toAbsolutePath().toString()).arg("--input").arg(inputsDirectory.resolve("files").toAbsolutePath().toString()).arg("--name").arg(maybeQuote(appName)).arg("--runtime-image").arg(maybeQuote(runtimeImageByPlatform.get().getEffectivePath(context, assembler).toAbsolutePath().toString())).arg("--app-version").arg(appVersion).arg("--vendor").arg(maybeQuote(vendor)).arg("--copyright").arg(maybeQuote(copyright)).arg("--description").arg(maybeQuote(context.getModel().getProject().getDescription()));
    if (assembler.isVerbose()) {
        cmd.arg("--verbose");
    }
    if (isNotBlank(moduleName)) {
        cmd.arg("--module").arg(moduleName + "/" + assembler.getJava().getMainClass());
    } else {
        cmd.arg("--main-class").arg(assembler.getJava().getMainClass()).arg("--main-jar").arg(maybeQuote(assembler.getMainJar().getResolvedPath().getFileName().toString()));
    }
    // Launcher
    for (String argument : assembler.getLauncher().getArguments()) {
        cmd.arg("--arguments").arg(maybeQuote(argument));
    }
    for (String javaOption : assembler.getLauncher().getJavaOptions()) {
        cmd.arg("--java-options").arg(maybeQuote(javaOption));
    }
    // ApplicationPackage
    String licenseFile = resolveTemplate(assembler.getApplicationPackage().getLicenseFile(), props);
    if (isNotBlank(licenseFile)) {
        Path licenseFilePath = context.getBasedir().resolve(licenseFile);
        if (Files.exists(licenseFilePath)) {
            cmd.arg("--license-file").arg(maybeQuote(licenseFilePath.toAbsolutePath().toString()));
        }
    }
    if (!assembler.getApplicationPackage().getFileAssociations().isEmpty()) {
        for (String filename : assembler.getApplicationPackage().getFileAssociations()) {
            Path path = context.getBasedir().resolve(resolveTemplate(filename, props));
            if (Files.exists(path)) {
                cmd.arg("--file-associations").arg(maybeQuote(path.toAbsolutePath().toString()));
            }
        }
    }
    customize(type, packager, inputsDirectory, cmd, props);
    context.getLogger().debug(String.join(" ", cmd.getArgs()));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    executeCommandCapturing(cmd, out);
    if (assembler.isVerbose()) {
        context.getLogger().debug(out.toString());
    }
    // replace only if not linux
    if (!PlatformUtils.isLinux(platform) && assembler.isAttachPlatform()) {
        try {
            Optional<Path> artifact = listFilesAndProcess(assembleDirectory, files -> files.filter(path -> path.getFileName().toString().endsWith(type)).findFirst());
            if (artifact.isPresent()) {
                String dest = artifact.get().getFileName().toString().replace("." + type, "-" + platformReplaced + "." + type);
                Files.move(assembleDirectory.resolve(artifact.get().getFileName()), assembleDirectory.resolve(dest), REPLACE_EXISTING);
            }
        } catch (IOException e) {
            throw new AssemblerProcessingException(RB.$("ERROR_unexpected_error"), e);
        }
    }
}
Also used : Jpackage(org.jreleaser.model.Jpackage) Path(java.nio.file.Path) Command(org.jreleaser.util.command.Command) AssemblerProcessingException(org.jreleaser.model.assembler.spi.AssemblerProcessingException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Artifact(org.jreleaser.model.Artifact)

Example 5 with Jpackage

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

the class JpackageValidator method postValidateJpackage.

private static void postValidateJpackage(JReleaserContext context, JReleaserContext.Mode mode, Jpackage jpackage, Errors errors) {
    Project project = context.getModel().getProject();
    if (!jpackage.resolveEnabled(project))
        return;
    Jpackage.ApplicationPackage applicationPackage = jpackage.getApplicationPackage();
    if (isBlank(applicationPackage.getCopyright())) {
        applicationPackage.setCopyright(project.getCopyright());
    }
}
Also used : Jpackage(org.jreleaser.model.Jpackage) Project(org.jreleaser.model.Project)

Aggregations

Jpackage (org.jreleaser.model.Jpackage)6 Path (java.nio.file.Path)4 Artifact (org.jreleaser.model.Artifact)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Jlink (org.jreleaser.model.Jlink)2 Project (org.jreleaser.model.Project)2 AssemblerProcessingException (org.jreleaser.model.assembler.spi.AssemblerProcessingException)2 SemVer (org.jreleaser.util.SemVer)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Collections.singletonList (java.util.Collections.singletonList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)1 RB (org.jreleaser.bundle.RB)1 Active (org.jreleaser.model.Active)1 Archive (org.jreleaser.model.Archive)1 Assemble (org.jreleaser.model.Assemble)1