2010-03-18 63 views
77

我想要一個Maven目標觸發java類的執行。我想一個Makefile上遷移與行:如何使用Maven執行程序?

neotest: 
    mvn exec:java -Dexec.mainClass="org.dhappy.test.NeoTraverse" 

而且我想mvn neotest生產什麼make neotest目前做。

exec plugin documentationMaven Ant tasks頁面都沒有任何直接的例子。

目前,我在:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.1</version> 
    <executions><execution> 
    <goals><goal>java</goal></goals> 
    </execution></executions> 
    <configuration> 
    <mainClass>org.dhappy.test.NeoTraverse</mainClass> 
    </configuration> 
</plugin> 

我不知道如何來觸發命令行插件,雖然。

回答

108

隨着全局配置您已爲EXEC-行家-插件定義:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.4</version> 
    <configuration> 
    <mainClass>org.dhappy.test.NeoTraverse</mainClass> 
    </configuration> 
</plugin> 

在命令行上調用mvn exec:java將調用其被配置成執行所述類org.dhappy.test.NeoTraverse插件。

因此,觸發命令行插件,只需要運行:

mvn exec:java 

現在,如果你想要執行exec:java目標作爲標準構建的一部分,你需要綁定的目標到default lifecycle的特定階段。要做到這一點,聲明phase到你想要的目標結合在execution元素:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.4</version> 
    <executions> 
    <execution> 
     <id>my-execution</id> 
     <phase>package</phase> 
     <goals> 
     <goal>java</goal> 
     </goals> 
    </execution> 
    </executions> 
    <configuration> 
    <mainClass>org.dhappy.test.NeoTraverse</mainClass> 
    </configuration> 
</plugin> 

有了這個例子,你的類將在package階段執行。這只是一個例子,可以根據您的需要進行調整。也適用於插件版本1.1。

+1

我在第一困惑:'EXEC:java'可也用於Scala和Clojure代碼,它本身不一定是Java代碼。 – rightfold 2015-02-24 10:19:11

+4

版本應該是1.4.0 – 2016-01-18 01:45:11

18

爲了執行多個程序,我還需要一個profiles部分:

<profiles> 
    <profile> 
    <id>traverse</id> 
    <activation> 
     <property> 
     <name>traverse</name> 
     </property> 
    </activation> 
    <build> 
     <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>exec-maven-plugin</artifactId> 
      <configuration> 
      <executable>java</executable> 
      <arguments> 
       <argument>-classpath</argument> 
       <classpath/> 
       <argument>org.dhappy.test.NeoTraverse</argument> 
      </arguments> 
      </configuration> 
     </plugin> 
     </plugins> 
    </build> 
    </profile> 
</profiles> 

這是可執行然後如:

mvn exec:exec -Dtraverse 
+0

那是怎麼回事' -classpath'line?我不認爲這是正確的。 – GreenGiant 2013-10-02 22:10:27

+0

是的,最有可能的''標籤誤入了那裏,應該刪除。所以這條線看起來就是:' -classpath' – 2014-01-17 18:50:53

+3

這不是一個錯誤。這表明在pom.xml中指定的依賴關係應該用作類路徑的一部分。 – user924272 2014-07-03 10:55:06