【问题标题】:Specify javaagent argument with Maven exec plugin使用 Maven exec 插件指定 javaagent 参数
【发布时间】:2013-02-08 17:18:40
【问题描述】:

我有一个类似的问题:this previous question

我正在将使用 Netbeans 的 Java 项目转换为 Maven。为了启动程序,我们需要的命令行参数之一是 -javaagent 设置。例如

-javaagent:lib/eclipselink.jar

我正在尝试让 Ne​​tbeans 启动应用程序以供开发使用(我们将为最终部署编写自定义启动脚本)

由于我使用 Maven 来管理 Eclipselink 依赖项,我可能不知道 Eclipselink jar 文件的确切文件名。根据我在 pom.xml 文件中配置的版本,它可能类似于 eclipselink-2.1.1.jar。

如何配置 exec-maven-plugin 以将确切的 eclipselink 文件名传递给命令行参数?

<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>

【问题讨论】:

    标签: maven javaagents


    【解决方案1】:

    我想出了一个似乎行之有效的方法。

    首先,将maven-dependency-plugin 设置为始终运行“属性”目标。

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

    稍后,使用它设置的属性as documented here,形式为:

    groupId:artifactId:type:[classifier]
    

    例如

    <argument>-javaagent:${mygroup:eclipselink:jar}</argument>
    

    【讨论】:

    • 太棒了!我只想指出,您需要将此 元素放在 元素所在的 pom.xml 中。 ( 在我的情况下)...即将它放在父 pom.xml 中似乎不起作用。再次感谢!
    【解决方案2】:

    只需为 eclipse 链接版本定义一个属性,并在您的 &lt;dependency&gt; 和 exec 插件中使用该属性:

        <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>
    

    【讨论】:

    • 非常接近我最终选择做的事情。我想出了一种动态设置属性而不是硬编码的方法。谢谢。
    • 大卫,我也有同样的问题,你能分享一下你对这个问题的解决方案吗??
    【解决方案3】:

    maven-dependency-plugin 和 exec-maven-plugin 应该放在节点下,否则不起作用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 2016-01-31
      • 2013-06-06
      相关资源
      最近更新 更多