Building a Java application

Stay organized with collections Save and categorize content based on your preferences.

Specify a Java version

By default, the latest version of Java 11 is ued. If your application requires a specific version, you can use the GOOGLE_RUNTIME_VERSION environment variable to specify a JDK version:

pack build sample-java --builder=gcr.io/buildpacks/builder \
  --env GOOGLE_RUNTIME_VERSION=17

Manage dependencies

Dependency configuration is supported for projects that use Maven or Gradle.

Configure Maven

Maven configurations can be applied using the MAVEN_OPTS environment variable. See documentation for additional instructions.

Examples:

  • MAVEN_OPTS=-Xms256m -Xmx512m passes these flags to the JVM running Maven.
  • MAVEN_OPTS=--add-opens java.base/java.lang=ALL-UNNAMED to suppress "illegal reflective access" warnings from Maven.

Configure Gradle

Gradle configurations can be applied using the GRADLE_OPTS environment variable. See documentation for additional instructions.

Example:

GRADLE_OPTS=-Xms256m -Xmx512m passes these flags to the JVM running Gradle.

Google Cloud hosted Maven Central mirror

You can use the hosted mirror of Maven Central by specifying the avaialbe URLs.

Mirror repositories in Maven

To configure a mirror, follow the Using Mirrors for Repositories instructions in the Maven project documentation.

Create a copy of the settings.xml from the default location of ~/.m2/settings.xml to inside your application source directory and specify GOOGLE_BUILD_ARGS=--settings <path/to/settings>.

Note: is relative to the source directory.

Example settings.xml file:

<settings>
  <mirrors>
    <mirror>
      <id>google-maven-central</id>
      <name>Cloud Storage Maven Central mirror</name>
      <url>https://maven-central.storage-download.googleapis.com/maven2/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
</settings>

Mirror repositories in Gradle

To configure a mirror, follow the Declaring Repositories instructions in the Gradle project documentation.

Example build.gradle entry:

  repositories {
      maven {
          url "https://maven-central.storage-download.googleapis.com/maven2/"
      }
  }