简体   繁体   English

通过pom.xml执行主类

[英]Executing Main Class through pom.xml

Guys I know I am asking a very silly question, but I am very curious like why my test is not running. 伙计们,我知道我在问一个非常愚蠢的问题,但是我很好奇为什么我的测试没有运行。

I have created simple maven project(no junits, etc just a simple main class) with one only main class in a test folder and I am trying to execute the same through pom.xml. 我已经创建了一个简单的maven项目(没有junits,等等,只是一个简单的主类),在测试文件夹中只有一个主类,而我试图通过pom.xml执行相同的项目。

I have gone through the existing question's over here but that didn't resolve it 我已经解决了现有问题,但是并没有解决

When I try to execute it I got the following output. 当我尝试执行它时,得到以下输出。

Running samplemav.TestOne
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 sec

pom.xml 的pom.xml

<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">
<modelVersion>4.0.0</modelVersion>
<groupId>samplemav</groupId>
<artifactId>samplemav</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20</version>
            <configuration>
                <includes>
                    <include>samplemav.TestOne</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

Java Class Java类

 package samplemav;
 public class TestOne {

    public static void main(String args[]){

        System.out.println("test");
    }

}

Regards 问候

I am not sure about the discussion on the question in the comments. 我不确定评论中有关该问题的讨论。 As per my understanding you can execute the main app just fine through the mvn command. 据我了解,您可以通过mvn命令执行主应用程序。 Check the below link. 检查以下链接。

Building and Running a Java SE Application by Using Maven 使用Maven构建和运行Java SE应用程序

follow the instructions as mentioned in the documentation. 请遵循文档中提到的说明。

In your case the command to execute will be 在您的情况下,要执行的命令将是

mvn exec:java -Dexec.mainClass="samplemav.TestOne" mvn exec:java -Dexec.mainClass =“ samplemav.TestOne”

I am assuming you may not have to provide settings.xml path 我假设您可能不必提供settings.xml路径

You are using maven-surefire-plugin to build you app so obviously it will try to execute test project. 您正在使用maven-surefire-plugin构建您的应用程序,因此显然它将尝试执行测试项目。 Now because there is no classes in there it will obviously give you the output you received. 现在,由于那里没有类,因此显然可以为您提供收到的输出。

You may want to use Junit for unit testing. 您可能要使用Junit进行单元测试。

package samplemav;
import org.junit.Test;
 public class TestOne {

    @Test
    public void test() {

        System.out.println("test");
    }

}

And add test dependency your pom.xml 并将测试依赖项添加到pom.xml

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM