> ## Documentation Index
> Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Moving a Java application to Upsun Cloud

> [!TIP]
>
> You can now use composable image to install runtimes and tools in your application container. To find out more, see the [Composable image](https://developer.upsun.com/docs/configure-apps/app-reference/composable-image.md) topic.

It is common to have a Java application that you want to migrate to Upsun Cloud.
Upsun Cloud supports several styles of Java application, such as monolith, microservices, stateful, and stateless.

## Minimum Requirement

To run a Java application at Upsun Cloud you need:

* [A supported Java version](https://developer.upsun.com/docs/languages/java#supported-versions)
* [A build management tool](https://developer.upsun.com/docs/languages/java#support-build-automation)
  * [Gradle](https://docs.gradle.org/current/userguide/gradle_wrapper.html)
  * [Maven](https://maven.apache.org/)
  * [Maven Wrapper](https://www.baeldung.com/maven-wrapper)
  * [Ant](https://ant.apache.org/)
* A Git Repository:
  * [GitHub](https://developer.upsun.com/docs/integrations/source/github.md)
  * [BitBucket](https://developer.upsun.com/docs/integrations/source/bitbucket.md)
  * [GitLab](https://developer.upsun.com/docs/integrations/source/gitlab.md)
  * The default Git repository provided by Upsun Cloud

> [!NOTE]
>
> A container application can't be bigger than **8 GB** of memory.
> For more details, see [tuning](https://developer.upsun.com/docs/languages/java/tuning.md).

## Monolith/Single Application

To start a Java application, you need to understand the [Upsun Cloud structure](https://developer.upsun.com/docs/core-concepts/structure.md).
You will need to configure your [application](https://developer.upsun.com/docs/configure-apps), [routes](https://developer.upsun.com/docs/routes),
and [services](https://developer.upsun.com/docs/add-services).

### Application


&#x20;   applications:
&#x20;     myapp:
&#x20;       type: 'java:\<VERSION>'

&#x20;       hooks:
&#x20;         build: \[2]
&#x20;       web:
&#x20;         commands:
&#x20;           start: \[3]
&#x20;&#x20;

1. [A Java version](https://developer.upsun.com/docs/languages/java#supported-versions), e,g.: `java:<VERSION>`.
2. [Hooks define what happens when building the application](https://developer.upsun.com/docs/configure-apps/hooks). This build process typically generates an executable file such as a uber-jar. For example, `mvn clean package`.
3. [The commands key defines the command to launch the application](https://developer.upsun.com/docs/configure-apps/image-properties/web.md#web-commands). For example,  `java -jar file.jar`.
4. In the start's command needs to receive the port where the application will execute thought the `PORT` environment. That's best when your app follows the port bind principle. For example, `java -jar jar --port=$PORT`.

> [!NOTE]
>
> Be aware that after the build, it creates a read-only system. You have the [mount option to create a writable folder](https://developer.upsun.com/docs/configure-apps/image-properties/mounts.md).

### Route


&#x20;   routes:
&#x20;     "https\://\{default}/":
&#x20;       type: upstream
&#x20;       upstream: "myapp:http" \[1]
&#x20;     "https\://www.\{default}/":
&#x20;       type: redirect
&#x20;       to: "https\://\{default}/"

&#x20;   applications:
&#x20;     myapp:
&#x20;       type: 'java:\<VERSION>'

&#x20;       hooks:
&#x20;         build: \[2]
&#x20;       web:
&#x20;         commands:
&#x20;           start: \[3]
&#x20;&#x20;

1. It defines the application will link in the route. For example,`"myapp:http"`.

> [!NOTE]
>
> Application instances have a limited amount of memory at build time, which has a maximum of 8 GB.
> At runtime that limit depends on your plan and configuration.
> A stateless application can be scaled horizontally to multiple application instances using Varnish in a [load balancer](https://support.platform.sh/hc/en-us/community/posts/16439676899474) configuration.

> [!NOTE]
>
> Application instances have a limited amount of memory at build time, which has a maximum of 8 GB.
> At runtime that limit depends on [the resources you have defined for your application container](https://developer.upsun.com/docs/manage-resources.md) using `upsun resources:set`.
> A stateless application can be scaled horizontally to multiple application instances with `upsun resources:set` or by using Varnish in a [load balancer](https://support.platform.sh/hc/en-us/community/posts/16439676899474) configuration.

## Microservices

You have the option to use several languages in microservices. If you're using Java there are several options to aggregate these services into a microservices:

* [Maven Modules](https://maven.apache.org/guides/mini/guide-multiple-modules.html)
* [Gradle Multi-project](https://guides.gradle.org/creating-multi-project-builds/)
* [Git submodules](https://developer.upsun.com/docs/development/submodules.md)

[Upsun Cloud supports multiple applications](https://developer.upsun.com/docs/configure-apps/multi-app) and there are two options:

* One application YAML file to each application
* Aggregate all applications in a single file with an `.upsun/config.yaml` file

| Article | Content |
| - | - |
| [Microservices in the cloud, part two](https://developer.upsun.com/posts/unknown/microservices-in-the-cloud-part-two) | [Source](https://github.com/EventosJEspanol/latin-america-micro-profile) |
| [Microservices in the cloud, part one](https://upsun.com/blog/) | [Source](https://github.com/EventosJEspanol/latin-america-micro-profile) |
| [Multiple Applications](https://support.platform.sh/hc/en-us/community/posts/16439649733010) | [Source](https://github.com/platformsh-examples/tomcat-multi-app) |
| [Configure multi-applications with `.upsun/config.yaml`](https://support.platform.sh/hc/en-us/community/posts/16439676928274) | [Source](https://github.com/platformsh-examples/tomcat-multi-app-applications) |

> [!NOTE]
>
> You can load balance to some or [all applications in the project cluster](https://support.platform.sh/hc/en-us/community/posts/16439662235026).
>
> While the table above shows examples for Upsun Fixed rather than for Upsun Flex, the same rules apply with only slight changes in configuration.

## Access to managed services

Upsun Cloud provides [managed services](https://developer.upsun.com/docs/add-services) such as databases, cache, and search engines.
However, you can use an external database or other services during a transition process. To maintain security, you must ensure that your external service (e.g., your external database or a third-party API)
is configured to accept connections from Upsun Cloud’s outbound IP addresses.

When applications need to access a *managed* service, it is important to include the [`relationships` key](https://developer.upsun.com/docs/configure-apps/image-properties/relationships).
By default an application may not talk to any other container without a `relationship` explicitly allowing access.

The most common mechanisms for connecting to a managed service are listed below.

### Overwrite

If you are using a framework that follows the [Twelve-Factor App](https://12factor.net/) methodology, particularly the [third point](https://12factor.net/config), you can configure the application directly from environment variables.
Examples of such frameworks include Spring, Eclipse MicroProfile Config, Quarkus, and Micronauts.

Service credentials are available within the [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables), or the [`PLATFORM_RELATIONSHIPS` environment variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables).

> [tab] Service environment variables
>
> Assuming the relationship `postgresql` is configured to grant access to a PostgreSQL service container, you can map the automatically generated environment variable (`POSTGRESQL_HOST`) to whatever your application expects to use:
>
> ```bash .environment theme={null}
> export DB_HOST="$POSTGRESQL_HOST"
> ```
>
> This sets environment variables with the names your app needs,
> and the values from [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables).

> [tab] `PLATFORM_RELATIONSHIPS` environment variable
>
> This variable is a base64-encoded JSON object with keys of the relationship name and values of arrays of relationship endpoint definitions.
>
> Upsun Cloud supports the [`jq` tool](https://stedolan.github.io/jq/), which allows to extract information from this JSON.
>
> ```bash .environment theme={null}
> export DB_HOST="$(echo "$PLATFORM_RELATIONSHIPS" | base64 --decode | jq -r '.postgresql[0].host')"
> ```
>
> This sets environment variables with names your app needs and the values from [`PLATFORM_RELATIONSHIPS` environment variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables).

| Article | Source |
| - | - |
| [Spring Data MongoDB](https://support.platform.sh/hc/en-us/community/posts/16439654854802) | [Source](https://github.com/platformsh-examples/java-overwrite-configuration/tree/master/spring-mongodb) |
| [Jakarta EE/MicroProfile Config](https://support.platform.sh/hc/en-us/community/posts/16439700735122) | [Source](https://github.com/platformsh-examples/java-overwrite-configuration/tree/master/jakarta-nosql) |
| [Spring Data JPA](https://support.platform.sh/hc/en-us/community/posts/16439669562130) | [Source](https://github.com/platformsh-examples/java-overwrite-configuration/tree/master/spring-jpa) |
| [Payara JPA](https://support.platform.sh/hc/en-us/community/posts/16439658290194) | [Source](https://github.com/platformsh-examples/java-overwrite-configuration/blob/master/payara/README) |

> [!NOTE]
>
> While the table above shows examples for Upsun Fixed rather than for Upsun Flex, the same rules apply with only slight changes in configuration.

To reduce the number of lines in the application file and to make it cleaner,
you have the option to move the variable environment to another file: a [`.environment` file](https://developer.upsun.com/docs/development/variables/set-variables#set-variables-via-script).

**Example:**

You can obtain relationship information through the [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables) themselves,
or through the [`PLATFORM_RELATIONSHIPS` environment variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables).
Say your application has a relationship named `postgresql` to a database service named `postgresql`:

> [tab] Service environment variables
>
> ```bash .environment theme={null}
> export DB_HOST="${POSTGRESQL_HOST}"
> export DB_PASSWORD="${POSTGRESQL_PASSWORD}"
> export DB_USER="${POSTGRESQL_USERNAME}"
> export DB_DATABASE="${POSTGRESQL_PATH}"
> export JDBC="jdbc:postgresql://${HOST}/${DATABASE}"
> export JAVA_MEMORY="-Xmx$(jq .info.limits.memory /run/config.json)m"
> export JAVA_OPTS="$JAVA_MEMORY -XX:+ExitOnOutOfMemoryError"
> ```
>
> This sets environment variables with the names your app needs,
> and the values from [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables).

> [tab] `PLATFORM_RELATIONSHIPS` environment variable
>
> This `PLATFORM_RELATIONSHIPS` variable is a base64-encoded JSON object with keys of the relationship name and values of arrays of relationship endpoint definitions.
>
> Upsun Cloud supports the [`jq` tool](https://stedolan.github.io/jq/), which allows to extract information from this JSON.
>
> ```bash .environment theme={null}
> export DB_HOST="$(echo "$PLATFORM_RELATIONSHIPS" | base64 --decode | jq -r ".postgresql[0].host')"
> export DB_PASSWORD="$(echo "$PLATFORM_RELATIONSHIPS" | base64 --decode | jq -r ".postgresql[0].password')"
> export DB_USER="$(echo "$PLATFORM_RELATIONSHIPS" | base64 --decode | jq -r ".postgresql[0].username')"
> export DB_DATABASE="$(echo "$PLATFORM_RELATIONSHIPS" | base64 --decode | jq -r ".postgresql[0].path')"
> export JDBC="jdbc:postgresql://${HOST}/${DATABASE}"
> export JAVA_MEMORY="-Xmx$(jq .info.limits.memory /run/config.json)m"
> export JAVA_OPTS="$JAVA_MEMORY -XX:+ExitOnOutOfMemoryError"
> ```
>
> This sets environment variables with names your app needs and the values from [`PLATFORM_RELATIONSHIPS` environment variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables).

This `.environment` file can interact to each application file.

**Example:**


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'java:\{\{version:java:latest}}'
&#x20;       hooks:
&#x20;         build: ./mvnw package -DskipTests -Dquarkus.package.uber-jar=true
&#x20;       relationships:
&#x20;         postgresql:
&#x20;       web:
&#x20;         commands:
&#x20;           start: java -jar \$JAVA\_OPTS \$CREDENTIAL -Dquarkus.http.port=\$PORT jarfile.jar


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.