Skip to content

Commit

Permalink
deprecate buildNative gradle task
Browse files Browse the repository at this point in the history
  • Loading branch information
glefloch committed Apr 7, 2020
1 parent a327a4b commit 7ad072d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -81,6 +82,9 @@ public void canBuild(SourceType sourceType) throws IOException, InterruptedExcep
assertThat(build.task(":build").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
// gradle build should not build the native image
assertThat(build.task(":buildNative")).isNull();
Path buildDir = projectRoot.toPath().resolve("build");
assertThat(buildDir).exists();
assertThat(buildDir.resolve("foo-1.0.0-SNAPSHOT-runner")).doesNotExist();
}

private void createProject(SourceType sourceType) throws IOException {
Expand Down
19 changes: 16 additions & 3 deletions devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
public class QuarkusPlugin implements Plugin<Project> {

public static final String ID = "io.quarkus";
public static final String QUARKUS_PACKAGE_TYPE = "quarkus.package.type";

public static final String EXTENSION_NAME = "quarkus";
public static final String LIST_EXTENSIONS_TASK_NAME = "listExtensions";
public static final String ADD_EXTENSION_TASK_NAME = "addExtension";
public static final String QUARKUS_BUILD_TASK_NAME = "quarkusBuild";
public static final String GENERATE_CONFIG_TASK_NAME = "generateConfig";
public static final String QUARKUS_DEV_TASK_NAME = "quarkusDev";

@Deprecated
public static final String BUILD_NATIVE_TASK_NAME = "buildNative";
public static final String TEST_NATIVE_TASK_NAME = "testNative";
public static final String QUARKUS_TEST_CONFIG_TASK_NAME = "quarkusTestConfig";
Expand Down Expand Up @@ -68,10 +71,12 @@ private void registerTasks(Project project) {
tasks.create(GENERATE_CONFIG_TASK_NAME, QuarkusGenerateConfig.class);

Task quarkusBuild = tasks.create(QUARKUS_BUILD_TASK_NAME, QuarkusBuild.class);
Task quarkusDev = tasks.create(QUARKUS_DEV_TASK_NAME, QuarkusDev.class);
Task buildNative = tasks.create(BUILD_NATIVE_TASK_NAME, QuarkusNative.class);
Task quarkusDev = tasks.create(QUARKUS_DEV_TASK_NAME, QuarkusDev.class);
Task quarkusTestConfig = tasks.create(QUARKUS_TEST_CONFIG_TASK_NAME, QuarkusTestConfig.class);

configureBuildNativeTask(project);

project.getPlugins().withType(
BasePlugin.class,
basePlugin -> tasks.getByName(BasePlugin.ASSEMBLE_TASK_NAME).dependsOn(quarkusBuild));
Expand Down Expand Up @@ -111,9 +116,8 @@ private void registerTasks(Project project) {
.extendsFrom(configurations.findByName(JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME));

Task testNative = tasks.create(TEST_NATIVE_TASK_NAME, QuarkusTestNative.class);
testNative.dependsOn(buildNative);
testNative.dependsOn(quarkusBuild);
testNative.setShouldRunAfter(Collections.singletonList(tasks.findByName(JavaPlugin.TEST_TASK_NAME)));

Consumer<Test> configureTestTask = t -> {
// Quarkus test configuration task which should be executed before any Quarkus test
t.dependsOn(quarkusTestConfig);
Expand All @@ -132,6 +136,15 @@ private void verifyGradleVersion() {
}
}

private void configureBuildNativeTask(Project project) {
project.getGradle().getTaskGraph().whenReady(taskGraph -> {
if (taskGraph.hasTask(project.getPath() + TEST_NATIVE_TASK_NAME)) {
project.getExtensions().getExtraProperties()
.set(QUARKUS_PACKAGE_TYPE, "native");
}
});
}

private void afterEvaluate(Project project) {
project.getConfigurations().getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME)
.getIncoming().getDependencies()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
import io.quarkus.bootstrap.resolver.AppModelResolver;
import io.quarkus.bootstrap.resolver.AppModelResolverException;

/**
* Legacy task for backwards compatibility reasons. This should not be used in new projects
*
* @deprecated This has been replaced by setting quarkus.package.type=native in the configuration.
*/
@Deprecated
public class QuarkusNative extends QuarkusTask {

private boolean reportErrorsAtRuntime = false;
Expand Down Expand Up @@ -355,7 +361,7 @@ public void setReportExceptionStackTraces(boolean reportExceptionStackTraces) {

@TaskAction
public void buildNative() {
getLogger().lifecycle("building native image");
getLogger().warn("The 'buildNative' task has been deprecated in favor of 'build -Dquarkus.package.type=native'");

final AppArtifact appArtifact = extension().getAppArtifact();
final AppModelResolver modelResolver = extension().getAppModelResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ If you want to build an _über-jar_, just add the `--uber-jar` option to the com

## Creating a native executable

You can create a native executable using: `./gradlew buildNative`.
You can create a native executable using: `./gradlew build -Dquarkus.package.type=native`.

Or, if you don't have GraalVM installed, you can run the native executable build in a container using: `./gradlew buildNative --docker-build=true`.
Or, if you don't have GraalVM installed, you can run the native executable build in a container using: `./gradlew build -Dquarkus.package.type=native -Dquarkus.native.container-build=true`.

You can then execute your native executable with: `./build/${project_artifactId}-${project_version}-runner`

Expand Down
48 changes: 4 additions & 44 deletions docs/src/main/asciidoc/gradle-tooling.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ Native executables make Quarkus applications ideal for containers and serverless

Make sure to have `GRAALVM_HOME` configured and pointing to GraalVM version {graalvm-version}.

Create a native executable using: `./gradlew buildNative`.
Create a native executable using: `./gradlew build -Dquarkus.package.type=native`.
A native executable will be present in `build/`.

=== Build a container friendly executable
Expand All @@ -271,18 +271,7 @@ To create an executable that will run in a container, use the following:

[source,shell]
----
./gradlew buildNative
----

==== Customize the build native task

There are situations where it may be required to alter the default values of the `buildNative` task (whose implementation can be found link:https://github.com/quarkusio/quarkus/blob/master/devtools/gradle/src/main/java/io/quarkus/gradle/tasks/QuarkusNative.java[here]).

The easiest way to supply custom configuration is via the command line. For example to use docker to build the native image, simply add the `--docker-build=true` flag like so:

[source,shell]
----
./gradlew buildNative --docker-build=true
./gradlew build -Dquarkus.package.type=native -Dquarkus.native.container-build=true
----

The produced executable will be a 64 bit Linux executable, so depending on your operating system it may no longer be runnable.
Expand All @@ -300,35 +289,6 @@ The list of the available Docker images can be found on https://quay.io/reposito
Be aware that a given Quarkus version might not be compatible with all the images available.
====

Another way of customizing the native build image build process is to configure the task inside the Gradle build script. If for example it is required to set the `enableHttpUrlHandler`, it can be done like so:

[source,groovy]
----
buildNative {
enableHttpUrlHandler = true
}
----

or, if you use the Gradle Kotlin DSL:

[source,kotlin,subs=attributes+]
----
tasks {
named<QuarkusNative>("buildNative") {
setEnableHttpUrlHandler(true)
}
}
----


The native executable would then be produced by executing:

[source,shell]
----
./gradlew buildNative
----


== Running native tests

Run the native tests using:
Expand All @@ -338,7 +298,7 @@ Run the native tests using:
./gradlew testNative
----

This task depends on `buildNative`, so it will generate the native image before running the tests.
This task depends on `quarkusBuild`, so it will generate the native image before running the tests.

== Building Uber-Jars

Expand All @@ -360,4 +320,4 @@ The entries are relative to the root of the generated Uber-Jar. You can specify

== Building with `./gradlew build`

Starting from 1.1.0.Final, `./gradlew build` will no longer build the native image. Use the `buildNative` task explicitly as explained above if needed.
Starting from 1.1.0.Final, `./gradlew build` will no longer build the native image. Add `-Dquarkus.package.type=native` build argument explicitly as explained above if needed.

0 comments on commit 7ad072d

Please sign in to comment.