Maven installation for Java

There are two methods for using New Relic through Maven:

  • Use the zip file.
  • Manually deploy individual components.

If you use the New Relic API, you need to make the API jar available at compile time.

For more information on working with the Java agent, see the manual Java agent installation directions.

Method 1: Use the zip file

The newrelic-java.zip contains all files needed to run the Java agent. This includes:

  • The newrelic.yml configuration file
  • The newrelic agent jar
  • The newrelic api jar
  • README

To pull down the Java agent zip file, add the following to your maven pom.xml. Replace myVersion with your version.

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

Call the maven-dependency-plugin in your pom.xml to unzip the newrelic-java.zip file. For example:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.6</version>
  <executions>
    <execution>
      <id>unpack-zip</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>unpack-dependencies</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>com.newrelic.agent.java</groupId>
            <artifactId>newrelic-java</artifactId>
            <version>myVersion</version>
            <type>zip</type>
            <overWrite>true</overWrite>
            <outputDirectory>target</outputDirectory>
            <destFileName>newrelic</destFileName>
          </artifactItem>
        </artifactItems>
        <outputDirectory>/Location/to/unzip</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

You will need to manually add your license key to the newrelic.yml file.

Method 2: Manual deployment

You will need the newrelic.yml that you receive when you create a New Relic account.

New Relic is provided as a collection of components. These directions are for including the two components most commonly used in Maven:

The New Relic agent

The New Relic agent (newrelic.jar) generates performance metrics. It is the core of New Relic.

To pull down the newrelic.jar, add this to your maven pom.xml. Replace myVersion with your version.

<dependency>
  <groupId>com.newrelic.agent.java</groupId>
  <artifactId>newrelic-agent</artifactId>
  <version>myVersion</version>
  <scope>provided</scope>
</dependency>

Pass the newrelic.jar as an argument to the JVM using the -javaagent command.

-javaagent:/path/to/newrelic.jar

When specifying the newrelic agent jar with the -javaagent command, be sure the name of the jar is newrelic.jar. Rename it if necessary.

The New Relic API

The newrelic-api.jar contains the New Relic Java agent public API.

To pull down the Java agent API jar, add this to your maven pom.xml. Replace myVersion with your version:

<dependency>
  <groupId>com.newrelic.agent.java</groupId>
  <artifactId>newrelic-api</artifactId>
  <version>myVersion</version>
  <scope>compile</scope>
</dependency>

Example of zip deployment

Here is an example pom for working with the zip file.

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.application</groupId>
<artifactId>my-example-app</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>My Example Application</name>
<url>http://example.com</url>

<dependencies>
  <!-- The newrelic-java.zip dependency. -->
  <dependency>
    <groupId>com.newrelic.agent.java</groupId>
    <artifactId>newrelic-java</artifactId>
    <version>myVersion</version>
    <scope>provided</scope>
    <type>zip</type>
  </dependency>
</dependencies>
<build>
  <plugins>
    <!-- Plugin which can be used to unpack the zip file. -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>2.6</version>
      <executions>
        <execution>
          <id>unpack-zip</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>unpack-dependencies</goal>
          </goals>
          <configuration>
            <artifactItems>
              <artifactItem>
                <groupId>com.newrelic.agent.java</groupId>
                <artifactId>newrelic-java</artifactId>
                <version>myVersion</version>
                <type>zip</type>
                <overWrite>true</overWrite>
                <outputDirectory>target</outputDirectory>
                <destFileName>newrelic</destFileName>
              </artifactItem>
            </artifactItems>
            <outputDirectory>/Location/to/unzip</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
    </plugins>
  </build>
</project>

Example of manual deployment

Here is an example pom for working with the individual components.

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.application</groupId>
<artifactId>my-example-app</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>My Example Application</name>
<url>http://example.com</url>

<dependencies>
  <!-- The newrelic.jar dependency. -->
  <dependency>
    <groupId>com.newrelic.agent.java</groupId>
    <artifactId>newrelic-agent</artifactId>
    <version>myVersion</version>
    <scope>provided</scope>
  </dependency>
  <!-- The newrelic-api.jar dependency. -->
  <dependency>
    <groupId>com.newrelic.agent.java</groupId>
    <artifactId>newrelic-api</artifactId>
    <version>myVersion</version>
    <scope>compile</scope>
  </dependency>
</project>

For more help

Additional documentation resources include:

Join the discussion about Java monitoring in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.