2012-07-31 85 views
4

我想使用exec:exec目標使用maven exec插件運行java程序。
我需要添加一個額外的jar到類路徑(太陽工具jar)。
由於includePluginDependencies只適用於exec:java目標,所以我想在arguments部分中手動添加它,但是找不到將它連接到基類路徑的方法。問題是,由於jar被定義爲系統範圍,maven不會將它添加到運行時類路徑中,我需要手動添加它。
如果有人知道如何從命令行這樣做,它會更好。 由於提前,
阿夫納將命令行參數添加到maven exec插件

  • 您可以看到該插件節波紋管

     <plugin> 
         <groupId>org.codehaus.mojo</groupId> 
         <artifactId>exec-maven-plugin</artifactId> 
         <version>1.2.1</version> 
         <dependencies> 
          <dependency> 
           <groupId>com.sun</groupId> 
           <artifactId>tools</artifactId> 
           <scope>system</scope> 
           <systemPath>${JDK_HOME}/lib/tools.jar</systemPath> 
          </dependency> 
          <dependency> 
           <groupId>${project.groupId}</groupId> 
           <artifactId>myArtifact</artifactId> 
           <version>1.0</version> 
          </dependency> 
         </dependencies> 
         <configuration> 
          <executable>java</executable> 
          <arguments> 
           <argument>-classpath</argument> 
           <classpath/>       
           <argument>com.mycompany.MyMainClass</argument> 
          </arguments> 
         </configuration> 
         <executions> 
          <execution> 
           <goals> 
            <goal>exec</goal> 
           </goals> 
          </execution> 
         </executions> 
        </plugin> 
    
+0

我不得不解決幾乎這個確切的問題,但我的配置看起來很像你的('')。我不清楚你的意思是「將它連接到基類路徑」。你的意思是「Boot」類路徑嗎?你能否提供你的構建輸出的一個片段並描述你的最終目標? – noahlz 2012-09-07 18:56:15

+0

我的目標是用maven計算的運行時類路徑運行我的java程序,同時添加tools.jar。由於這些工具被定義爲系統範圍,因此它不會被添加到由maven計算的運行時類路徑中。最終我決定使用maven-antrun-plugin。 – 2012-09-09 07:11:31

回答

1

嘗試增加

<argument>-Xbootclasspath/a:${env.JAVA_HOME}/lib/tools.jar</argument> 

在命令行中,添加

-Dexec.args="-Xbootclasspath/a:$JAVA_HOME/lib/tools.jar" 

另一種選擇是聲明的tools.jar作爲系統依賴然後設置Exec插件範圍爲「系統。 「參見:exec-maven-plugin - classpathScope

+0

由於我沒有設置了,我無法檢查它,但我已經標記爲答案,因爲它看起來像正確的方向。 – 2012-09-12 16:08:31

+0

我已經嘗試了系統類路徑的方向,但問題是您需要在運行時類路徑和系統類路徑之間進行選擇,並且系統不包含運行時(我認爲) – 2012-09-13 05:49:01

+0

http://stackoverflow.com/questions/5286279/maven-exec-plugin-how-to-include-system-classpath – noahlz 2012-09-13 09:32:55

1

你可以嘗試設置CLASSPATH環境變量。

+0

我認爲CLASSPATH變量被賦給java可執行文件的-classpath參數覆蓋。 OP需要Maven生成的類路徑來組裝項目依賴關係。無論如何,我可以試試這個。 – 2012-09-06 19:59:19

2

最終我決定使用maven-antrun-plugin,所以這裏有一個可能的替代解決方案。

<configuration> 
<target> 
    <property name="runtime_classpath" refid="maven.runtime.classpath"/> 

    <java classname="com.mycompany.MyClass" 
      fork="true" 
      spawn="false" 
      failonerror="true" 
      maxmemory="512m" > 

     <classpath> 
      <pathelement path="${runtime_classpath}" /> 
      <pathelement path="${JDK_HOME}/lib/tools.jar" /> 
     </classpath> 
     <arg value="${ant.param1}" /> 
     <arg value="${ant.param2}" /> 
     <arg value="${ant.param3}" /> 
     <arg value="${ant.param4}" /> 
     <arg value="${ant.param5}" /> 
    </java> 
</target> 
</configuration> 
+0

我還是不明白這與原來的問題有什麼不同,或者你實際試圖解決什麼問題。 – noahlz 2012-09-09 11:48:01

+0

正如你所看到的,我需要運行一個java類,它有一個由maven(運行時類路徑)計算的大型依賴列表。我正在尋找的是一種將jar(tools.jar)添加到構建的計算的類路徑maven並將其用於運行我的java類的方法。在上面的示例中,您可以看到,在antrun插件中,可以將計算的maven運行時類路徑與其他jar連接起來。如果你有一個簡單的方法用maven exec插件來隨意分享。 – 2012-09-12 16:03:35

+0

更新了我的答案,建議使用'-Dexec。classpathScope = 「系統」' – noahlz 2012-09-12 17:58:25