原文地址:

http://blog.csdn.net/qbg19881206/article/details/19850857?utm_source=tuicool&utm_medium=referral

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中的配置用标签包裹后就能通过指定该配置文件来执行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/

[转] 使用maven运行java main的三种方式的更多相关文章

  1. 使用Maven运行Java main的3种方式

    使用Maven运行Java main的3种方式 原文  http://blog.csdn.net/qbg19881206/article/details/19850857 主题 Maven maven ...

  2. 使用Maven运行Java main的3种方式使用Maven运行Java main的3种方式

    maven使用exec插件运行java main方法,以下是3种不同的操作方式. 一.从命令行运行 1.运行前先编译代码,exec:java不会自动编译代码,你需要手动执行mvn compile来完成 ...

  3. 使用Maven运行Java main的方法(转)

    使用Maven运行Java Main的方法(既Java Application项目),可以有如下方式解决: 1.将Maven项目导入到eclipse中,然后直接项目右键[Run As]->[Ja ...

  4. Maven打jar包的三种方式

    Maven打jar包的三种方式 不包含依赖jar包 该方法打包的jar,不包含依赖的jar包,也没有指定入口类. <build> <plugins> <plugin> ...

  5. Java线程的三种方式

    创建线程有三种方式: 1.继承Thread类 2.实现Runnable接口 3.使用Callable和Future创建线程 三种方式详解如下: ---------------------------- ...

  6. java基础-jdbc——三种方式加载驱动建立连接

    String url = "jdbc:mysql://localhost:3306/student?Unicode=true&characterEncoding=utf-8" ...

  7. (十一)Maven运行TestNG的testcase 两种方式:testng.xml和testngCase.java

    原文:https://blog.csdn.net/wwhrestarting/article/details/46596869?utm_source=copy 1.通过maven-surefire-p ...

  8. java定时任务的三种方式

    /**  * 普通thread  * 这是最常见的,创建一个thread,然后让它在while循环里一直运行着,  * 通过sleep方法来达到定时任务的效果.这样可以快速简单的实现,代码如下 */  ...

  9. java 定时器的三种方式

    原地址:http://blog.csdn.net/haorengoodman/article/details/23281343/ /** * 普通thread * 这是最常见的,创建一个thread, ...

随机推荐

  1. 视频播放(iOS开发)

    视频播放 一.视频播放介绍(5种实现方案) AVPlayer 优点 可以自定义UI,进行控制 缺点 单纯的播放,没有控制UI,而且如果要显示播放界面,需要借助AVPlayerLayer,添加图层到需要 ...

  2. struts总结

    struts总结 1.为什么学习Struts框架 a.Struts框架好处 struts2是主流的开发技术,大多数公司在使用,struts把相关的servlet组件的各种操作都进行了相应的封装,这样就 ...

  3. C# .Net基础知识点解答

    原文地址 1. 什么是.NET?什么是CLI?什么是CLR?IL是什么?JIT是什么,它是如何工作的?GC是什么,简述一下GC的工作方式? 通俗的讲,.Net是微软开发应用程序的一个平台: CLI是C ...

  4. validatebox实现多重规则验证

    作为Easyui的校验插件没有实现多重校验能力是一种缺憾.比如说,既要限制格式为email,同时要求最长长度为20,我们就要扩展一种规则,而对长度的要求很容易变化,如果变成要求30,我们又得扩张一种规 ...

  5. 新建共享,NTFS权限设置

    1. Overview Some time ago, I was automating a few tasks with PowerShell and needed to set NTFS permi ...

  6. Java 实现组合(Composite)模式

    类图 /** * 树 总体 * * @author stone * */ public class Tree { private TreeNode root; //根节点 public Tree(St ...

  7. Ubuntu 12.04 64bit 配置完android 5.0编译环境后出现“could not write bytes: Broken pipe.”而无法进入输入帐号密码的登陆界面

    Ubuntu 12.04 64bit 配置完android 5.0编译环境后出现“could not write bytes: Broken pipe.”而无法进入输入帐号密码的登陆界面.上网问了问百 ...

  8. jfinal文件上传和form表单值为null的解决方法

    今天使用jfinal做上传提交的时候,遇到一个问题:添加了上传功能,原来的form表单submit提交时所有值都为null了,研究了很长时间,终于发现 在jfinal上传时候,jsp加 enctype ...

  9. sharepoint 2013 更改搜索server组态

    1.新搜索server在.安装sharepoint server 2013,并连接到一个现有的sharepoint server领域,完成后.您可以配置新的搜索server. 打开sharepoint ...

  10. Android框架浅析之锁屏(Keyguard)机制原理

    最近终于成功的摆脱了FM收音机,迈向了新的模块:锁屏.状态栏.Launcher---姑且称之为“IDLE”小组,或许叫手机 美容小组,要是能施展下周星星同学的还我漂漂拳,岂不快哉. OK,闲话打住,咱 ...