在 Maven 中,如何运行位于 src/test/java 下的类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4060613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
From Maven, how do I run a class that lives under src/test/java?
提问by woddle
I have inherited a codebase :)
我继承了一个代码库:)
Under src/test/java/ there's a file that I need to run (I need to run its public static void main(String[] args)
, not a @Test
method within it).
在 src/test/java/ 下有一个我需要运行的文件(我需要运行它的public static void main(String[] args)
,而不是其中的@Test
方法)。
The closest I have got is:
我得到的最接近的是:
mvn -e exec:java -Dexec.mainClass="com.me.packagex.RunFile" -Dexec.classpathScope="test"
but that then fails, and it appears to be because RunFile wants to use classes that exist under src/main/java/com/me/packagex/ (notice, /main/, not /test/). The files under there are in the same package as RunFile, i.e. 'package com.me.packagex;'.
但这失败了,这似乎是因为 RunFile 想要使用存在于 src/main/java/com/me/packagex/ 下的类(注意,/main/,而不是 /test/)。那里的文件和RunFile在同一个包中,即'package com.me.packagex;'。
If I remove the -Dexec.classpathScope="test"
then it can't find RunFile at all. It's as if I need to give it two scopes, but it doesn't accept "test,compile".
如果我删除-Dexec.classpathScope="test"
它,那么它根本找不到 RunFile。就好像我需要给它两个范围,但它不接受“测试,编译”。
The person I've inherited this from (dearly departed) used to run it from Eclipse. I need a way to run it from the command-line.
我从(已故的)那里继承了它的人曾经从 Eclipse 运行它。我需要一种从命令行运行它的方法。
I hope this is clearly explained.
我希望这能得到清楚的解释。
tyvm,
tyvm,
This is promising. Pascal, I've tried your example and that doesn'twork for me.
这是有希望的。帕斯卡,我试过你的例子,但对我不起作用。
Although now I look at it - it's not finding Demo, rather than not finding Dog.
虽然现在我看它 - 它不是找到 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.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.run(ExecJavaMojo.java:284)
at java.lang.Thread.run(Thread.java:619)
回答by Pascal Thivent
(...) I hope this is clearly explained.
(...) 我希望这个解释清楚。
Not bad but I can't reproduce. I created a project:
不错,但我无法重现。我创建了一个项目:
$ mvn archetype:generate -DgroupId=com.stackoverflow \ -DartifactId=Q4060613 \ -Dversion=1.0-SNAPSHOT \ -DarchetypeArtifactId=maven-archetype-quickstart
Then cd
ed into it and created a Dog
class (under src/main/java
):
然后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!";
}
}
and created a Demo
class (under src/test/java
):
并创建了一个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());
}
}
After compiling the source code, running the command you provided works as expected:
编译源代码后,运行您提供的命令按预期工作:
$ 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] ------------------------------------------------------------------------ ...
Something else must be wrong.
一定是别的什么地方出了问题。
回答by Jeevan Adiga
Include following lines in pom.xml in exec-maven-plugin plugin. <classpathScope>test</classpathScope>
在 exec-maven-plugin 插件的 pom.xml 中包含以下行。 <classpathScope>test</classpathScope>
plugin section in POM looks something like this
POM 中的插件部分看起来像这样
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>my-execution</id>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.MainClass</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</plugin>
Note : com.example.MainClass is the class containing mainmethod.
注意:com.example.MainClass 是包含main方法的类。
回答by Jin Kwon
I faced the same problem and figured it out.
我遇到了同样的问题并想通了。
Shortly speaking, the class must be compiled beforeexec:java
goal. (It surely works without test-compile
phase if the class is already compiled by other user action. Note that Pascal Thivent, in his answer, invoked mvn test
before exec:java
.)
简而言之,类必须在exec:java
目标之前编译。(test-compile
如果该类已经由其他用户操作编译,它肯定可以在没有阶段的情况下工作。请注意,在他的回答中,Pascal Thivent是在之前调用的。)mvn test
exec:java
$ mvn -Dexec.mainClass=... -Dexec.classpathScope=test test-compile exec:java
You can prove it for yourself if you want to see the ClassNotFoundException
again.
如果你想ClassNotFoundException
再次看到它,你可以为自己证明。
$ mvn -Dexec.mainClass=... -Dexec.classpathScope=test clean exec:java
回答by woddle
Ok, spurred on by it working for everyone else I dug a little harder. The code wasn't reporting its problems very well and I was misreading the stacktrace.
好吧,在它为其他人工作的推动下,我更努力地挖掘。代码没有很好地报告它的问题,我误读了堆栈跟踪。
It does:
它确实:
FileInputStream is = new FileInputStream("lib/other-thing.jar");
which was failing. I symlinked trunk/src/main/assembly/lib/ into trunk/ and now it works. There might be a neater way to fix that then the symlink, though.
这是失败的。我将 trunk/src/main/assembly/lib/ 符号链接到 trunk/ 中,现在它可以工作了。不过,可能有一种更简洁的方法来解决这个问题,然后是符号链接。
Thank you chaps.
谢谢各位。