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 ")));
}
}
});
});
});
}
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)));
});
});
}
}
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));
}));
}
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));
});
}
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;
}
Aggregations