【问题标题】:when I run java Maven Project from cmd I get MojoExecutionException当我从 cmd 运行 java Maven 项目时,我得到 MojoExecutionException
【发布时间】:2019-06-09 14:25:26
【问题描述】:

我从命令行运行 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 异常,但实际上我不知道应该在哪里提供要运行的类的名称或路径

这是命令行中命令的响应

【问题讨论】:

  • 你可以在这里发布 Main.java 文件吗?

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


【解决方案1】:

我修改了你的 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>

【讨论】:

  • 非常感谢,非常有帮助。
  • 你知道我是否可以像在CMD中一样在linux上运行项目@Sambit
猜你喜欢
  • 2016-03-02
  • 2021-01-16
  • 1970-01-01
  • 2023-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 2016-12-09
相关资源
最近更新 更多