使用 Maven 执行 java main class(java应用程序)

今天突然想练练自己脱离IDE的 hard code 水平,于是只是用 maven & UE 做了一些练习,突然发现了一个问题,如何使用maven指令执行 java main 呢?一开始想了一个比较直接的解决方式,直接使用 java 指令:

 
  
java - cp target / classes org.ts.App

但这样明显比较麻烦...,以前跑测试时,一条指令解决所有问题,像这样:

 
  
mvn clean compile test

但现在应为要用到 java ... ,所以要下两次指令...

 

那么 maven 就不能执行 java main 吗?显然 maven 不可能这么弱,google一下.. 果然发现了已经有现成的博文了:http://www.vineetmanohar.com/2009/11/02/3-ways-to-run-java-main-from-maven/

 

博文是E文的,但很简单,说明的也蛮详细,这里不再赘述。

不过列出以下maven指令集,无需编写任何代码,初学者可依次执行指令,就能看到结果:

 
  
mvn archetype:create - DgroupId = org.ts.main - DartifactId = ts - main
cd ts
- main
mvn compile
mvn exec:java
- Dexec.mainClass = " org.ts.main.App "

依次执行后,能看到如下的结果:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
C:\Users\Timothy\ts - main > mvn exec:java - Dexec.mainClass = " org.ts.main.App "
[INFO] Scanning
for projects...
[INFO] Searching repository
for plugin with prefix: 'exec'.
[INFO]
------------------------------------------------------------------------
[INFO] Building ts
- main
[INFO] task
- segment: [exec:java]
[INFO]
------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed
for project - skipping
[INFO] [exec:java {execution:
default - cli}]
Hello World
!
[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO]
------------------------------------------------------------------------
[INFO] Total time:
1 second
[INFO] Finished at: Mon May
10 02 : 53 : 50 CST 2010
[INFO] Final Memory: 5M
/ 15M
[INFO]
------------------------------------------------------------------------

 

如果需要编辑App.java,完成后,直接使用以下指令即可看到结果:

 
  
mvn clean compile exec:java - Dexec.mainClass = " org.ts.main.App "

 

比如我将"Hello world!" 修改为"Hello Timothy!"后,则只要按一次 [上档]+[Enter],即可看到执行结果:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
C:\Users\Timothy\ts - main > mvn clean compile exec:java - Dexec.mainClass = " org.ts.main.App "
[INFO] Scanning
for projects...
[INFO] Searching repository
for plugin with prefix: 'exec'.
[INFO]
------------------------------------------------------------------------
[INFO] Building ts
- main
[INFO] task
- segment: [clean, compile, exec:java]
[INFO]
------------------------------------------------------------------------
[INFO] [clean:clean {execution:
default - clean}]
[INFO] Deleting directory C:\Users\Timothy\ts
- main\target
[INFO] [resources:resources {execution:
default - resources}]
[INFO] Using 'UTF
- 8 ' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Timothy\ts
- main\src\main\resources
[INFO] [compiler:compile {execution:
default - compile}]
[INFO] Compiling
1 source file to C:\Users\Timothy\ts - main\target\classes
[INFO] Preparing exec:java
[INFO] No goals needed
for project - skipping
[INFO] [exec:java {execution:
default - cli}]
Hello Timothy
!
[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO]
------------------------------------------------------------------------
[INFO] Total time:
3 seconds
[INFO] Finished at: Mon May
10 02 : 58 : 33 CST 2010
[INFO] Final Memory: 10M
/ 26M
[INFO]
------------------------------------------------------------------------

 

好,先到这里... 希望对初学者有所帮助..

转载于:https://www.cnblogs.com/foxswl/archive/2010/05/10/maven-execute-java-main.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值