Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to pass env vars to the builder process #280

Merged
merged 3 commits into from Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/src/docs/asciidoc/index.adoc
Expand Up @@ -16,6 +16,11 @@ If you are using alternative build systems, see <<alternative-build-systems.adoc

[[changelog]]
== Changelog
=== Release 0.9.14

=== Gradle plugin
* Add ability to set environment variables to the native image builder process

=== Release 0.9.13

=== Gradle plugin
Expand Down
Expand Up @@ -188,4 +188,32 @@ class JavaApplicationFunctionalTest extends AbstractFunctionalTest {

}

@Issue("https://github.com/graalvm/native-build-tools/issues/275")
def "can pass environment variables to the builder process"() {
def nativeApp = file("build/native/nativeCompile/java-application")

given:
withSample("java-application")

buildFile << """
graalvmNative.binaries.all {
buildArgs.add("--initialize-at-build-time=org.graalvm.demo.Application")
environmentVariables.put('CUSTOM_MESSAGE', 'Hello, custom message!')
}
""".stripIndent()

when:
run 'nativeCompile'

then:
nativeApp.exists()

when:
def process = execute(nativeApp)

then:
process.output.contains "Hello, custom message!"

}

}
Expand Up @@ -107,6 +107,15 @@ public interface NativeImageOptions extends Named {
@Input
MapProperty<String, Object> getSystemProperties();

/**
* Returns the environment variables which will be used by the native-image builder process.
* @return the environment variables. Returns an empty map when there are no environment variables.
*
* @since 0.9.14
*/
@Input
MapProperty<String, Object> getEnvironmentVariables();

/**
* Returns the classpath for the native-image building.
*
Expand Down
Expand Up @@ -125,6 +125,15 @@ public String getName() {
@Input
public abstract MapProperty<String, Object> getSystemProperties();

/**
* Returns the environment variables which will be used by the native-image builder process.
* @return the environment variables. Returns an empty map when there are no environment variables.
*
* @since 0.9.14
*/
@Override
public abstract MapProperty<String, Object> getEnvironmentVariables();

/**
* Returns the classpath for the native-image building.
*
Expand Down
Expand Up @@ -129,6 +129,11 @@ public MapProperty<String, Object> getSystemProperties() {
return warnAboutDeprecation(delegate::getSystemProperties);
}

@Override
public MapProperty<String, Object> getEnvironmentVariables() {
return warnAboutDeprecation(delegate::getEnvironmentVariables);
}

@Override
@Classpath
@InputFiles
Expand Down
Expand Up @@ -53,6 +53,7 @@
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
Expand Down Expand Up @@ -185,6 +186,10 @@ public void exec() {
File outputDir = getOutputDirectory().getAsFile().get();
if (outputDir.isDirectory() || outputDir.mkdirs()) {
getExecOperations().exec(spec -> {
MapProperty<String, Object> environmentVariables = options.getEnvironmentVariables();
if (environmentVariables.isPresent() && !environmentVariables.get().isEmpty()) {
spec.environment(environmentVariables.get());
}
spec.setWorkingDir(getWorkingDirectory());
if (getTestListDirectory().isPresent()) {
NativeImagePlugin.TrackingDirectorySystemPropertyProvider directoryProvider = getObjects().newInstance(NativeImagePlugin.TrackingDirectorySystemPropertyProvider.class);
Expand Down
11 changes: 8 additions & 3 deletions samples/java-application-with-custom-packaging/pom.xml
Expand Up @@ -41,7 +41,7 @@
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.graalvm.buildtools.examples</groupId>
<artifactId>java-application-with-custom-packaging</artifactId>
Expand All @@ -61,7 +61,7 @@
<micronaut.version>3.3.4</micronaut.version>
<exec.mainClass>org.graalvm.demo.Application</exec.mainClass>
<micronaut.runtime>netty</micronaut.runtime>
<maven.native.plugin.version>0.9.13-SNAPSHOT</maven.native.plugin.version>
<native.maven.plugin.version>0.9.14-SNAPSHOT</native.maven.plugin.version>
</properties>

<repositories>
Expand Down Expand Up @@ -140,7 +140,12 @@
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native.maven.plugin.version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
@@ -1,7 +1,9 @@
package org.graalvm.demo;

public class Application {
private static final String MESSAGE = System.getenv("CUSTOM_MESSAGE");

public static void main(String[] args) {
System.out.println("Hello, native!");
System.out.println(MESSAGE != null ? MESSAGE : "Hello, native!");
}
}