Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to re-use an existing native build #17970

Merged
merged 1 commit into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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