use of io.micronaut.gradle.MicronautRuntime in project micronaut-gradle-plugin by micronaut-projects.
the class MicronautDockerPlugin method configureNativeDockerBuild.
private TaskProvider<NativeImageDockerfile> configureNativeDockerBuild(Project project, TaskContainer tasks, TaskProvider<BuildLayersTask> buildLayersTask, String imageName) {
File f = project.file(adaptTaskName("DockerfileNative", imageName));
TaskProvider<NativeImageDockerfile> dockerFileTask;
String dockerfileNativeTaskName = adaptTaskName("dockerfileNative", imageName);
Provider<RegularFile> targetDockerFile = project.getLayout().getBuildDirectory().file("docker/native-" + imageName + "/DockerfileNative");
if (f.exists()) {
dockerFileTask = tasks.register(dockerfileNativeTaskName, NativeImageDockerfile.class, task -> {
task.setGroup(BasePlugin.BUILD_GROUP);
task.setDescription("Builds a Native Docker File for image " + imageName);
task.instructionsFromTemplate(f);
task.getDestFile().set(targetDockerFile);
});
} else {
dockerFileTask = tasks.register(dockerfileNativeTaskName, NativeImageDockerfile.class, task -> {
task.setGroup(BasePlugin.BUILD_GROUP);
task.setDescription("Builds a Native Docker File for image " + imageName);
task.getDestFile().set(targetDockerFile);
});
}
TaskProvider<PrepareDockerContext> prepareContext = tasks.register(adaptTaskName("dockerPrepareContext", imageName), PrepareDockerContext.class, context -> {
// Because docker requires all files to be found in the build context we need to
// copy the configuration file directories into the build context
context.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir("docker/native-" + imageName + "/config-dirs"));
context.getInputDirectories().from(dockerFileTask.map(t -> t.getNativeImageOptions().get().getConfigurationFileDirectories()));
});
TaskProvider<DockerBuildImage> dockerBuildTask = tasks.register(adaptTaskName("dockerBuildNative", imageName), DockerBuildImage.class, task -> {
task.setGroup(BasePlugin.BUILD_GROUP);
task.setDescription("Builds a Native Docker Image using GraalVM (image " + imageName + ")");
task.getInputs().files(prepareContext);
if (f.exists()) {
task.getDockerFile().set(f);
} else {
task.getDockerFile().convention(dockerFileTask.flatMap(Dockerfile::getDestFile));
}
task.getImages().set(Collections.singletonList(project.getName()));
task.dependsOn(buildLayersTask);
task.getInputDir().set(dockerFileTask.flatMap(Dockerfile::getDestDir));
});
TaskProvider<DockerPushImage> pushDockerImage = tasks.register(adaptTaskName("dockerPushNative", imageName), DockerPushImage.class);
pushDockerImage.configure(task -> {
task.dependsOn(dockerBuildTask);
task.setGroup("upload");
task.setDescription("Pushes a Native Docker Image using GraalVM (image " + imageName + ")");
task.getImages().set(dockerBuildTask.flatMap(DockerBuildImage::getImages));
});
project.afterEvaluate(p -> {
MicronautRuntime mr = PluginsHelper.resolveRuntime(p);
if (mr.isLambdaProvided()) {
TaskContainer taskContainer = p.getTasks();
TaskProvider<DockerCreateContainer> createLambdaContainer = taskContainer.register(adaptTaskName("createLambdaContainer", imageName), DockerCreateContainer.class, task -> {
task.dependsOn(dockerBuildTask);
task.targetImageId(dockerBuildTask.flatMap(DockerBuildImage::getImageId));
});
TaskProvider<DockerCopyFileFromContainer> buildLambdaZip = taskContainer.register(adaptTaskName("buildNativeLambda", imageName), DockerCopyFileFromContainer.class);
Provider<String> lambdaZip = project.getLayout().getBuildDirectory().dir("libs").map(dir -> dir.file(project.getName() + "-" + project.getVersion() + "-" + simpleNameOf("lambda", imageName) + ".zip").getAsFile().getAbsolutePath());
TaskProvider<DockerRemoveContainer> removeContainer = taskContainer.register(adaptTaskName("destroyLambdaContainer", imageName), DockerRemoveContainer.class);
removeContainer.configure(task -> {
task.mustRunAfter(buildLambdaZip);
task.getContainerId().set(createLambdaContainer.flatMap(DockerCreateContainer::getContainerId));
});
buildLambdaZip.configure(task -> {
task.dependsOn(createLambdaContainer);
task.getContainerId().set(createLambdaContainer.flatMap(DockerCreateContainer::getContainerId));
task.getRemotePath().set("/function/function.zip");
task.getHostPath().set(lambdaZip);
task.doLast(new Action<Task>() {
@Override
public void execute(Task task1) {
System.out.println("AWS Lambda ZIP built: " + lambdaZip.get());
}
});
task.finalizedBy(removeContainer);
});
}
});
return dockerFileTask;
}
use of io.micronaut.gradle.MicronautRuntime in project micronaut-gradle-plugin by micronaut-projects.
the class MicronautDockerPlugin method withBuildStrategy.
private void withBuildStrategy(Project project, Consumer<? super Optional<DockerBuildStrategy>> action) {
project.afterEvaluate(eval -> {
Optional<DockerBuildStrategy> buildStrategy;
MicronautRuntime mr = PluginsHelper.resolveRuntime(project);
if (mr != MicronautRuntime.NONE) {
buildStrategy = Optional.of(mr.getBuildStrategy());
} else {
buildStrategy = Optional.empty();
}
action.accept(buildStrategy);
});
}
use of io.micronaut.gradle.MicronautRuntime in project micronaut-gradle-plugin by micronaut-projects.
the class MicronautGraalPlugin method apply.
@Override
public void apply(Project project) {
project.getPluginManager().apply(NativeImagePlugin.class);
workaroundForResourcesDirectoryMissing(project);
project.getPluginManager().withPlugin("io.micronaut.minimal.library", plugin -> {
MicronautExtension extension = project.getExtensions().findByType(MicronautExtension.class);
configureAnnotationProcessing(project, extension);
});
project.getPluginManager().withPlugin("io.micronaut.minimal.application", plugin -> {
MicronautExtension extension = project.getExtensions().findByType(MicronautExtension.class);
configureAnnotationProcessing(project, extension);
});
GraalVMExtension graal = project.getExtensions().findByType(GraalVMExtension.class);
graal.getBinaries().configureEach(options -> options.resources(rsrc -> rsrc.autodetection(inf -> {
inf.getEnabled().convention(true);
inf.getIgnoreExistingResourcesConfigFile().convention(true);
inf.getRestrictToProjectDependencies().convention(true);
})));
project.getPluginManager().withPlugin("application", plugin -> {
TaskContainer tasks = project.getTasks();
tasks.withType(BuildNativeImageTask.class).named("nativeCompile", nativeImageTask -> {
MicronautRuntime mr = PluginsHelper.resolveRuntime(project);
if (mr.isLambdaProvided()) {
DependencySet implementation = project.getConfigurations().getByName("implementation").getDependencies();
boolean isAwsApp = implementation.stream().noneMatch(dependency -> Objects.equals(dependency.getGroup(), "io.micronaut.aws") && dependency.getName().equals("micronaut-function-aws"));
if (isAwsApp) {
nativeImageTask.getOptions().get().getMainClass().set("io.micronaut.function.aws.runtime.MicronautLambdaRuntime");
}
}
});
// We use `afterEvaluate` here in order to preserve laziness of task configuration
// and because there is no API to allow reacting to registration of tasks.
Set<String> alreadyRegisteredTaskNames = new HashSet<>();
project.afterEvaluate(p -> tasks.withType(Test.class).getCollectionSchema().getElements().forEach(element -> {
String testName = element.getName();
registerTestAgainstNativeImageTask(alreadyRegisteredTaskNames, tasks, testName);
}));
});
}
Aggregations