Skip to content

Instantly share code, notes, and snippets.

@ryanbrainard
Last active April 8, 2019 09:15
Show Gist options
  • Save ryanbrainard/5087037 to your computer and use it in GitHub Desktop.
Save ryanbrainard/5087037 to your computer and use it in GitHub Desktop.

Java configuration with Maven

The New Relic Java agent is available as a Maven dependency that can be automatically downloaded and installed during your project's build. This has the advantage of not having to check the agent into your project's source control and makes it easier to change versions in the future.

Add New Relic to your POM

  1. Add newrelic-java as a dependency. Check Maven Central for the latest version:

     <dependency>
       <groupId>com.newrelic.agent.java</groupId>
       <artifactId>newrelic-java</artifactId>
       <version>2.15.0</version>
       <type>zip</type>
       <scope>runtime</scope>
     </dependency>
    
  2. Bind the an execution of the unpack-dependencies goal to the package phase. This will unzip the agent to target/dependecy/newrelic during your project's build:

     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-dependency-plugin</artifactId>
                 <version>2.3</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>
                     </configuration>
                   </execution>
                 </executions>
             </plugin>
         </plugins>
     </build>
    
  3. Push these changes to your application:

     :::term
     $ git add pom.xml
     $ git commit -m 'add newrelic'
     $ git push heroku master
    

Enable the Java agent

Add the following flag to your JAVA_OPTS config var on Heroku: -javaagent:target/dependency/newrelic/newrelic.jar

You can get the current value of your config var with:

::: term
$ heroku config
...
JAVA_OPTS             => -Xmx384m -Xss512k -XX:+UseCompressedOops
...

Copy the value and add the Java agent flag:

:::term
$ heroku config:add JAVA_OPTS='-Xmx384m -Xss512k -XX:+UseCompressedOops -javaagent:target/dependency/newrelic/newrelic.jar'
Adding config vars:
  JAVA_OPTS => -Xmx384m -Xss512...lic/newrelic.jar
@tylerbenson
Copy link

Your pom addition is missing the <executions> tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment