Search in sources :

Example 1 with UpxCompressedBuildItem

use of io.quarkus.deployment.pkg.builditem.UpxCompressedBuildItem in project quarkus by quarkusio.

the class UpxCompressionBuildStep method compress.

@BuildStep(onlyIf = NativeBuild.class)
public void compress(NativeConfig nativeConfig, NativeImageBuildItem image, CompiledJavaVersionBuildItem compiledJavaVersionBuildItem, BuildProducer<UpxCompressedBuildItem> upxCompressedProducer, BuildProducer<ArtifactResultBuildItem> artifactResultProducer) {
    if (nativeConfig.compression.level.isEmpty()) {
        log.debug("UPX compression disabled");
        return;
    }
    String effectiveBuilderImage = nativeConfig.getEffectiveBuilderImage(compiledJavaVersionBuildItem.getJavaVersion().isExactlyJava11() == CompiledJavaVersionBuildItem.JavaVersion.Status.FALSE);
    Optional<File> upxPathFromSystem = getUpxFromSystem();
    if (upxPathFromSystem.isPresent()) {
        log.debug("Running UPX from system path");
        if (!runUpxFromHost(upxPathFromSystem.get(), image.getPath().toFile(), nativeConfig)) {
            throw new IllegalStateException("Unable to compress the native executable");
        }
    } else if (nativeConfig.isContainerBuild()) {
        log.infof("Running UPX from a container using the builder image: " + effectiveBuilderImage);
        if (!runUpxInContainer(image, nativeConfig, effectiveBuilderImage)) {
            throw new IllegalStateException("Unable to compress the native executable");
        }
    } else {
        log.errorf("Unable to compress the native executable. Either install `upx` from https://upx.github.io/" + " on your machine, or enable in-container build using `-Dquarkus.native.container-build=true`.");
        throw new IllegalStateException("Unable to compress the native executable: `upx` not available");
    }
    log.infof("Native executable compressed: %s", image.getPath().toFile().getAbsolutePath());
    upxCompressedProducer.produce(new UpxCompressedBuildItem());
}
Also used : UpxCompressedBuildItem(io.quarkus.deployment.pkg.builditem.UpxCompressedBuildItem) File(java.io.File) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

BuildStep (io.quarkus.deployment.annotations.BuildStep)1 UpxCompressedBuildItem (io.quarkus.deployment.pkg.builditem.UpxCompressedBuildItem)1 File (java.io.File)1