Search in sources :

Example 1 with MicronautRuntime

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;
}
Also used : Dockerfile(com.bmuschko.gradle.docker.tasks.image.Dockerfile) DockerPushImage(com.bmuschko.gradle.docker.tasks.image.DockerPushImage) HashMap(java.util.HashMap) DefaultMicronautDockerImage(io.micronaut.gradle.docker.model.DefaultMicronautDockerImage) NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) BasePlugin(org.gradle.api.plugins.BasePlugin) FileCollection(org.gradle.api.file.FileCollection) DockerExtension(com.bmuschko.gradle.docker.DockerExtension) SourceSet(org.gradle.api.tasks.SourceSet) ArrayList(java.util.ArrayList) Configuration(org.gradle.api.artifacts.Configuration) Task(org.gradle.api.Task) Provider(org.gradle.api.provider.Provider) RuntimeKind(io.micronaut.gradle.docker.model.RuntimeKind) DockerRemoveContainer(com.bmuschko.gradle.docker.tasks.container.DockerRemoveContainer) PrepareDockerContext(io.micronaut.gradle.docker.tasks.PrepareDockerContext) TaskProvider(org.gradle.api.tasks.TaskProvider) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) Map(java.util.Map) ExtensionContainer(org.gradle.api.plugins.ExtensionContainer) MicronautExtension(io.micronaut.gradle.MicronautExtension) LayerKind(io.micronaut.gradle.docker.model.LayerKind) Strings.capitalize(io.micronaut.gradle.Strings.capitalize) RUNTIME_CLASSPATH_CONFIGURATION_NAME(org.gradle.api.plugins.JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME) Jar(org.gradle.api.tasks.bundling.Jar) Action(org.gradle.api.Action) Project(org.gradle.api.Project) DockerBuildImage(com.bmuschko.gradle.docker.tasks.image.DockerBuildImage) PluginsHelper(io.micronaut.gradle.PluginsHelper) MicronautBasePlugin(io.micronaut.gradle.MicronautBasePlugin) MicronautDockerImage(io.micronaut.gradle.docker.model.MicronautDockerImage) DockerCopyFileFromContainer(com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer) JavaApplication(org.gradle.api.plugins.JavaApplication) File(java.io.File) Consumer(java.util.function.Consumer) List(java.util.List) TaskContainer(org.gradle.api.tasks.TaskContainer) RegularFile(org.gradle.api.file.RegularFile) MicronautRuntime(io.micronaut.gradle.MicronautRuntime) Optional(java.util.Optional) BuildLayersTask(io.micronaut.gradle.docker.tasks.BuildLayersTask) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) DockerCreateContainer(com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer) Task(org.gradle.api.Task) BuildLayersTask(io.micronaut.gradle.docker.tasks.BuildLayersTask) DockerBuildImage(com.bmuschko.gradle.docker.tasks.image.DockerBuildImage) DockerCopyFileFromContainer(com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer) MicronautRuntime(io.micronaut.gradle.MicronautRuntime) RegularFile(org.gradle.api.file.RegularFile) DockerCreateContainer(com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer) DockerRemoveContainer(com.bmuschko.gradle.docker.tasks.container.DockerRemoveContainer) PrepareDockerContext(io.micronaut.gradle.docker.tasks.PrepareDockerContext) TaskContainer(org.gradle.api.tasks.TaskContainer) DockerPushImage(com.bmuschko.gradle.docker.tasks.image.DockerPushImage) File(java.io.File) RegularFile(org.gradle.api.file.RegularFile)

Example 2 with MicronautRuntime

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);
    });
}
Also used : MicronautRuntime(io.micronaut.gradle.MicronautRuntime)

Example 3 with MicronautRuntime

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);
        }));
    });
}
Also used : ProcessResources(org.gradle.language.jvm.tasks.ProcessResources) Arrays(java.util.Arrays) BuildNativeImageTask(org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask) ListProperty(org.gradle.api.provider.ListProperty) DependencySet(org.gradle.api.artifacts.DependencySet) CommandLineArgumentProvider(org.gradle.process.CommandLineArgumentProvider) SourceSet(org.gradle.api.tasks.SourceSet) HashSet(java.util.HashSet) NativeImagePlugin(org.graalvm.buildtools.gradle.NativeImagePlugin) Provider(org.gradle.api.provider.Provider) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) PathSensitivity(org.gradle.api.tasks.PathSensitivity) MicronautExtension(io.micronaut.gradle.MicronautExtension) GraalVMExtension(org.graalvm.buildtools.gradle.dsl.GraalVMExtension) Test(org.gradle.api.tasks.testing.Test) Project(org.gradle.api.Project) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) PluginsHelper(io.micronaut.gradle.PluginsHelper) Set(java.util.Set) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) TaskContainer(org.gradle.api.tasks.TaskContainer) RegularFile(org.gradle.api.file.RegularFile) InputFile(org.gradle.api.tasks.InputFile) PathSensitive(org.gradle.api.tasks.PathSensitive) MicronautRuntime(io.micronaut.gradle.MicronautRuntime) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) GraalVMExtension(org.graalvm.buildtools.gradle.dsl.GraalVMExtension) TaskContainer(org.gradle.api.tasks.TaskContainer) BuildNativeImageTask(org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask) MicronautExtension(io.micronaut.gradle.MicronautExtension) MicronautRuntime(io.micronaut.gradle.MicronautRuntime) DependencySet(org.gradle.api.artifacts.DependencySet) HashSet(java.util.HashSet)

Aggregations

MicronautRuntime (io.micronaut.gradle.MicronautRuntime)3 MicronautExtension (io.micronaut.gradle.MicronautExtension)2 PluginsHelper (io.micronaut.gradle.PluginsHelper)2 File (java.io.File)2 Collections (java.util.Collections)2 Plugin (org.gradle.api.Plugin)2 Project (org.gradle.api.Project)2 RegularFile (org.gradle.api.file.RegularFile)2 Provider (org.gradle.api.provider.Provider)2 SourceSet (org.gradle.api.tasks.SourceSet)2 SourceSetContainer (org.gradle.api.tasks.SourceSetContainer)2 TaskContainer (org.gradle.api.tasks.TaskContainer)2 DockerExtension (com.bmuschko.gradle.docker.DockerExtension)1 DockerCopyFileFromContainer (com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer)1 DockerCreateContainer (com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer)1 DockerRemoveContainer (com.bmuschko.gradle.docker.tasks.container.DockerRemoveContainer)1 DockerBuildImage (com.bmuschko.gradle.docker.tasks.image.DockerBuildImage)1 DockerPushImage (com.bmuschko.gradle.docker.tasks.image.DockerPushImage)1 Dockerfile (com.bmuschko.gradle.docker.tasks.image.Dockerfile)1 MicronautBasePlugin (io.micronaut.gradle.MicronautBasePlugin)1