使用Maven运行Java main的3种方式
2015-09-15 23:31
591 查看
From: http://www.tuicool.com/articles/UJJvim
maven使用exec插件运行java main方法,以下是3种不同的操作方式。
一、 从命令行运行
1、运行前先编译代码,exec:java不会自动编译代码,你需要手动执行mvn compile来完成编译。
2、编译完成后,执行exec运行main方法。
不需要传递参数:
需要传递参数:
指定对classpath的运行时依赖:
二、 在pom.xml中指定某个阶段执行
将CodeGenerator.main()方法的执行绑定到maven的 test 阶段,通过下面的命令可以执行main方法:
三、 在pom.xml中指定某个配置来执行
将2中的配置用<profile>标签包裹后就能通过指定该配置文件来执行main方法,如下:
注:通过以下命令可以获取mvn exec的其他配置参数说明。
英文地址: http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/
maven使用exec插件运行java main方法,以下是3种不同的操作方式。
一、 从命令行运行
1、运行前先编译代码,exec:java不会自动编译代码,你需要手动执行mvn compile来完成编译。
mvn compile
2、编译完成后,执行exec运行main方法。
不需要传递参数:
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main"
需要传递参数:
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2"
指定对classpath的运行时依赖:
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime
二、 在pom.xml中指定某个阶段执行
<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.vineetmanohar.module.CodeGenerator</mainClass> <arguments> <argument>arg0</argument> <argument>arg1</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> </build>
将CodeGenerator.main()方法的执行绑定到maven的 test 阶段,通过下面的命令可以执行main方法:
mvn test
三、 在pom.xml中指定某个配置来执行
<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.vineetmanohar.module.CodeGenerator</mainClass> <arguments> <argument>arg0</argument> <argument>arg1</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> </build>
</profile>
</profiles>
将2中的配置用<profile>标签包裹后就能通过指定该配置文件来执行main方法,如下:
mvn test -Pcode-generator
注:通过以下命令可以获取mvn exec的其他配置参数说明。
mvn exec:help -Ddetail=true -Dgoal=java
英文地址: http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/
相关文章推荐
- 8张图理解Java
- BlackBerry Java 开发环境搭建及插件安装
- java数据类型
- 这些天自身努力的体会,关于java方面的
- Java集合Set、List、Map的遍历方法
- eclipse 上传项目到git
- 详解java类的生命周期
- java线程中断和终止线程运行
- java学习记录之——泛型
- java序列化和反序列化
- Java之——Unsupported version number [51.0] for class format
- Java中类似于eval()的函数?
- Struts2 拦截器详细配置过程
- 转载的,讲解java.util的集合类
- NetBeans实现的简单计算器
- Java之——使用proguard混淆java web项目代码
- java 读取txt文件内容(一)
- Spring定时器的时间表达式
- about <<Not only on Java>>
- Java中的Callable和Future