Skip to content

Commit

Permalink
Make samples native build fully optional
Browse files Browse the repository at this point in the history
- Add springShellSampleNative project property
- Implement generic samples build logic in buildSrc
- Fixes #873
  • Loading branch information
jvalkeal committed Sep 24, 2023
1 parent 585125d commit da529fa
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
with:
node-version: '16'
- run: |
./gradlew clean build nativeCompile -PspringShellSampleMusl=${{ matrix.musl }} -PspringShellSampleE2E=true
./gradlew clean build nativeCompile -PspringShellSampleMusl=${{ matrix.musl }} -PspringShellSampleNative=true -PspringShellSampleE2E=true
timeout-minutes: 30
- uses: actions/upload-artifact@v3
with:
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ ext {
def properties = new Properties()
properties.load(it)
set("springBootVersion", properties["springBootVersion"])
set("nativeBuildToolsVersion", properties["nativeBuildToolsVersion"])
}
}

dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}"))
implementation "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
implementation("org.springframework:spring-core")
implementation 'org.asciidoctor:asciidoctor-gradle-jvm:3.3.2'
implementation 'org.jfrog.buildinfo:build-info-extractor-gradle:4.29.0'
implementation "org.graalvm.buildtools:native-gradle-plugin:${nativeBuildToolsVersion}"
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,21 +15,75 @@
*/
package org.springframework.shell.gradle;

import org.graalvm.buildtools.gradle.NativeImagePlugin;
import org.graalvm.buildtools.gradle.dsl.GraalVMExtension;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.PluginManager;
import org.springframework.boot.gradle.dsl.SpringBootExtension;
import org.springframework.boot.gradle.plugin.SpringBootPlugin;
import org.springframework.boot.gradle.tasks.bundling.BootJar;

/**
* @author Janne Valkealahti
*/
class SamplePlugin implements Plugin<Project> {

private static final String NATIVE = "springShellSampleNative";
private static final String E2E = "springShellSampleE2E";
private static final String MUSL = "springShellSampleMusl";

@Override
public void apply(Project project) {
PluginManager pluginManager = project.getPluginManager();
pluginManager.apply(JavaPlugin.class);
pluginManager.apply(ManagementConfigurationPlugin.class);
pluginManager.apply(SpringBootPlugin.class);
if (isEnabled(project, NATIVE)) {
pluginManager.apply(NativeImagePlugin.class);
customizeNative(project);
}
configureBootJar(project);
configureBootBuildInfo(project);
new JavaConventions().apply(project);
}

private void configureBootJar(Project project) {
if (isEnabled(project, E2E)) {
project.getTasks().withType(BootJar.class, (bootJar) -> {
String name = String.format("%s.%s", bootJar.getArchiveBaseName().get(),
bootJar.getArchiveExtension().get());
bootJar.getArchiveFileName().set(name);
});
}
}

private void configureBootBuildInfo(Project project) {
SpringBootExtension extension = project.getExtensions().getByType(SpringBootExtension.class);
extension.buildInfo(buildInfo -> {
buildInfo.getExcludes().add("time");
});
}

private void customizeNative(Project project) {
project.getPlugins().withType(NativeImagePlugin.class).all(nativePlugin -> {
configureGraalVmExtension(project);
});
}

private void configureGraalVmExtension(Project project) {
GraalVMExtension extension = project.getExtensions().getByType(GraalVMExtension.class);
if (isEnabled(project, MUSL)) {
extension.getBinaries().getByName(NativeImagePlugin.NATIVE_MAIN_EXTENSION).buildArgs("--static",
"--libc=musl");
}
}

private boolean isEnabled(Project project, String property) {
if (project.hasProperty(property)) {
return Boolean.valueOf(String.valueOf(project.findProperty(property)));
}
return false;
}
}
2 changes: 0 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ pluginManagement {
}
}
plugins {
id 'org.springframework.boot' version "$springBootVersion"
id 'org.graalvm.buildtools.native' version "$nativeBuildToolsVersion"
id 'com.gradle.enterprise' version "$gradleEnterpriseVersion"
id 'io.spring.ge.conventions' version "$springGeConventionsVersion"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
plugins {
id 'org.springframework.shell.sample'
id 'org.springframework.boot'
id 'org.graalvm.buildtools.native'
}

description = 'Spring Shell Sample Catalog'
Expand All @@ -13,29 +11,3 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.awaitility:awaitility'
}

springBoot {
buildInfo {
excludes = ['time']
}
}

if (project.hasProperty('springShellSampleE2E') && springShellSampleE2E.toBoolean()) {
bootJar {
archiveFileName = "${archiveBaseName.get()}.${archiveExtension.get()}"
}
}

graalvmNative {
metadataRepository {
enabled = true
}
binaries {
main {
if (project.hasProperty('springShellSampleMusl') && springShellSampleMusl.toBoolean()) {
buildArgs.add('--static')
buildArgs.add('--libc=musl')
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
plugins {
id 'org.springframework.shell.sample'
id 'org.springframework.boot'
id 'org.graalvm.buildtools.native'
}

description = 'Spring Shell Sample Commands'
Expand All @@ -13,29 +11,3 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.awaitility:awaitility'
}

springBoot {
buildInfo {
excludes = ['time']
}
}

if (project.hasProperty('springShellSampleE2E') && springShellSampleE2E.toBoolean()) {
bootJar {
archiveFileName = "${archiveBaseName.get()}.${archiveExtension.get()}"
}
}

graalvmNative {
metadataRepository {
enabled = true
}
binaries {
main {
if (project.hasProperty('springShellSampleMusl') && springShellSampleMusl.toBoolean()) {
buildArgs.add('--static')
buildArgs.add('--libc=musl')
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
plugins {
id 'org.springframework.shell.sample'
id 'org.springframework.boot'
id 'org.graalvm.buildtools.native'
}

description = 'Spring Shell Sample E2E'
Expand All @@ -13,29 +11,3 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.awaitility:awaitility'
}

springBoot {
buildInfo {
excludes = ['time']
}
}

if (project.hasProperty('springShellSampleE2E') && springShellSampleE2E.toBoolean()) {
bootJar {
archiveFileName = "${archiveBaseName.get()}.${archiveExtension.get()}"
}
}

graalvmNative {
metadataRepository {
enabled = true
}
binaries {
main {
if (project.hasProperty('springShellSampleMusl') && springShellSampleMusl.toBoolean()) {
buildArgs.add('--static')
buildArgs.add('--libc=musl')
}
}
}
}

0 comments on commit da529fa

Please sign in to comment.