maven項目執行main方法講解


       項目中有時候會遇到執行main函數來測試類中所寫的方法。普通的java程序在eclipse中執行非常簡單,對要執行的java類,run as 即可編譯運行,查看結果。

      但是使用maven管理項目,對於maven項目還按照原來的方式就行不通了。下面講解下如何在maven項目中執行main函數。

    一、  maven項目執行main函數方法,需引入兩個插件:maven-compiler-plugin和exec-maven-plugin插件。

     maven-compiler-plugin :用於編譯java文件

     exec-maven-plugin:用來執行class文件,其中插件配置中需指明執行類的路徑。

    具體引入,參考maven項目中的pom.xml文件配置。

   

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 2   <modelVersion>4.0.0</modelVersion>
 3   <groupId>com.hik.shiro.tutorial</groupId>
 4   <artifactId>shiro-tutorial</artifactId>
 5   <version>0.0.1-SNAPSHOT</version>
 6   <name>First Apache Shiro Application</name>
 7   <description>First Apache Shiro Application</description>
 8   
 9   <properties>
10         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11     </properties>
12     
13      <build>
14         <plugins>
15             <plugin>
16                 <groupId>org.apache.maven.plugins</groupId>
17                 <artifactId>maven-compiler-plugin</artifactId>
18                 <version>3.6.1</version>
19                 <configuration>
20                     <source>1.7</source>
21                     <target>1.7</target>
22                     <encoding>${project.build.sourceEncoding}</encoding>
23                 </configuration>
24             </plugin>
25 
26         <!-- This plugin is only to test run our little application.  It is not
27              needed in most Shiro-enabled applications: -->
28             <plugin>
29                 <groupId>org.codehaus.mojo</groupId>
30                 <artifactId>exec-maven-plugin</artifactId>
31                 <version>1.6.0</version>
32                 <executions>
33                     <execution>
34                         <goals>
35                             <goal>java</goal>
36                         </goals>
37                     </execution>
38                 </executions>
39                 <configuration>
40                     <classpathScope>test</classpathScope>
41                     <mainClass>shiro.Tutorial</mainClass>
42                 </configuration>
43             </plugin>
44         </plugins>
45     </build>
46   
47   <dependencies>
48       <dependency>
49         <groupId>org.apache.shiro</groupId>
50         <artifactId>shiro-core</artifactId>
51         <version>1.2.4</version>
52     </dependency>
53       <dependency>
54         <groupId>org.slf4j</groupId>
55         <artifactId>slf4j-log4j12</artifactId>
56         <version>1.7.25</version>
57         <scope>test</scope>
58     </dependency>
59   </dependencies>
60 </project>
View Code

  配置需注意的地方:mainClass 一定能跳轉到執行的類,否則執行報錯會找不到類。

 

 

  二、eclipse執行maven項目配置

右鍵項目名稱->RUN As->Run Configurations

 

 需注意的地方:

    eclipse編譯器Maven插件已經幫你加上了mvn前綴,因此在配置編譯,執行命令可省略,否則會報錯:

    錯誤LifecyclePhaseNotFoundException,Unknown lifecycle phase"mvn". You must specify a valid lifecycle

 

 

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM