【问题标题】:From Maven, how do I run a class that lives under src/test/java?在 Maven 中,如何运行位于 src/test/java 下的类?
【发布时间】:2011-05-02 21:58:54
【问题描述】:

我继承了一个代码库:)

在 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/ 下存在的类(注意,/main/,而不是 /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 没有工作,因为我没有进行编译/测试编译。现在更仔细地查看原始示例的错误跟踪。

标签: java maven-2 exec-maven-plugin


【解决方案1】:

我遇到了同样的问题并想通了。

简而言之,类必须在exec:java目标之前编译。 (如果该类已经由其他用户操作编译,它肯定可以在没有test-compile 阶段的情况下工作。请注意,Pascal Thiventhis answer 中,在exec:java 之前调用mvn test。)

$ mvn -Dexec.mainClass=... -Dexec.classpathScope=test test-compile exec:java

如果你想再次看到ClassNotFoundException,你可以自己证明。

$ mvn -Dexec.mainClass=... -Dexec.classpathScope=test clean exec:java

【讨论】:

    【解决方案2】:

    (...) 我希望能清楚地解释这一点。

    不错,但我无法复制。我创建了一个项目:

    $ mvn 原型:生成-DgroupId=com.stackoverflow \ -DartifactId=Q4060613 \ -Dversion=1.0-快照\ -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 测试 ... $ mvn exec:java -Dexec.mainClass="com.stackoverflow.Demo" -Dexec.classpathScope="test" [INFO] 正在扫描项目... ... [信息] --- exec-maven-plugin:1.2:java (default-cli) @ Q4060613 --- 哇! [信息] --------------------------------------------- ------------------------- [信息] 构建成功 [信息] --------------------------------------------- ------------------------- ...

    肯定有其他地方出错了。

    【讨论】:

    • 试过这个并将输出添加到我原来的问题中。
    • @seanizer:ROFL。我想我的意思是吠叫:)
    • 特别是-Dexec.classpathScope="test"
    • ClassNotFoundException 因为你在 exec:java 之前做过 mvn test
    • 很多有用的 cmets:最终它引导我:mvn test-compile exec:java -Dexec.mainClass="com.stackoverflow.Demo" -Dexec.classpathScope="test"
    【解决方案3】:

    在 exec-maven-plugin 插件的 pom.xml 中包含以下行。 <classpathScope>test</classpathScope>

    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>
    

    注意:com.example.MainClass 是包含 ma​​in 方法的类。

    【讨论】:

    • 很多有用的 cmets:最终它引导我:mvn test-compile exec:java -Dexec.mainClass="com.stackoverflow.Demo" -Dexec.classpathScope="test"
    【解决方案4】:

    好的,在它为其他所有人工作的推动下,我更加努力地挖掘。代码没有很好地报告它的问题,我误读了堆栈跟踪。

    确实如此:

    FileInputStream is = new FileInputStream("lib/other-thing.jar");
    

    失败了。我将trunk/src/main/assembly/lib/符号链接到trunk/中,现在它可以工作了。不过,可能有比符号链接更巧妙的方法来解决这个问题。

    谢谢各位。

    【讨论】:

      猜你喜欢
      • 2015-02-09
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      相关资源
      最近更新 更多