Hi
I'm using Spring Boot 2.3.0 and JDK 14.0.1 and want to pack my application using new buildpack feature in Spring Boot Maven plugin.
Since I use preview features I have to add "--enable-preview" flag as JVM argument (JAVA_OPTS environment variable).
So here's my Spring Boot Maven plugin configuration:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>
--enable-preview
</jvmArguments>
<image>
<env>
<JAVA_OPTS>--enable-preview</JAVA_OPTS>
</env>
</image>
</configuration>
<executions>
<execution>
<id>buildImage</id>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
</plugin>
Docker image is built successfully but container fails to start with exception:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Preview features are not enabled for demo/Application (class file version 58.65535). Try running with '--enable-preview'
And resulting JAVA_OPTS environment variable in the image is missing my argument:
-Djava.security.properties=/layers/paketo-buildpacks_bellsoft-liberica/java-security-properties/java-security.properties -agentpath:/layers/paketo-buildpacks_bellsoft-liberica/jvmkill/jvmkill-1.16.0-RELEASE.so=printHeapHistogram=1 -XX:ActiveProcessorCount=2 -XX:MaxDirectMemorySize=10M -XX:MaxMetaspaceSize=104953K -XX:ReservedCodeCacheSize=240M -Xss1M -Xmx431622K -Dio.paketo.openssl.ca-certificates=/etc/ssl/certs/ca-certificates.crt
As I understand, <env> is not for passing environment variables to the application image. It is for configuring buildpack behaviors (e.g. BP_JVM_VERSION to choose which JVM version to be included in the image).
I also have the same question as yours. I want to pass some environment variables to my application image. I've checked Paketo buildpacks web site and it doesn't seem to have the information I need.
I'd suggest to improve the plugin document to give an example how to include environment variables into the OCI images.
As I understand,
<env>is not for passing environment variables to the application image. It is for configuring buildpack behaviors (e.g.BP_JVM_VERSIONto choose which JVM version to be included in the image).
That's correct. We'll research how to get environment variables passed to the JVM in the built image.
After discussions with the Cloud Native Buildpacks team, there currently isn't a way to pass environment variables to a builder and request that the builder set the variable in the resulting image. In order to support this, CNB builder clients like the pack CLI and the Spring Boot build plugins would need a way to specify whether an environment variable is meant for use by the builder, or intended to be used only during image build process, or intended to be set in the resulting image. The CNB team will need to think about a design for this requirement.
As an alternative to setting something like JAVA_OPTS permanently in the built image, you can provide environment variables to the container runtime when running the container image. For example, with Docker you can do this:
$ docker run --env JAVA_OPTS="--enable-preview" <image>
@scottfrederick do you have a reference on CNB for this feature request? Thank you
There's not an issue opened with CNB or Paketo for this feature yet, but you can join the discussion in the Paketo slack.
what if we deploy the images in something like hashi corp Nomad? Any tricks for passing jvm options there?
@bmd007 The Paketo documentation now has information on passing arguments run running an app in an image. See https://paketo.io/docs/buildpacks/language-family-buildpacks/java/.
As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. This is a question that would be better suited to Stack Overflow.
I think it's now possible to burn runtime arguments into the JVM at build time. Via @ekcasey, the environment variables buildpack can be used like this:
To use the environment variables buildpack you would probably want to pass
--env "BPE_APPEND_JAVA_TOOL_OPTIONS=-Dspring.devtools.restart.enabled=true"and--env "BPE_DELIM_JAVA_TOOL_OPTIONS= "to append your value with a whitespace delimiter and thus avoid overriding that buildpack鈥檚 memory configuration.
We should try it out and perhaps make some documentation updates if it works as hoped.
There is an open issue to document the environment variables buildpack on the Paketo documentation site. Once this is done, the Spring Boot documentation should link to this Paketo documentation and embellish it with examples of setting BPE_APPEND_JAVA_TOOL_OPTIONS and BPE_DELIM_JAVA_TOOL_OPTIONS in a Maven pom.xml and Gradle build.gradle.
Most helpful comment
As I understand,
<env>is not for passing environment variables to the application image. It is for configuring buildpack behaviors (e.g. BP_JVM_VERSION to choose which JVM version to be included in the image).I also have the same question as yours. I want to pass some environment variables to my application image. I've checked Paketo buildpacks web site and it doesn't seem to have the information I need.
I'd suggest to improve the plugin document to give an example how to include environment variables into the OCI images.