Search in sources :

Example 11 with JReleaserContext

use of org.jreleaser.model.JReleaserContext 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 12 with JReleaserContext

use of org.jreleaser.model.JReleaserContext 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 13 with JReleaserContext

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

the class AbstractJavaAssemblerProcessor method copyFiles.

protected Set<Path> copyFiles(JReleaserContext context, Path destination) throws AssemblerProcessingException {
    Set<Path> paths = new LinkedHashSet<>();
    // resolve all first
    for (Glob glob : assembler.getFiles()) {
        glob.getResolvedArtifacts(context).stream().map(artifact -> artifact.getResolvedPath(context, assembler)).forEach(paths::add);
    }
    // copy all next
    try {
        Files.createDirectories(destination);
        for (Path path : paths) {
            context.getLogger().debug(RB.$("assembler.copying"), path.getFileName());
            Files.copy(path, destination.resolve(path.getFileName()), REPLACE_EXISTING);
        }
    } catch (IOException e) {
        throw new AssemblerProcessingException(RB.$("ERROR_assembler_copying_files"), e);
    }
    return paths;
}
Also used : Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Constants(org.jreleaser.util.Constants) JavaAssembler(org.jreleaser.model.JavaAssembler) Files(java.nio.file.Files) MustacheUtils.applyTemplate(org.jreleaser.util.MustacheUtils.applyTemplate) Set(java.util.Set) IOException(java.io.IOException) Reader(java.io.Reader) Glob(org.jreleaser.model.Glob) Project(org.jreleaser.model.Project) TemplateUtils.resolveAndMergeTemplates(org.jreleaser.templates.TemplateUtils.resolveAndMergeTemplates) Map(java.util.Map) JReleaserContext(org.jreleaser.model.JReleaserContext) RB(org.jreleaser.bundle.RB) Path(java.nio.file.Path) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) AssemblerProcessingException(org.jreleaser.model.assembler.spi.AssemblerProcessingException) LinkedHashSet(java.util.LinkedHashSet) AssemblerProcessingException(org.jreleaser.model.assembler.spi.AssemblerProcessingException) Glob(org.jreleaser.model.Glob) IOException(java.io.IOException)

Example 14 with JReleaserContext

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

the class AssemblerUtils method copyJars.

public static Set<Path> copyJars(JReleaserContext context, JavaAssembler assembler, Path jarsDirectory, String platform) throws AssemblerProcessingException {
    Set<Path> paths = new LinkedHashSet<>();
    // resolve all first
    if (isBlank(platform)) {
        paths.add(assembler.getMainJar().getEffectivePath(context, assembler));
    }
    for (Glob glob : assembler.getJars()) {
        if ((isBlank(platform) && isBlank(glob.getPlatform())) || (isNotBlank(platform) && PlatformUtils.isCompatible(platform, glob.getPlatform()))) {
            glob.getResolvedArtifacts(context).stream().map(artifact -> artifact.getResolvedPath(context, assembler)).forEach(paths::add);
        }
    }
    // copy all next
    try {
        Files.createDirectories(jarsDirectory);
        for (Path path : paths) {
            context.getLogger().debug(RB.$("assembler.copying"), path.getFileName());
            Files.copy(path, jarsDirectory.resolve(path.getFileName()), REPLACE_EXISTING);
        }
    } catch (IOException e) {
        throw new AssemblerProcessingException(RB.$("ERROR_assembler_copying_jars"), e);
    }
    return paths;
}
Also used : Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) JavaAssembler(org.jreleaser.model.JavaAssembler) Properties(java.util.Properties) Files(java.nio.file.Files) Set(java.util.Set) StringUtils.isBlank(org.jreleaser.util.StringUtils.isBlank) IOException(java.io.IOException) PlatformUtils(org.jreleaser.util.PlatformUtils) File(java.io.File) StringUtils.isNotBlank(org.jreleaser.util.StringUtils.isNotBlank) Glob(org.jreleaser.model.Glob) Paths(java.nio.file.Paths) JReleaserContext(org.jreleaser.model.JReleaserContext) RB(org.jreleaser.bundle.RB) Path(java.nio.file.Path) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) AssemblerProcessingException(org.jreleaser.model.assembler.spi.AssemblerProcessingException) LinkedHashSet(java.util.LinkedHashSet) AssemblerProcessingException(org.jreleaser.model.assembler.spi.AssemblerProcessingException) Glob(org.jreleaser.model.Glob) IOException(java.io.IOException)

Example 15 with JReleaserContext

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

the class JReleaserAutoConfigReleaseTask method execute.

@Override
public void execute() throws BuildException {
    Banner.display(new PrintWriter(System.out, true));
    basedir();
    initLogger();
    JReleaserContext context = ModelAutoConfigurer.builder().logger(logger).basedir(actualBasedir).outputDirectory(getOutputDirectory()).dryrun(dryrun).gitRootSearch(gitRootSearch).projectName(projectName).projectVersion(projectVersion).projectVersionPattern(projectVersionPattern).projectSnapshotPattern(projectSnapshotPattern).projectSnapshotLabel(projectSnapshotLabel).projectSnapshotFullChangelog(projectSnapshotFullChangelog).tagName(tagName).previousTagName(previousTagName).releaseName(releaseName).branch(branch).milestoneName(milestoneName).prerelease(prerelease).prereleasePattern(prereleasePattern).draft(draft).overwrite(overwrite).update(update).updateSections(collectUpdateSections()).skipTag(skipTag).skipRelease(skipRelease).changelog(changelog).changelogFormatted(changelogFormatted).username(username).commitAuthorName(commitAuthorName).commitAuthorEmail(commitAuthorEmail).signing(signing).armored(armored).files(fileSet.stream().map(Resource::getName).collect(toList())).globs(globs).selectedPlatforms(collectSelectedPlatforms()).autoConfigure();
    Workflows.release(context).execute();
}
Also used : JReleaserContext(org.jreleaser.model.JReleaserContext) PrintWriter(java.io.PrintWriter)

Aggregations

JReleaserContext (org.jreleaser.model.JReleaserContext)29 RB (org.jreleaser.bundle.RB)13 Map (java.util.Map)11 List (java.util.List)10 Project (org.jreleaser.model.Project)8 StringUtils.isBlank (org.jreleaser.util.StringUtils.isBlank)8 StringUtils.isNotBlank (org.jreleaser.util.StringUtils.isNotBlank)8 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)7 Active (org.jreleaser.model.Active)7 Artifact (org.jreleaser.model.Artifact)7 Errors (org.jreleaser.util.Errors)7 ArrayList (java.util.ArrayList)5 LinkedHashSet (java.util.LinkedHashSet)5 Optional (java.util.Optional)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 TemplateValidator.validateTemplate (org.jreleaser.model.validation.TemplateValidator.validateTemplate)5 PlatformUtils (org.jreleaser.util.PlatformUtils)5 IOException (java.io.IOException)4 Distribution (org.jreleaser.model.Distribution)4