当我从cmd运行java Maven Project时,我得到MojoExecutionException

时间:2019-06-09 14:25:26

标签: java maven maven-2 maven-exec-plugin

我从命令行运行Maven-Project时遇到问题 我在cmd中使用了此命令来运行名为mainClass的类

   mvn -e exec:java -Dexec.mainClass="com.example.Main"

这是我的pom文件中的内容:

   <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>Linux</groupId>
   <artifactId>Linux</artifactId>
   <version>1.0</version>

   <dependencies>
       <dependency>
           <groupId>org.seleniumhq.selenium</groupId>
           <artifactId>selenium-java</artifactId>
           <version>2.37.1</version>
       </dependency>

       <dependency>
           <groupId>org.codehaus.groovy</groupId>
           <artifactId>groovy-all</artifactId>
           <!-- Needs to be the same version that REST Assured depends on -->
           <version>2.1.2</version>
           <scope>test</scope>
       </dependency>

       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.8.1</version>
       </dependency>
   </dependencies>

    </project>

由于类未找到异常而导致Mojo异常,但实际上我不知道该在何处提供名称或要运行的类的路径

这是命令行中命令的响应

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我已经修改了您的Maven pom.xml并添加了Maven mojo插件。您可以在代码下面找到。

首先,您必须构建项目,然后才能执行Main java类。 在这里,按照步骤操作。

  1. 转到命令提示符,然后转到包含pom.xml的项目/目录。
  2. 键入命令mvnw clean package。我用过Maven包装器。
  3. 然后键入命令mvnw exec:java -Dexec.mainClass="com.so.help.maven.Main"

您可以在github中找到示例项目。

<?xml version="1.0" encoding="UTF-8"?>

<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>execute-java-maven</groupId>
    <artifactId>execute-java-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>execute-java-maven</name>
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.37.1</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <!-- Needs to be the same version that REST Assured depends on -->
            <version>2.1.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.6.0</version>
                    <configuration>
                        <executable>java</executable>
                        <arguments>
                            <argument>com.so.help.maven.Main</argument>
                        </arguments>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>