use of org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask in project micronaut-gradle-plugin by micronaut-projects.
the class MicronautGraalPlugin method registerTestAgainstNativeImageTask.
/**
* For each `Test` task, we register a new `testNativeImage` task which tests _against_ the native image
* server. Note that this is different from the `nativeTest` task that the GraalVM Gradle plugin provides,
* as the latter executes all tests _within_ the native image.
*/
private void registerTestAgainstNativeImageTask(Set<String> alreadyRegistered, TaskContainer tasks, String testName) {
if (alreadyRegistered.add(testName)) {
tasks.register(testName + "NativeImage", Test.class, nativeImageTestTask -> {
Test testTask = (Test) tasks.getByName(testName);
nativeImageTestTask.setClasspath(testTask.getClasspath());
nativeImageTestTask.getJavaLauncher().set(testTask.getJavaLauncher());
BuildNativeImageTask nativeBuild = (BuildNativeImageTask) tasks.findByName("nativeCompile");
nativeImageTestTask.setForkEvery(testTask.getForkEvery());
nativeImageTestTask.setTestClassesDirs(testTask.getTestClassesDirs());
nativeImageTestTask.getJvmArgumentProviders().add(new CommandLineArgumentProvider() {
@InputFile
@PathSensitive(PathSensitivity.RELATIVE)
Provider<RegularFile> getInputFile() {
return nativeBuild.getOutputFile();
}
@Override
public Iterable<String> asArguments() {
return Collections.singleton("-Dmicronaut.test.server.executable=" + getInputFile().get().getAsFile().getAbsolutePath());
}
});
nativeImageTestTask.setDescription("Runs tests against a native image build of the server. Requires the server to allow the port to configurable with 'micronaut.server.port'.");
});
}
}
use of org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask in project native-build-tools by graalvm.
the class NativeImagePlugin method configureAutomaticTaskCreation.
private void configureAutomaticTaskCreation(Project project, GraalVMExtension graalExtension, Map<String, Provider<Boolean>> agents, TaskContainer tasks, SourceSetContainer sourceSets) {
graalExtension.getBinaries().configureEach(options -> {
String binaryName = options.getName();
String compileTaskName = deriveTaskName(binaryName, "native", "Compile");
if ("main".equals(binaryName)) {
compileTaskName = NATIVE_COMPILE_TASK_NAME;
}
Provider<Boolean> agent = agentPropertyOverride(project, options);
agents.put(binaryName, agent);
TaskProvider<BuildNativeImageTask> imageBuilder = tasks.register(compileTaskName, BuildNativeImageTask.class, builder -> {
builder.setDescription("Compiles a native image for the " + options.getName() + " binary");
builder.setGroup(LifecycleBasePlugin.BUILD_GROUP);
builder.getOptions().convention(options);
builder.getAgentEnabled().set(agent);
builder.getUseArgFile().convention(graalExtension.getUseArgFile());
});
String runTaskName = deriveTaskName(binaryName, "native", "Run");
if ("main".equals(binaryName)) {
runTaskName = NativeRunTask.TASK_NAME;
} else if (binaryName.toLowerCase(Locale.US).endsWith("test")) {
runTaskName = "native" + capitalize(binaryName);
}
tasks.register(runTaskName, NativeRunTask.class, task -> {
task.setGroup(LifecycleBasePlugin.BUILD_GROUP);
task.setDescription("Executes the " + options.getName() + " native binary");
task.getImage().convention(imageBuilder.map(t -> t.getOutputFile().get()));
task.getRuntimeArgs().convention(options.getRuntimeArgs());
});
configureClasspathJarFor(tasks, options, imageBuilder);
SourceSet sourceSet = "test".equals(binaryName) ? sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME) : sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
TaskProvider<GenerateResourcesConfigFile> generateResourcesConfig = registerResourcesConfigTask(graalExtension.getGeneratedResourcesDirectory(), options, tasks, transitiveProjectArtifacts(project, sourceSet.getRuntimeClasspathConfigurationName()), deriveTaskName(binaryName, "generate", "ResourcesConfigFile"));
options.getConfigurationFileDirectories().from(generateResourcesConfig.map(t -> t.getOutputFile().map(f -> f.getAsFile().getParentFile())));
configureJvmReachabilityConfigurationDirectories(project, graalExtension, options, sourceSet);
});
}
use of org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask in project native-build-tools by graalvm.
the class NativeImagePlugin method registerTestBinary.
public void registerTestBinary(Project project, DefaultGraalVmExtension graalExtension, DefaultTestBinaryConfig config) {
NativeImageOptions mainOptions = graalExtension.getBinaries().getByName("main");
String name = config.getName();
boolean isPrimaryTest = "test".equals(name);
TaskContainer tasks = project.getTasks();
// Testing part begins here. -------------------------------------------
// In future Gradle releases this becomes a proper DirectoryProperty
File testResultsDir = GradleUtils.getJavaPluginConvention(project).getTestResultsDir();
DirectoryProperty testListDirectory = project.getObjects().directoryProperty();
// Add DSL extension for testing
NativeImageOptions testOptions = createTestOptions(graalExtension, name, project, mainOptions, testListDirectory, config.getSourceSet());
if (isPrimaryTest) {
deprecateExtension(project, testOptions, DEPRECATED_NATIVE_TEST_EXTENSION, "test");
}
TaskProvider<Test> testTask = config.validate().getTestTask();
testOptions.getAgent().getInstrumentedTask().set(testTask);
testTask.configure(test -> {
File testList = new File(testResultsDir, test.getName() + "/testlist");
testListDirectory.set(testList);
test.getOutputs().dir(testList);
// Set system property read by the UniqueIdTrackingListener.
test.systemProperty(JUNIT_PLATFORM_LISTENERS_UID_TRACKING_ENABLED, true);
TrackingDirectorySystemPropertyProvider directoryProvider = project.getObjects().newInstance(TrackingDirectorySystemPropertyProvider.class);
directoryProvider.getDirectory().set(testListDirectory);
test.getJvmArgumentProviders().add(directoryProvider);
test.doFirst("cleanup test ids", new CleanupTestIdsDirectory(testListDirectory));
});
// Following ensures that required feature jar is on classpath for every project
injectTestPluginDependencies(project, graalExtension.getTestSupport());
TaskProvider<BuildNativeImageTask> testImageBuilder = tasks.named(deriveTaskName(name, "native", "Compile"), BuildNativeImageTask.class, task -> {
task.setOnlyIf(t -> graalExtension.getTestSupport().get());
task.getTestListDirectory().set(testListDirectory);
testTask.get();
ConfigurableFileCollection testList = project.getObjects().fileCollection();
// Later this will be replaced by a dedicated task not requiring execution of tests
testList.from(testListDirectory).builtBy(testTask);
testOptions.getClasspath().from(testList);
});
if (isPrimaryTest) {
tasks.register(DEPRECATED_NATIVE_TEST_BUILD_TASK, t -> {
t.dependsOn(testImageBuilder);
t.doFirst("Warn about deprecation", task -> task.getLogger().warn("Task " + DEPRECATED_NATIVE_TEST_BUILD_TASK + " is deprecated. Use " + NATIVE_TEST_COMPILE_TASK_NAME + " instead."));
});
}
tasks.named(isPrimaryTest ? NATIVE_TEST_TASK_NAME : "native" + capitalize(name), NativeRunTask.class, task -> {
task.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
task.setOnlyIf(t -> graalExtension.getTestSupport().get());
});
}
use of org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask in project native-build-tools by graalvm.
the class NativeImagePlugin method configureJavaProject.
private void configureJavaProject(Project project, Provider<NativeImageService> nativeImageServiceProvider, DefaultGraalVmExtension graalExtension) {
logger.log("====================");
logger.log("Initializing project: " + project.getName());
logger.log("====================");
Map<String, Provider<Boolean>> agents = graalExtension.getAgentProperties();
// Add DSL extensions for building and testing
NativeImageOptions mainOptions = createMainOptions(graalExtension, project);
deprecateExtension(project, mainOptions, DEPRECATED_NATIVE_BUILD_EXTENSION, "main");
project.getPlugins().withId("application", p -> mainOptions.getMainClass().convention(project.getExtensions().findByType(JavaApplication.class).getMainClass()));
project.getPlugins().withId("java-library", p -> mainOptions.getSharedLibrary().convention(true));
registerServiceProvider(project, nativeImageServiceProvider);
// Register Native Image tasks
TaskContainer tasks = project.getTasks();
JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
configureAutomaticTaskCreation(project, graalExtension, agents, tasks, javaConvention.getSourceSets());
TaskProvider<BuildNativeImageTask> imageBuilder = tasks.named(NATIVE_COMPILE_TASK_NAME, BuildNativeImageTask.class);
TaskProvider<Task> deprecatedTask = tasks.register(DEPRECATED_NATIVE_BUILD_TASK, t -> {
t.dependsOn(imageBuilder);
t.doFirst("Warn about deprecation", task -> task.getLogger().warn("Task " + DEPRECATED_NATIVE_BUILD_TASK + " is deprecated. Use " + NATIVE_COMPILE_TASK_NAME + " instead."));
});
// We want to add agent invocation to "run" task, but it is only available when
// Application Plugin is initialized.
project.getPlugins().withType(ApplicationPlugin.class, applicationPlugin -> {
TaskProvider<? extends JavaForkOptions> runTask = tasks.withType(JavaExec.class).named(ApplicationPlugin.TASK_RUN_NAME);
mainOptions.getAgent().getInstrumentedTask().convention(runTask);
});
graalExtension.registerTestBinary("test", config -> {
config.forTestTask(tasks.named("test", Test.class));
config.usingSourceSet(GradleUtils.findSourceSet(project, SourceSet.TEST_SOURCE_SET_NAME));
});
}
Aggregations