[英]Maven error when using command line to execute java program
當我從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>
如何解決此錯誤?
您需要為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>
您已將插件添加為不正確的依賴項。 您應該將其添加為插件。
<build>
<plugins>
<plugin>
<groupId>org.co dehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
</plugin>
</plugins>
</build>
我沒有真正看到它在NetBeans中的工作方式,但是絕對必須通過此配置才能使用此配置。
我在IntelliJ中的maven項目遇到了相同的問題,即使沒有exec-maven-plugin,我也找到了使其工作的替代方法。 我能夠按照建設項目這個答案,並包括以下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運行配置,而是選擇了普通的Application 。 在選擇主類並定義了程序參數之后,一切都正常。
同樣,如果必須從命令行運行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配置可能顯示意外錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.