2010-02-11 58 views
3

如何在構建過程中或構建完成後執行Java程序?是否有可能直接從POM做到這一點?如何編寫Maven構建腳本以執行Java

mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main 

編輯

比方說,我想執行org.eclipse.content.MyClass。我將如何編寫代碼?

此生成項目,但它不執行我的課:

<plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>exec-maven-plugin</artifactId> 
       <version>1.1</version> 
       <executions> 
        <execution> 
         <phase>deploy</phase> 
         <goals> 
          <goal>java</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <mainClass>org.eclipse.content.MyClass</mainClass> 
       </configuration> 
      </plugin> 

回答

4

配置Maven-EXEC與你的POM -plugin

<build> 
<plugins> 
    <plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.1</version> 
    <executions> 
     <execution> 
     <phase>yourPhase</phase> 
     ... 
     <goals> 
      <goal>java</goal> 
     </goals> 
      <configuration> 
      <mainClass>mainClass=org.sonatype.mavenbook.weather.Main</mainClass> 
      </configuration> 
     </execution> 
    </executions> 
    </plugin> 
</plugins> 
</build> 

在這個插件應該跑什麼線<phase>yourPhase</phase>插入行家phase

該類需要在pom classpath中可用(作爲源或依賴項)。在其他情況下,如果它不應該是項目依賴項,請閱讀this article如何配置exec插件。

+0

@cetnar這是怎麼回事? ' yourPhase ... ',可以說我想執行org.eclipse.content.MyClass如何編寫代碼? – 2010-02-11 09:29:53

+0

@cetnar我更新了我的問題 – 2010-02-11 09:37:20

+0

@Gandalf我已經更新了我的答案。主要類和其他參數可以放在配置部分。 – cetnar 2010-02-11 09:39:42

2

嘗試exec maven plugin ......哎呀,我應該更仔細地閱讀:-(

+0

@PéterTörök我更新了我的問題 – 2010-02-11 09:38:15