Install Java agent using Gradle

This document explains how to install the Java agent using Gradle. For information on manually installing the Java agent, see Install the Java agent and Java agent configuration: Config file.

Install agent package using Gradle

  1. Install the New Relic Java agent, using either of these options:

    Install complete agent package

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

    • newrelic.yml (agent configuration file)
    • newrelic.jar (Java agent jar)
    • newrelic-api.jar (Java agent API jar)
    • README

    To do this:

    1. Configure your build.gradle file with the plugin you will use to download the agent. For example:

      plugins {
          id "de.undercouch.download" version "3.4.3"
      }
    2. Add tasks to download and unzip the agent. For example:
      task downloadNewrelic(type: Download) {
          mkdir 'newrelic'
          src 'https://download.newrelic.com/newrelic/java-agent/newrelic-agent/current/newrelic-java.zip'
          dest file('newrelic')
      }
      
      task unzipNewrelic(type: Copy) {
          from zipTree(file('newrelic/newrelic-java.zip'))
          into rootDir
      }
    3. Run the tasks to download and unzip the agent. For example:

      ./gradlew downloadNewrelic
      ./gradlew unzipNewrelic
      

    This will download the newrelic-java.zip file into a newrelic/ directory and then unzip it in the same directory.

    Here is an example build.gradle file for working with the zip file:

    plugins {
        id 'java'
        id "de.undercouch.download" version "3.4.3"
    }
    
    group 'example_group'
    version '1.0-SNAPSHOT'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    task downloadNewrelic(type: Download) {
        mkdir 'newrelic'
        src 'https://download.newrelic.com/newrelic/java-agent/newrelic-agent/current/newrelic-java.zip'
        dest file('newrelic')
    }
    
    task unzipNewrelic(type: Copy) {
        from zipTree(file('newrelic/newrelic-java.zip'))
        into rootDir
    }
    Install individual components

    This section explains how to configure Gradle to download individual components of the Java agent, specifically the newrelic.jar and the newrelic-api.jar.

    1. Configure your build.gradle file to download either newrelic.jar or newrelic-api.jar. Here's an example for downloading newrelic.jar:

      dependencies {
          compile group: 'com.newrelic.agent.java', name: 'newrelic-agent', version: 'JAVA_AGENT_VERSION'
      }

      Here's an example for downloading newrelic-api.jar:

      dependencies {
          compile group: 'com.newrelic.agent.java', name: 'newrelic-api', version: 'JAVA_AGENT_VERSION'
      }
      

      Replace JAVA_AGENT_VERSION with the latest Java agent version.

    2. Locate the newrelic.yml file you received when creating your New Relic account or download one for the version of the agent that you are using.

    Here is an example build.gradle for working with the individual components (Java agent and API jars).

    plugins {
        id 'java'
    }
    
    group 'example_group'
    version '1.0-SNAPSHOT'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile group: 'com.newrelic.agent.java', name: 'newrelic-agent', version: '4.11.0'
        compile group: 'com.newrelic.agent.java', name: 'newrelic-api', version: '4.11.0'
    }
    
  2. Place newrelic.yml in the same folder as newrelic.jar, unless you specify otherwise in the JVM arg Dnewrelic.config.file.

  3. Configure the newrelic.yml file (or JVM system properties) with your license_key and app_name.
  4. Pass -javaagent:/path/to/newrelic.jar to the JVM running your application server.
  5. Optional: If using the New Relic Java agent API, make the API jar available at compile time by adding it to your application class path.

Generate some traffic for your app, then wait a few minutes for data to appear in the APM Overview page. If nothing appears, follow the troubleshooting procedures.

For more help

Recommendations for learning more: