我继承了一个代码库:)
在src/test/java /下面有一个我需要运行的文件(我需要运行它public static void main(String[] args)
,而不是其中的@Test
方法).
我最接近的是:
mvn -e exec:java -Dexec.mainClass="com.me.packagex.RunFile" -Dexec.classpathScope="test"
但那失败了,似乎是因为RunFile想要使用src/main/java/com/me/packagex /(notice,/ main /,not/test /)下的类.其下的文件与RunFile在同一个包中,即'package com.me.packagex;'.
如果我删除它-Dexec.classpathScope="test"
然后它根本找不到RunFile.就好像我需要给它两个范围,但它不接受"测试,编译".
我从(亲爱的离开)继承的人曾经从Eclipse运行它.我需要一种从命令行运行它的方法.
我希望这清楚地解释了.
tyvm,
这很有希望.帕斯卡,我已经试过你的榜样,并且没有为我工作.
虽然现在我看着它 - 它没有找到Demo,而不是找到Dog.
Apache Maven 2.2.1 (rdebian-1) Java version: 1.6.0_18 Java home: /usr/lib/jvm/java-6-openjdk/jre Default locale: en_GB, platform encoding: UTF-8 OS name: "linux" version: "2.6.32-25-generic" arch: "i386" Family: "unix" $ mvn -e exec:java -Dexec.mainClass="com.stackoverflow.Demo" -Dexec.classpathScope="test" [ERROR] BUILD ERROR [INFO] ------------------------------------------------------------------------ [INFO] An exception occured while executing the Java class. com.stackoverflow.Demo [INFO] ------------------------------------------------------------------------ [INFO] Trace org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executing the Java class. com.stackoverflow.Demo at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138) at org.apache.maven.cli.MavenCli.main(MavenCli.java:362) at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315) at org.codehaus.classworlds.Launcher.launch(Launcher.java:255) at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430) at org.codehaus.classworlds.Launcher.main(Launcher.java:375) Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. com.stackoverflow.Demo at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:346) at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694) ... 17 more Caused by: java.lang.ClassNotFoundException: com.stackoverflow.Demo at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:284) at java.lang.Thread.run(Thread.java:619)
Pascal Thive.. 20
(...)我希望这清楚地解释.
不错,但我无法重现.我创建了一个项目:
$ mvn archetype:generate -DgroupId=com.stackoverflow \ -DartifactId=Q4060613 \ -Dversion=1.0-SNAPSHOT \ -DarchetypeArtifactId=maven-archetype-quickstart
然后cd
编辑并创建一个Dog
类(下src/main/java
):
$ cd Q4060613 $ cat > src/main/java/com/stackoverflow/Dog.java package com.stackoverflow; public class Dog { public String bark() { return "woof!"; } }
并创建了一个Demo
类(下src/test/java
):
$ cat > src/test/java/com/stackoverflow/Demo.java package com.stackoverflow; public class Demo { public static void main(String[] args) { System.out.println(new Dog().bark()); } }
编译源代码后,运行您提供的命令按预期工作:
$ mvn test ... $ mvn exec:java -Dexec.mainClass="com.stackoverflow.Demo" -Dexec.classpathScope="test" [INFO] Scanning for projects... ... [INFO] --- exec-maven-plugin:1.2:java (default-cli) @ Q4060613 --- woof! [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ...
别的一定是错的.
(...)我希望这清楚地解释.
不错,但我无法重现.我创建了一个项目:
$ mvn archetype:generate -DgroupId=com.stackoverflow \ -DartifactId=Q4060613 \ -Dversion=1.0-SNAPSHOT \ -DarchetypeArtifactId=maven-archetype-quickstart
然后cd
编辑并创建一个Dog
类(下src/main/java
):
$ cd Q4060613 $ cat > src/main/java/com/stackoverflow/Dog.java package com.stackoverflow; public class Dog { public String bark() { return "woof!"; } }
并创建了一个Demo
类(下src/test/java
):
$ cat > src/test/java/com/stackoverflow/Demo.java package com.stackoverflow; public class Demo { public static void main(String[] args) { System.out.println(new Dog().bark()); } }
编译源代码后,运行您提供的命令按预期工作:
$ mvn test ... $ mvn exec:java -Dexec.mainClass="com.stackoverflow.Demo" -Dexec.classpathScope="test" [INFO] Scanning for projects... ... [INFO] --- exec-maven-plugin:1.2:java (default-cli) @ Q4060613 --- woof! [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ...
别的一定是错的.
在exec-maven-plugin插件的pom.xml中包含以下行.
POM中的插件部分看起来像这样
org.codehaus.mojo exec-maven-plugin my-execution test java com.example.MainClass test
注意:com.example.MainClass是包含main方法的类.
我遇到了同样的问题并弄明白了.
简而言之,必须在 exec:java
目标之前编译该类.(test-compile
如果该类已经被其他用户操作编译,那么它肯定没有阶段.注意Pascal Thivent,在他的回答中,mvn test
之前调用过exec:java
.)
$ mvn -Dexec.mainClass=... -Dexec.classpathScope=test test-compile exec:java
如果你想ClassNotFoundException
再次看到它,你可以自己证明.
$ mvn -Dexec.mainClass=... -Dexec.classpathScope=test clean exec:java