Maven运行Java主要

2018-01-09 19:18 更新

Maven教程 - Maven运行Java主要


将源包装到Jar文件后,我们可以使用以下三种方式运行Java main方法。

我们可以使用Maven exec 插件来运行Java类的main方法,项目依赖项将自动包含在类路径中。

从命令行运行

假设我们在前面的章节中创建了项目。要从Maven运行Java main方法,可以使用以下命令。

mvn exec:java -Dexec.mainClass="com.java2s.ide.App"  

上面的代码生成以下结果。

c:\mvn_test\xmlFileEditor>mvn exec:java -Dexec.mainClass="com.java2s.ide.App"
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/maven-metadata.xml
...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xmlFileEditor 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ xmlFileEditor ---
Downloading: https://repo.maven.apache.org/maven2/junit/junit/4.11/junit-4.11.pom
...
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.847 s
[INFO] Finished at: 2014-11-03T16:25:54-08:00
[INFO] Final Memory: 22M/369M
[INFO] ------------------------------------------------------------------------
c:\mvn_test\xmlFileEditor>

带参数:

mvn exec:java -Dexec.mainClass="com.java2s.ide.App" -Dexec.args="arg0 arg1 arg2"

在CLASSPATH中具有运行时依赖关系:

mvn exec:java -Dexec.mainClass="com.java2s.ide.App" -Dexec.classpathScope=runtime


在pom.xml中的一个阶段中运行

我们可以在maven阶段运行 main 方法。例如,您可以运行App.main()方法作为其一部分测试阶段。

<build>  
 <plugins>  
  <plugin>  
   <groupId>org.codehaus.mojo</groupId>  
   <artifactId>exec-maven-plugin</artifactId>  
   <version>1.1.1</version>  
   <executions>  
    <execution>  
     <phase>test</phase>  
     <goals>  
      <goal>java</goal>  
     </goals>  
     <configuration>  
      <mainClass>com.java2s.ide.App</mainClass>  
      <arguments>  
       <argument>arg0</argument>  
       <argument>arg1</argument>  
      </arguments>  
     </configuration>  
    </execution>  
   </executions>  
  </plugin>  
 </plugins>  
</build>  

要运行具有上述配置的exec插件,只需运行相应的阶段。

mvn test 


在pom.xml中的配置文件中运行

我们可以在不同的配置文件中运行main方法。将上述配置包裹在< profile> 标签。

<profiles>  
 <profile>  
  <id>code-generator</id>  
  <build>  
   <plugins>  
    <plugin>  
     <groupId>org.codehaus.mojo</groupId>  
     <artifactId>exec-maven-plugin</artifactId>  
     <version>1.1.1</version>  
     <executions>  
      <execution>  
       <phase>test</phase>  
       <goals>  
        <goal>java</goal>  
       </goals>  
       <configuration>  
        <mainClass>com.java2s.ide.App</mainClass>  
        <arguments>  
         <argument>arg0</argument>  
         <argument>arg1</argument>  
        </arguments>  
       </configuration>  
      </execution>  
     </executions>  
    </plugin>  
   </plugins>  
  </build>  
 </profile>  
</profiles>  

要调用以上配置文件,请运行以下命令:

mvn test -Pcode-generator  
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号