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

     

原文地址:https://www.cnblogs.com/jedjia/p/maven-main.html