Skip to content

Commit

Permalink
Add the ability to re-use an existing native build
Browse files Browse the repository at this point in the history
This allows you to deploy something you have already built.
  • Loading branch information
stuartwdouglas committed Jun 17, 2021
1 parent 805aa6c commit b082390
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ public class NativeConfig {
@ConfigItem
public boolean reportErrorsAtRuntime;

/**
* Don't build a native image if it already exists.
*
* This is useful if you have already built an image and you want to use Quarkus to deploy it somewhere.
*
* Note that this is not able to detect if the existing image is outdated, if you have modified source
* or config and want a new image you must not use this flag.
*/
@ConfigItem(defaultValue = "false")
public boolean reuseExisting;

/**
* Build time configuration options for resources inclusion in the native executable.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa

String nativeImageName = getNativeImageName(outputTargetBuildItem, packageConfig);
String resultingExecutableName = getResultingExecutableName(nativeImageName, isContainerBuild);
Path generatedExecutablePath = outputDir.resolve(resultingExecutableName);
Path finalExecutablePath = outputTargetBuildItem.getOutputDirectory().resolve(resultingExecutableName);

NativeImageBuildRunner buildRunner = getNativeImageBuildRunner(nativeConfig, outputDir,
nativeImageName, resultingExecutableName);
Expand All @@ -160,6 +162,14 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
} else {
log.error("Unable to get GraalVM version from the native-image binary.");
}
if (nativeConfig.reuseExisting) {
if (Files.exists(finalExecutablePath)) {
return new NativeImageBuildItem(finalExecutablePath,
new NativeImageBuildItem.GraalVMVersion(graalVMVersion.fullVersion, graalVMVersion.major,
graalVMVersion.minor,
graalVMVersion.distribution.name()));
}
}

try {
if (nativeConfig.cleanupServer) {
Expand Down Expand Up @@ -189,8 +199,6 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
if (exitCode != 0) {
throw imageGenerationFailed(exitCode, nativeImageArgs);
}
Path generatedExecutablePath = outputDir.resolve(resultingExecutableName);
Path finalExecutablePath = outputTargetBuildItem.getOutputDirectory().resolve(resultingExecutableName);
IoUtils.copy(generatedExecutablePath, finalExecutablePath);
Files.delete(generatedExecutablePath);
if (nativeConfig.debug.enabled) {
Expand Down
2 changes: 2 additions & 0 deletions docs/src/main/asciidoc/container-image.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ To build a container image for your project, `quarkus.container-image.build=true
./mvnw clean package -Dquarkus.container-image.build=true
----

NOTE: If you ever want to build a native container image and already have an existing native image you can set `-Dquarkus.native.reuse-existing=true` and the native image build will not be re-run.

== Pushing

To push a container image for your project, `quarkus.container-image.push=true` needs to be set using any of the ways that Quarkus supports.
Expand Down

0 comments on commit b082390

Please sign in to comment.