今天突然想练练自己脱离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 "
cd ts - main
mvn compile
mvn exec:java - Dexec.mainClass = " org.ts.main.App "
依次执行后,能看到如下的结果:
![ContractedBlock.gif](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/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] ------------------------------------------------------------------------
[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](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/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] ------------------------------------------------------------------------
[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] ------------------------------------------------------------------------
好,先到这里... 希望对初学者有所帮助..