使用命令行执行java程序时出现Maven错误

问题描述 投票:1回答:3

当我从Netbeans运行我的Maven项目时,一切正常。但是,当我使用命令行时:

mvn exec:java -Dexec.mainClass="Main.Laucher" -Dexec.args=$args1 $args2 $args3

我得到了这个错误:

     [ERROR] BUILD FAILURE
     [INFO] ------------------------------------------------------------------------
     [INFO] Invalid task '1': you must specify a valid lifecycle phase, or a goal in the format plugin:goal or
 pluginGroupId:pluginArtifactId:pluginVersion:goal
     [INFO] ------------------------------------------------------------------------
     [INFO] For more information, run Maven with the -e switch
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: < 1 second
    [INFO] Finished at: Mon Apr 21 12:06:23 CEST 2014
     [INFO] Final Memory: 7M/491M

maven版本是:

Apache Maven 2.2.1 (rdebian-8)
Java version: 1.7.0_51
Java home: /usr/lib/jvm/java-7-oracle/jre
Default locale: fr_FR, platform encoding: UTF-8
OS name: "linux" version: "3.2.0-58-generic" arch: "amd64" Family: "unix"

pom.xml我有exec-maven-plugin

     <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
      <execution>
        <goals>
          <goal>java</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
         <mainClass>simulation.amazon.Laucher</mainClass>
    </configuration>
    </plugin>

我该如何解决这个错误?

java bash maven maven-2
3个回答
1
投票

你需要配置exec-maven-plugin插件你的pom.xml例如:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            ...
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.example.Main</mainClass>
          <arguments>
            <argument>argument1</argument>
            ...
          </arguments>
          <systemProperties>
            <systemProperty>
              <key>myproperty</key>
              <value>myvalue</value>
            </systemProperty>
            ...
          </systemProperties>
        </configuration>
      </plugin>
    </plugins>
  </build>
   ...
</project>

0
投票

您已将插件添加为不正确的依赖项。您应该将其添加为插件。

<build>
    <plugins>
        <plugin>
            <groupId>org.co dehaus.mojo</groupId> 
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
        </plugin> 
     </plugins>
</build>

我真的不知道它在NetBeans中是如何工作的,但是这个配置肯定是通过命令行工作所必需的。


0
投票

我在IntelliJ中的maven项目遇到了同样的问题,我找到了一种让它工作的替代方法,即使没有exec-maven-plugin也是如此。我能够通过遵循this答案并在以下pom.xml文件中包含默认目标来构建项目:

<defaultGoal>install</defaultGoal>
    <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.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>

            </configuration>
            <executions>
                <execution>
                    <id>assemble-all</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

然后,即使项目可以构建,每当我在运行配置中使用命令行参数时,我遇到的问题是第一个参数被认为是生命周期的名称。因此,我选择了一个普通的应用程序,而不是定义新的Maven运行配置。选择主类并定义程序参数后,一切正常。

同样,如果必须从命令行运行java程序,则可以首先构建项目然后调用主类来避免此错误。我特意做了:

mvn package
java -cp target/myproject-1.0-SNAPSHOT-jar-with-dependencies.jar run.MyMain "a" "b" "c" "d"

它工作得很好。与之相比,它看起来像是一个充分的解决方法

mvn exec:java  -Dexec.mainClass=run.MyMain "a" "b" "c" "d"

和Intellij的maven配置可能会显示意外错误。

© www.soinside.com 2019 - 2024. All rights reserved.