Search in sources :

Example 21 with AssemblerProcessingException

use of org.jreleaser.model.assembler.spi.AssemblerProcessingException in project jreleaser by jreleaser.

the class NativeImageAssemblerProcessor method upx.

private void upx(Path image) throws AssemblerProcessingException {
    Upx upx = new Upx(context, assembler.getUpx().getVersion());
    try {
        if (!upx.setup()) {
            context.getLogger().warn(RB.$("tool_unavailable", "upx"));
            return;
        }
    } catch (ToolException e) {
        throw new AssemblerProcessingException(e.getMessage(), e);
    }
    List<String> args = new ArrayList<>(assembler.getUpx().getArgs());
    args.add(image.getFileName().toString());
    context.getLogger().info("upx {}", image.getFileName().toString());
    try {
        upx.compress(image.getParent(), args);
    } catch (CommandException e) {
        throw new AssemblerProcessingException(RB.$("ERROR_unexpected_error"), e);
    }
}
Also used : AssemblerProcessingException(org.jreleaser.model.assembler.spi.AssemblerProcessingException) ArrayList(java.util.ArrayList) ToolException(org.jreleaser.sdk.tool.ToolException) CommandException(org.jreleaser.util.command.CommandException) Upx(org.jreleaser.sdk.tool.Upx)

Example 22 with AssemblerProcessingException

use of org.jreleaser.model.assembler.spi.AssemblerProcessingException 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 23 with AssemblerProcessingException

use of org.jreleaser.model.assembler.spi.AssemblerProcessingException in project jreleaser by jreleaser.

the class AbstractAssemblerProcessor method writeFile.

protected void writeFile(byte[] content, Path outputFile) throws AssemblerProcessingException {
    try {
        createDirectoriesWithFullAccess(outputFile.getParent());
        Files.write(outputFile, content, CREATE, WRITE, TRUNCATE_EXISTING);
        grantFullAccess(outputFile);
    } catch (IOException e) {
        throw new AssemblerProcessingException(RB.$("ERROR_unexpected_error_writing_file", outputFile.toAbsolutePath()), e);
    }
}
Also used : AssemblerProcessingException(org.jreleaser.model.assembler.spi.AssemblerProcessingException) IOException(java.io.IOException)

Aggregations

AssemblerProcessingException (org.jreleaser.model.assembler.spi.AssemblerProcessingException)23 IOException (java.io.IOException)18 Path (java.nio.file.Path)18 Command (org.jreleaser.util.command.Command)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Properties (java.util.Properties)3 Artifact (org.jreleaser.model.Artifact)3 Reader (java.io.Reader)2 Files (java.nio.file.Files)2 REPLACE_EXISTING (java.nio.file.StandardCopyOption.REPLACE_EXISTING)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 RB (org.jreleaser.bundle.RB)2 Archive (org.jreleaser.model.Archive)2 Glob (org.jreleaser.model.Glob)2 JReleaserContext (org.jreleaser.model.JReleaserContext)2 JavaAssembler (org.jreleaser.model.JavaAssembler)2 Jpackage (org.jreleaser.model.Jpackage)2 SemVer (org.jreleaser.util.SemVer)2