Specify javaagent argument with Maven exec plugin
Asked Answered
J

3

15

I have a similar question to: this previous question

I am converting a Java project using Netbeans to Maven. In order to launch the program, one of the command-line arguments we need is the -javaagent setting. e.g.

-javaagent:lib/eclipselink.jar

I'm trying to get Netbeans to launch the application for development use (we will write custom launch scripts for final deployment)

Since I'm using Maven to manage the Eclipselink dependencies, I may not know the exact filename of the Eclipselink jar file. It may be something like eclipselink-2.1.1.jar based on the version I have configured in the pom.xml file.

How do I configure the exec-maven-plugin to pass the exact eclipselink filename to the command line argument?

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <configuration>
       <executable>java</executable>
           <arguments>
               <argument>-Xmx1000m</argument>
               <argument>-javaagent:lib/eclipselink.jar</argument> <==== HELP?
               <argument>-classpath</argument>
               <classpath/>
               <argument>my.App</argument>
           </arguments>
    </configuration>
</plugin>
Josi answered 8/2, 2013 at 17:18 Comment(0)
J
17

I figured out a way that seems to work well.

First, setup the maven-dependency-plugin to always run the "properties" goal.

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
        <execution>
            <id>getClasspathFilenames</id>
            <goals>
                <goal>properties</goal>
            </goals>
        </execution>
     </executions>
</plugin>

Later on, use the property it sets as documented here with the form:

groupId:artifactId:type:[classifier]

e.g.

<argument>-javaagent:${mygroup:eclipselink:jar}</argument>
Josi answered 8/2, 2013 at 17:45 Comment(1)
Awesomeness! I would just point out that you need to put this <plugin/> element in the very pom.xml where your <argument/> element resides. (<extraJvmArgs/> in my case)... I.e. having it in a parent pom.xml does not seem to work. Thanks again!Verlinevermeer
A
5

Simply define a property for the eclipse link version and use the property in your <dependency> and the exec plugin:

    <properties>
        <eclipselink.version>2.4.0</eclipselink.version>
    </properties>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>${eclipselink.version}</version>
    </dependency>
    ...
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <configuration>
      <executable>java</executable>
       <arguments>
           <argument>-Xmx1000m</argument>
           <argument>-javaagent:lib/eclipselink-${eclipselink.version}.jar</argument>
           <argument>-classpath</argument>
           <classpath/>
           <argument>my.App</argument>
       </arguments>
     </configuration>
   </plugin>
Agger answered 8/2, 2013 at 17:30 Comment(2)
Very close to what I finally chose to do. I figured out a way to set a property dynamically instead of hard-coding it. Thanks.Josi
David, I Have the same issue, can you please share your solution to this problem??Muco
R
0

the maven-dependency-plugin and exec-maven-plugin should be put under the node ,otherwise it will not work

Recover answered 28/2, 2017 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.