简体   繁体   English

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

[英]Maven error when using command line to execute java program

When I run my Maven project from Netbeans everything works properly. 当我从Netbeans运行我的Maven项目时,一切正常。 However, when i use the command line: 但是,当我使用命令行时:

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

I obtain this error: 我收到此错误:

     [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

The maven version is : 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"

In pom.xml i have exec-maven-plugin : 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> 

How can I resolve this error? 如何解决此错误?

you need to configure exec-maven-plugin plugin your pom.xml for example: 您需要为pom.xml配置exec-maven-plugin插件,例如:

<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>

You have added the plugin as a dependency which is incorrect. 您已将插件添加为不正确的依赖项。 You should add it as a plugin. 您应该将其添加为插件。

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

I don't really see how it is working in NetBeans but this configuration is definitely required for it to work via the command line. 我没有真正看到它在NetBeans中的工作方式,但是绝对必须通过此配置才能使用此配置。

I am having the same issue with my maven project in IntelliJ and I found an alternative way to make it work, even without the exec-maven-plugin. 我在IntelliJ中的maven项目遇到了相同的问题,即使没有exec-maven-plugin,我也找到了使其工作的替代方法。 I was able to build the project by following this answer and including a default goal in the following pom.xml file: 我能够按照建设项目这个答案,并包括以下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>

Then, even though the project could be build, whenever I used command line parameters in the run configuration, I had the same issue that the first argument was considered the lifecycle's name. 然后,即使可以构建项目,只要在运行配置中使用命令行参数,我都会遇到一个问题,即第一个参数被认为是生命周期的名称。 So, instead of defining a new Maven run configuration, I chose a normal Application . 因此,我没有定义新的Maven运行配置,而是选择了普通的Application After selecting the main class and defining the program parameters as well, everything worked. 在选择主类并定义了程序参数之后,一切都正常。

Similarly, if you must run your java program from the command line, you can avoid this error by first building your project and then calling your main class. 同样,如果必须从命令行运行Java程序,则可以通过首先构建项目然后调用主类来避免此错误。 I specifically did: 我专门做了:

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

and it worked fine. 而且效果很好。 It looks like a sufficient workaround in comparison to 与之相比,它似乎是一种足够的解决方法

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

and Intellij's maven configuration that might show unexpected errors. 和Intellij的Maven配置可能显示意外错误。

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

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