Search in sources :

Example 1 with JavaApplication

use of org.gradle.api.plugins.JavaApplication in project micronaut-gradle-plugin by micronaut-projects.

the class MicronautAotPlugin method registerJavaExecOptimizedRun.

private void registerJavaExecOptimizedRun(Project project, TaskContainer tasks, TaskProvider<MicronautAotOptimizerTask> prepareJit) {
    TaskProvider<Jar> mainJar = tasks.named("jar", Jar.class);
    TaskProvider<Jar> jarTask = registerOptimizedJar(project, tasks, prepareJit, OptimizerIO.TargetRuntime.JIT);
    ShadowPluginSupport.withShadowPlugin(project, () -> AotShadowSupport.registerShadowJar(project, getArchiveOperations(), tasks, jarTask));
    project.getPlugins().withType(DistributionPlugin.class, p -> registerOptimizedDistribution(project, jarTask));
    project.getPlugins().withType(ApplicationPlugin.class, p -> {
        tasks.register("optimizedRun", JavaExec.class, task -> {
            JavaExec runTask = tasks.named("run", JavaExec.class).get();
            JavaApplication javaApplication = project.getExtensions().getByType(JavaApplication.class);
            JavaPluginConvention javaPluginConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
            SourceSet mainSourceSet = javaPluginConvention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
            task.setGroup(runTask.getGroup());
            task.setDescription("Executes the Micronaut application with AOT optimizations");
            task.getMainClass().convention(javaApplication.getMainClass());
            // https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/385
            task.getOutputs().upToDateWhen(t -> false);
            Set<File> mainSourceSetOutput = mainSourceSet.getOutput().getFiles();
            task.setClasspath(project.files(jarTask, mainSourceSet.getRuntimeClasspath().filter(f -> !mainJar.get().getArchiveFile().get().getAsFile().equals(f) && mainSourceSetOutput.stream().noneMatch(f::equals))));
            task.doFirst(new Action<Task>() {

                @Override
                public void execute(Task t) {
                    if (task.getLogger().isDebugEnabled()) {
                        task.getLogger().debug("Running optimized entry point: " + task.getMainClass().get() + "\nClasspath:\n    " + task.getClasspath().getFiles().stream().map(File::getName).collect(Collectors.joining("\n    ")));
                    }
                }
            });
        });
    });
}
Also used : SourceSet(org.gradle.api.tasks.SourceSet) Task(org.gradle.api.Task) JavaExec(org.gradle.api.tasks.JavaExec) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) JavaApplication(org.gradle.api.plugins.JavaApplication) Jar(org.gradle.api.tasks.bundling.Jar) File(java.io.File) RegularFile(org.gradle.api.file.RegularFile)

Example 2 with JavaApplication

use of org.gradle.api.plugins.JavaApplication in project micronaut-gradle-plugin by micronaut-projects.

the class MicronautAotPlugin method registerDockerImage.

@SuppressWarnings("unchecked")
private void registerDockerImage(Project project, TaskProvider<Jar> optimizedJar, OptimizerIO.TargetRuntime runtime) {
    MicronautExtension micronautExtension = project.getExtensions().getByType(MicronautExtension.class);
    NamedDomainObjectContainer<MicronautDockerImage> dockerImages = (NamedDomainObjectContainer<MicronautDockerImage>) micronautExtension.getExtensions().getByName("dockerImages");
    TaskContainer tasks = project.getTasks();
    TaskProvider<Jar> optimizedRunnerJar = tasks.register("optimizedRunner" + runtime.getCapitalizedName() + "Jar", Jar.class, jar -> {
        jar.from(getArchiveOperations().zipTree(optimizedJar.map(Jar::getArchiveFile)));
        jar.getArchiveClassifier().set("optimized-runner");
        jar.manifest(manifest -> {
            JavaApplication javaApplication = project.getExtensions().getByType(JavaApplication.class);
            Attributes attrs = manifest.getAttributes();
            attrs.put("Main-Class", javaApplication.getMainClass());
            attrs.put("Class-Path", project.getProviders().provider(() -> {
                List<String> classpath = new ArrayList<>();
                Configuration runtimeClasspath = project.getConfigurations().getByName(RUNTIME_CLASSPATH_CONFIGURATION_NAME);
                for (File file : runtimeClasspath) {
                    classpath.add("libs/" + file.getName());
                }
                classpath.add("resources/");
                classpath.add("classes/");
                return String.join(" ", classpath);
            }));
            manifest.attributes(attrs);
        });
    });
    MicronautDockerImage optimized = dockerImages.findByName("optimized");
    if (optimized != null) {
        // only consider the main app
        optimized.addLayer(layer -> {
            layer.getLayerKind().set(LayerKind.APP);
            layer.getRuntimeKind().set(runtime == OptimizerIO.TargetRuntime.JIT ? RuntimeKind.JIT : RuntimeKind.NATIVE);
            layer.getFiles().from(optimizedRunnerJar);
        });
    } else {
        dockerImages.create("optimized", image -> {
            image.addLayer(layer -> {
                layer.getLayerKind().set(LayerKind.APP);
                layer.getRuntimeKind().set(runtime == OptimizerIO.TargetRuntime.JIT ? RuntimeKind.JIT : RuntimeKind.NATIVE);
                layer.getFiles().from(optimizedRunnerJar);
            });
            image.addLayer(layer -> {
                layer.getLayerKind().set(LayerKind.LIBS);
                layer.getFiles().from(layer.getFiles().from(project.getConfigurations().getByName(RUNTIME_CLASSPATH_CONFIGURATION_NAME)));
            });
        });
    }
}
Also used : Configuration(org.gradle.api.artifacts.Configuration) MicronautExtension(io.micronaut.gradle.MicronautExtension) Attributes(org.gradle.api.java.archives.Attributes) NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) TaskContainer(org.gradle.api.tasks.TaskContainer) MicronautDockerImage(io.micronaut.gradle.docker.model.MicronautDockerImage) JavaApplication(org.gradle.api.plugins.JavaApplication) Jar(org.gradle.api.tasks.bundling.Jar) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) RegularFile(org.gradle.api.file.RegularFile)

Example 3 with JavaApplication

use of org.gradle.api.plugins.JavaApplication in project micronaut-gradle-plugin by micronaut-projects.

the class MicronautAotPlugin method registerOptimizedDistribution.

private void registerOptimizedDistribution(Project project, TaskProvider<Jar> optimizedJar) {
    DistributionContainer distributions = project.getExtensions().getByType(DistributionContainer.class);
    ApplicationPluginConvention appConvention = project.getConvention().getPlugin(ApplicationPluginConvention.class);
    ConfigurableFileCollection classpath = project.getObjects().fileCollection();
    classpath.from(optimizedJar);
    classpath.from(project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME));
    TaskProvider<CreateStartScripts> startScripts = project.getTasks().register("create" + capitalize(OPTIMIZED_DIST_NAME) + "StartScripts", CreateStartScripts.class, task -> {
        JavaApplication javaApplication = project.getExtensions().getByType(JavaApplication.class);
        task.setDescription("Creates OS specific scripts to run the AOT optimized application as a JVM application.");
        task.setClasspath(classpath);
        task.getMainClass().set(javaApplication.getMainClass());
        task.getConventionMapping().map("applicationName", appConvention::getApplicationName);
        task.getConventionMapping().map("outputDir", () -> new File(project.getBuildDir(), "optimizedScripts"));
        task.getConventionMapping().map("executableDir", appConvention::getExecutableDir);
        task.getConventionMapping().map("defaultJvmOpts", appConvention::getApplicationDefaultJvmArgs);
    });
    distributions.register(OPTIMIZED_DIST_NAME, dist -> dist.contents(contents -> {
        contents.into("bin", spec -> spec.from(startScripts));
        contents.into("lib", spec -> spec.from(classpath));
    }));
}
Also used : ConfigurableFileCollection(org.gradle.api.file.ConfigurableFileCollection) Arrays(java.util.Arrays) JavaPlugin(org.gradle.api.plugins.JavaPlugin) MicronautDockerPlugin(io.micronaut.gradle.docker.MicronautDockerPlugin) NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) ApplicationPlugin(org.gradle.api.plugins.ApplicationPlugin) Task(org.gradle.api.Task) Provider(org.gradle.api.provider.Provider) RuntimeKind(io.micronaut.gradle.docker.model.RuntimeKind) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) TaskProvider(org.gradle.api.tasks.TaskProvider) NativeImageOptions(org.graalvm.buildtools.gradle.dsl.NativeImageOptions) MicronautExtension(io.micronaut.gradle.MicronautExtension) GraalVMExtension(org.graalvm.buildtools.gradle.dsl.GraalVMExtension) FileCopyDetails(org.gradle.api.file.FileCopyDetails) RUNTIME_CLASSPATH_CONFIGURATION_NAME(org.gradle.api.plugins.JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME) Project(org.gradle.api.Project) Set(java.util.Set) MicronautDockerImage(io.micronaut.gradle.docker.model.MicronautDockerImage) Collectors(java.util.stream.Collectors) List(java.util.List) TaskContainer(org.gradle.api.tasks.TaskContainer) ArchiveOperations(org.gradle.api.file.ArchiveOperations) MicronautComponentPlugin(io.micronaut.gradle.MicronautComponentPlugin) NotNull(org.jetbrains.annotations.NotNull) StringGroovyMethods.capitalize(org.codehaus.groovy.runtime.StringGroovyMethods.capitalize) Attributes(org.gradle.api.java.archives.Attributes) DistributionContainer(org.gradle.api.distribution.DistributionContainer) Logger(org.gradle.api.logging.Logger) SourceSet(org.gradle.api.tasks.SourceSet) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) Configuration(org.gradle.api.artifacts.Configuration) NativeImagePlugin(org.graalvm.buildtools.gradle.NativeImagePlugin) RelativePath(org.gradle.api.file.RelativePath) AttributeContainer(org.gradle.api.attributes.AttributeContainer) Attribute(org.gradle.api.attributes.Attribute) LayerKind(io.micronaut.gradle.docker.model.LayerKind) Directory(org.gradle.api.file.Directory) Jar(org.gradle.api.tasks.bundling.Jar) Action(org.gradle.api.Action) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) Files(java.nio.file.Files) IOException(java.io.IOException) MicronautBasePlugin(io.micronaut.gradle.MicronautBasePlugin) JavaApplication(org.gradle.api.plugins.JavaApplication) File(java.io.File) ApplicationPluginConvention(org.gradle.api.plugins.ApplicationPluginConvention) ProviderFactory(org.gradle.api.provider.ProviderFactory) ShadowPluginSupport(io.micronaut.gradle.ShadowPluginSupport) RegularFile(org.gradle.api.file.RegularFile) DistributionPlugin(org.gradle.api.distribution.plugins.DistributionPlugin) JavaExec(org.gradle.api.tasks.JavaExec) CreateStartScripts(org.gradle.api.tasks.application.CreateStartScripts) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) JavaApplication(org.gradle.api.plugins.JavaApplication) ApplicationPluginConvention(org.gradle.api.plugins.ApplicationPluginConvention) ConfigurableFileCollection(org.gradle.api.file.ConfigurableFileCollection) CreateStartScripts(org.gradle.api.tasks.application.CreateStartScripts) File(java.io.File) RegularFile(org.gradle.api.file.RegularFile) DistributionContainer(org.gradle.api.distribution.DistributionContainer)

Example 4 with JavaApplication

use of org.gradle.api.plugins.JavaApplication in project micronaut-gradle-plugin by micronaut-projects.

the class AotShadowSupport method registerShadowJar.

static void registerShadowJar(Project project, ArchiveOperations archiveOperations, TaskContainer tasks, TaskProvider<Jar> optimizedJar) {
    project.afterEvaluate(unused -> {
        JavaApplication javaApplication = project.getExtensions().findByType(JavaApplication.class);
        TaskProvider<ShadowJar> shadowProvider = tasks.register(optimizedJar.getName() + "All", ShadowJar.class, shadow -> {
            shadow.setGroup(SHADOW_GROUP);
            shadow.setDescription("Creates a fat jar including the Micronaut AOT optimizations");
            shadow.getArchiveClassifier().convention("all-optimized");
            Jar mainJar = tasks.named("jar", Jar.class).get();
            shadow.getManifest().inheritFrom(mainJar.getManifest());
            if (javaApplication != null) {
                // This is the reason why we use an afterEvalute:
                // The shadow plugin apparently does something with attributes,
                // breaking support for providers
                shadow.getManifest().getAttributes().put("Main-Class", javaApplication.getMainClass().get());
            }
            shadow.from(optimizedJar.map(jar -> archiveOperations.zipTree(jar.getArchiveFile().get())));
            shadow.getConfigurations().add(project.getConfigurations().findByName("runtimeClasspath"));
            shadow.getExcludes().addAll(tasks.named(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME, ShadowJar.class).get().getExcludes());
        });
        tasks.named("assemble").configure(assemble -> assemble.dependsOn(shadowProvider));
    });
}
Also used : ShadowJar(com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) Jar(org.gradle.api.tasks.bundling.Jar) TaskContainer(org.gradle.api.tasks.TaskContainer) Project(org.gradle.api.Project) TaskProvider(org.gradle.api.tasks.TaskProvider) ArchiveOperations(org.gradle.api.file.ArchiveOperations) ShadowJavaPlugin(com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin) JavaApplication(org.gradle.api.plugins.JavaApplication) SHADOW_GROUP(com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.SHADOW_GROUP) ShadowJar(com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) JavaApplication(org.gradle.api.plugins.JavaApplication) ShadowJar(com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) Jar(org.gradle.api.tasks.bundling.Jar)

Example 5 with JavaApplication

use of org.gradle.api.plugins.JavaApplication in project micronaut-gradle-plugin by micronaut-projects.

the class NativeImageDockerfile method buildActualCommandLine.

protected List<String> buildActualCommandLine(Provider<String> executable, DockerBuildStrategy buildStrategy, BaseImageForBuildStrategyResolver imageResolver) {
    NativeImageOptions options = newNativeImageOptions("actualDockerOptions");
    prepareNativeImageOptions(options);
    if (buildStrategy == DockerBuildStrategy.ORACLE_FUNCTION) {
        options.getMainClass().set("com.fnproject.fn.runtime.EntryPoint");
        options.getBuildArgs().add("--report-unsupported-elements-at-runtime");
    } else if (buildStrategy == DockerBuildStrategy.LAMBDA) {
        JavaApplication javaApplication = getProject().getExtensions().getByType(JavaApplication.class);
        if (!javaApplication.getMainClass().isPresent()) {
            options.getMainClass().set("io.micronaut.function.aws.runtime.MicronautLambdaRuntime");
        }
        if (!options.getMainClass().isPresent()) {
            options.getMainClass().set("io.micronaut.function.aws.runtime.MicronautLambdaRuntime");
        }
    }
    List<String> commandLine = new ArrayList<>();
    commandLine.add("native-image");
    List<String> args = buildNativeImageCommandLineArgs(executable, options);
    commandLine.addAll(args);
    String baseImage = imageResolver.get();
    // add --static if image is scratch
    if (baseImage.equalsIgnoreCase("scratch") && !commandLine.contains("--static")) {
        commandLine.add("--static");
    }
    // to build a "mostly" static native-image if image when using distroless
    if (baseImage.contains("distroless") && !commandLine.contains("-H:+StaticExecutableWithDynamicLibC")) {
        commandLine.add("-H:+StaticExecutableWithDynamicLibC");
    }
    return commandLine;
}
Also used : NativeImageOptions(org.graalvm.buildtools.gradle.dsl.NativeImageOptions) BaseNativeImageOptions(org.graalvm.buildtools.gradle.internal.BaseNativeImageOptions) JavaApplication(org.gradle.api.plugins.JavaApplication) ArrayList(java.util.ArrayList)

Aggregations

JavaApplication (org.gradle.api.plugins.JavaApplication)11 ArrayList (java.util.ArrayList)5 File (java.io.File)4 List (java.util.List)4 RegularFile (org.gradle.api.file.RegularFile)4 Jar (org.gradle.api.tasks.bundling.Jar)4 Project (org.gradle.api.Project)3 Configuration (org.gradle.api.artifacts.Configuration)3 SourceSet (org.gradle.api.tasks.SourceSet)3 TaskContainer (org.gradle.api.tasks.TaskContainer)3 MicronautExtension (io.micronaut.gradle.MicronautExtension)2 MicronautDockerImage (io.micronaut.gradle.docker.model.MicronautDockerImage)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 NativeImageOptions (org.graalvm.buildtools.gradle.dsl.NativeImageOptions)2 NamedDomainObjectContainer (org.gradle.api.NamedDomainObjectContainer)2 Task (org.gradle.api.Task)2 ArchiveOperations (org.gradle.api.file.ArchiveOperations)2 Attributes (org.gradle.api.java.archives.Attributes)2 JavaPluginConvention (org.gradle.api.plugins.JavaPluginConvention)2