Skip to content

Commit 66aefab

Browse files
authored
Merge pull request #48413 from turing85/main
2 parents da9f88e + e2c4f23 commit 66aefab

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

core/deployment/src/main/java/io/quarkus/deployment/pkg/NativeConfig.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,25 @@ interface Debug {
538538

539539
@ConfigGroup
540540
interface Compression {
541+
/**
542+
* Whether compression should be enabled.
543+
*/
544+
@WithDefault("true")
545+
boolean enabled();
546+
547+
/**
548+
* Whether the compression should be executed within a container.
549+
*/
550+
Optional<Boolean> containerBuild();
551+
552+
/**
553+
* The image used for compression. Defaults to {@code quarkus.native.builder-image} if not
554+
* set.
555+
* <p>
556+
* Setting this variable will automatically activate
557+
*/
558+
Optional<String> containerImage();
559+
541560
/**
542561
* The compression level in [1, 10].
543562
* 10 means <em>best</em>.

core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/UpxCompressionBuildStep.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void compress(NativeConfig nativeConfig, NativeImageRunnerBuildItem nativ
4141
BuildProducer<UpxCompressedBuildItem> upxCompressedProducer,
4242
BuildProducer<ArtifactResultBuildItem> artifactResultProducer) {
4343

44-
if (nativeConfig.compression().level().isEmpty()) {
44+
if (nativeConfig.compression().level().isEmpty() || !nativeConfig.compression().enabled()) {
4545
log.debug("UPX compression disabled");
4646
return;
4747
}
@@ -52,7 +52,8 @@ public void compress(NativeConfig nativeConfig, NativeImageRunnerBuildItem nativ
5252

5353
String effectiveBuilderImage = nativeConfig.builderImage().getEffectiveImage();
5454
Optional<File> upxPathFromSystem = getUpxFromSystem();
55-
if (upxPathFromSystem.isPresent()) {
55+
if (upxPathFromSystem.isPresent() && !nativeConfig.compression().containerBuild().orElse(false)
56+
&& nativeConfig.compression().containerImage().isEmpty()) {
5657
log.debug("Running UPX from system path");
5758
if (!runUpxFromHost(upxPathFromSystem.get(), image.getPath().toFile(), nativeConfig)) {
5859
throw new IllegalStateException("Unable to compress the native executable");
@@ -61,9 +62,12 @@ public void compress(NativeConfig nativeConfig, NativeImageRunnerBuildItem nativ
6162
log.error("Compression of native executables is not yet implemented for remote container builds.");
6263
throw new IllegalStateException(
6364
"Unable to compress the native executable: Compression of native executables is not yet supported for remote container builds");
64-
} else if (nativeImageRunner.isContainerBuild()) {
65-
log.info("Running UPX from a container using the builder image: " + effectiveBuilderImage);
66-
if (!runUpxInContainer(image, nativeConfig, effectiveBuilderImage)) {
65+
} else if (nativeConfig.compression().containerBuild().orElse(true) &&
66+
(nativeImageRunner.isContainerBuild() ||
67+
nativeConfig.compression().containerImage().isPresent())) {
68+
String compressorImage = nativeConfig.compression().containerImage().orElse(effectiveBuilderImage);
69+
log.info("Running UPX from a container using the compressor image: " + compressorImage);
70+
if (!runUpxInContainer(image, nativeConfig, compressorImage)) {
6771
throw new IllegalStateException("Unable to compress the native executable");
6872
}
6973
} else {

docs/src/main/asciidoc/upx.adoc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,30 @@ Note that UPX compression:
2525
The UPX compression requires:
2626

2727
* the `upx` command to be available in the system `PATH`;
28+
* an explicitly defined `quarkus.native.compression.container-image`;
2829
* or to have built the native executable using an in-container build.
2930

31+
If `quarkus.native.compression.container-image` is not set explicitly, it will implicitly default to `quarkus.native.builder-image`.
32+
3033
If you have the `upx` command available on your path, Quarkus uses it.
31-
Otherwise, if you built the native image using an in-container build (using `quarkus.native.container-build=true`) and if the builder image provides the `upx` command, Quarkus compresses the executable from inside the container.
34+
Otherwise, if you built the native image using an in-container build (using `quarkus.native.container-build=true`) and if the compression image provides the `upx` command, Quarkus compresses the executable from inside the container.
35+
36+
If you want to force compression to take place in a container, you can set `quarkus.native.compression.container-build` to `true` (or `false` to explicitly not run compression in a container).
3237

3338
If you are not in one of these cases, the compression fails.
3439

40+
Setting `quarkus.native.compression.container-image` results in the compression to run in a container.
41+
If you want to set the variable, but not run the compression in a container, set `quakrus.native.compression.container-build` explicitly to `false`.
42+
43+
[IMPORTANT]
44+
.`WORKDIR` for the image used for compression
45+
====
46+
The executable to compress is mounted in directory `/project`.
47+
Since the container runs `upx` in the current working directory, the `WORKDIR` of the image used for compression must be `/project`.
48+
====
49+
50+
Compression can be explicitly en-/disabled by setting `quarkus.native.compression.enabled`.
51+
3552
[IMPORTANT]
3653
.upx is cross-platform.
3754
====

0 commit comments

Comments
 (0)