javaeclipsemavenjavaagentsmaven-tomcat-plugin

pass my javaagent to tomcat-maven-plugin NOT Tomcat Server


I have spent 2 days trying to get my mvn project to run my war file with javaagent. Let me explain in details.

In order to run a normal jar file with javaagent one would need to do the following:

java -javaagent:path/to/the/agent -jar /path/to/your/jar

now I have a web application which I run using the following command:

mvn tomcat7:run-war

I have read that if it is a normal tomcat server , then I would modify the catalina.sh file and add $JAVE_OPTS="-javaagent:path/to/the/agent"

Thats in case I am running my war with my tomcat server. But this is not the case. I am running the war using tomcat-maven-plugin and I Have spent countless hours trying to get it running something like the following:

mvn -javaagent:path/to/my/agent tomcat7:run-war

Here is my war project pom.xml

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>


<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<groupId>MonitoringService</groupId>
<artifactId>MonitoringService</artifactId>

<repositories>
    <repository>
        <id>commons-mvn-repo</id>
        <url>https://raw.github.com/mostafaelganainy/socialhub-commons/mvn-repo/</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
</repositories>

<dependencies>

    <dependency>
            <groupId>com.smartera.monitor</groupId>
            <artifactId>jobsHandler-servlet-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>2.12.3</version>
    </dependency>


    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>


    <dependency>
        <groupId>com.goebl</groupId>
        <artifactId>david-webb</artifactId>
        <version>1.3.0</version>
    </dependency>

    <dependency>
        <groupId>socialhub-commons</groupId>
        <artifactId>socialhub-commons</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>com.smartera.monitor</groupId>
          <artifactId>JSpy</artifactId>
          <version>0.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.14.0-GA</version>
        <type>jar</type>
    </dependency>

    <dependency>
            <groupId>com.smartera.monitor</groupId>
            <artifactId>jobsHandler</artifactId>
            <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <dependency>
            <groupId>com.smartera.monitor</groupId>
            <artifactId>jobsHandler-servlet-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>jar</type>
    </dependency>



</dependencies>


<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>     
                 <webXml>WebContent\WEB-INF\web.xml</webXml>
            </configuration>
        </plugin>



        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>


    </plugins>
</build>

I would really appreciate if someone can guide me to a solution or tell me why this could be not possible


Solution

  • I was finally able to do it with an extremely simple 3 steps for ubuntu 14.04

    1. nano ~/.profile
    2. at the beginning of the file appended the following export MAVEN_OPTS="-javaagent:path/to/your/agent"
    3. save and close the file, then close the termina, then logout and log back in.

    and tattaaaa , it works. Happy coding.