Platform.sh User Documentation

Java

Get your license key Anchor to this heading

Sign up at https://newrelic.com and get your license key.

Add your license key Anchor to this heading

Add your New Relic license key as an environment level variable:

platform variable:create --level environment --environment '<your-environment>' --visible-build false --inheritable false env:NEW_RELIC_LICENSE_KEY --value '<your-new-relic-license-key>'

Give your application a name Anchor to this heading

Add a new environment level variable to give your application a recognizable name:

platform variable:create --level environment --environment '<your-environment>' --visible-build false --inheritable false env:NEW_RELIC_APP_NAME --value '<your-application-name>'

Set up the New Relic agent Anchor to this heading

To set up New Relic in the Java project, we have two ways:

  • Using the Maven project
  • Download the code through .platform.app.yaml.

Using Maven Anchor to this heading

This section explains how to configure Maven to download and unzip the newrelic-java.zip file, which contains all New Relic Java agent components.

Configure your pom.xml to download newrelic-java.zip:

<dependency>
  <groupId>com.newrelic.agent.java</groupId>
  <artifactId>newrelic-java</artifactId>
    <version>JAVA_AGENT_VERSION</version>
  <scope>provided</scope>
  <type>zip</type>
</dependency>

Replace JAVA_AGENT_VERSION with the latest Java agent version 1.

Unzip newrelic-java.zip by configuring maven-dependency-plugin in your pom.xml:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>3.1.1</version>
      <executions>
        <execution>
          <id>unpack-newrelic</id>
          <phase>package</phase>
          <goals>
            <goal>unpack-dependencies</goal>
          </goals>
          <configuration>
            <includeGroupIds>com.newrelic.agent.java</includeGroupIds>
            <includeArtifactIds>newrelic-java</includeArtifactIds>
            <excludes>**/newrelic.yml</excludes>
            <overWriteReleases>false</overWriteReleases>
            <overWriteSnapshots>false</overWriteSnapshots>
            <overWriteIfNewer>true</overWriteIfNewer>
            <outputDirectory>${project.build.directory}</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>

The next step is to configure your app to set the agent in the JVM parameters:

name: app
type: 'java:8'
disk: 1024

hooks:
    build: |
        mvn clean package
        rm -rf newrelic/
        mv target/newrelic/ newrelic/        

web:
    commands:
        start: |
            java -jar \
            -Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError \
            -javaagent:/app/newrelic/newrelic.jar            

Manual Configuration Anchor to this heading

To use this installation it is only required that you modify .platform.app.yaml , which will download and set the New Relic Java agent for you.

name: app
type: 'java:8'
disk: 1024

variables:
    env:
        NEW_RELIC_URL: https://download.newrelic.com/newrelic/java-agent/newrelic-agent/current/newrelic-java.zip

hooks:
    build: |
        mvn clean package
        rm -rf newrelic
        curl -O $NEW_RELIC_URL
        unzip newrelic-java.zip        

web:
    commands:
        start: |
            java -jar \
            -Xmx$(jq .info.limits.memory /run/config.json)m \
            -XX:+ExitOnOutOfMemoryError \
            -javaagent:/app/newrelic/newrelic.jar            

You need to wait a little bit for your New Relic dashboard to be generated.

Is this page helpful?