Maven在测试阶段之前运行类:exec-maven-plugin exec:java不执行类 [英] Maven run class before test phase: exec-maven-plugin exec:java not executing class

查看:131
本文介绍了Maven在测试阶段之前运行类:exec-maven-plugin exec:java不执行类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Jenkins盒子上运行用Maven构建的jUnit4测试.我的目标是在执行测试之前还原测试数据库.

I am running jUnit4 tests, built with Maven, on a Jenkins box. My goal is to restore a test database before executing the tests.

看起来exec-maven-plugin是可行的方法,但我无法使其运行.有指针吗?尽管有很多示例,但mojo网站上的文档非常薄.

It looks like exec-maven-plugin is the way to go, but I cannot get it running. Any pointers? Although there are lots of examples, the doc on the mojo site is pretty thin.

我当前需要运行的课程位于:

The class I need to run currently lives at:

MyProject.src.test.java._tools.BuildTestEnvironment.java

MyProject.src.test.java._tools.BuildTestEnvironment.java

我的pom.xml包括:

My pom.xml includes:

<pluginManagement>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>            
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <id>build-test-environment</id>
                    <phase>generate-test-resources</phase>            
                    <goals>
                        <goal>java</goal>            
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>src.test.java._tools.BuildTestEnvironment</mainClass>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

在Jenkins中运行此命令,实际上什么也没发生.如果我在本地运行它,我会得到

Run this in Jenkins, nothing really happens. If I run it locally, I get

我尝试过的事,没有成功:

Things I have tried, without success:

  1. 在Jenkins中运行构建:什么都没有发生.该项目将构建并开始运行测试,但我的课程未运行.

  1. Run the build in Jenkins: Nothing really happens. The project builds and starts running tests, but my class is not run.

在本地运行构建:与Jenkins中的结果相同.没什么奇怪的.

Run the build locally: Same result as in Jenkins. No surprise there.

在本地运行generate-test-resources:ClassNotFoundException.即:

Run generate-test-resources locally: ClassNotFoundException. ie:

MVN exec:java generate-test-resources
java.lang.ClassNotFoundException:src.test.java._tools.BuildTestEnvironment

mvn exec:java generate-test-resources
java.lang.ClassNotFoundException: src.test.java._tools.BuildTestEnvironment

  • 将该类编译到一个jar中,然后将其添加到我的pom中.

  • Compile the class into a jar, and add that to my pom.

    更新:

    阅读@ppuskar的评论后,我尝试将buildxxx类稍微移动一下.将其移至src.main.java._tools.BuildTestEnvironment之后,我仍然收到类似的消息.如果有帮助,这是我的构建日志:

    After reading @ppuskar's comments, I tried moving my buildxxx class around a bit. After moving it to src.main.java._tools.BuildTestEnvironment, I still get a similar message. Here is my build log, in case that helps:

    [DEBUG]调用:test.java._tools.BuildTestEnvironment.main()
    [DEBUG]插件依赖项将被排除.
    [DEBUG]将包含项目依赖项.
    [DEBUG]收集的项目工件[joda-time:joda-time:jar:2.3:compile,net.sf.jt400:jt400:jar:6.7:compile,junit:junit:jar:4.11:compile,org.hamcrest:hamcrest -core:jar:1.3:compile,com.fasterxml.jackson.core:jackson-core:jar:2.3.0:compile,com.fasterxml.jackson.core:jackson-databind:jar:2.3.0:compile,com .fasterxml.jackson.core:jackson-annotations:jar:2.3.0:compile,org.hamcrest:hamcrest-all:jar:1.3:compile,org.apache.logging.log4j:log4j-api:jar:2.0-rc1 :compile,org.apache.logging.log4j:log4j-core:jar:2.0-rc1:compile]
    [DEBUG]收集的项目类路径[C:\ workspace \ VSP_UnitTest \ target \ classes]
    [DEBUG]添加到类路径中:file:/C:/workspace/VSP_UnitTest/target/classes/
    [DEBUG]添加项目依赖项:joda-time到classpath
    [DEBUG]将项目依赖工件:jt400添加到classpath
    [DEBUG]添加项目依赖项:junit到classpath
    [DEBUG]将项目依赖工件:hamcrest-core添加到classpath
    [DEBUG]添加项目依赖项:jackson-core到classpath
    [DEBUG]添加项目依赖项目:jackson-databind到classpath
    [DEBUG]添加项目依赖工件:类路径的杰克逊注释
    [DEBUG]将项目依赖工件:hamcrest-all添加到classpath
    [DEBUG]将项目依赖工件:log4j-api添加到classpath
    [DEBUG]添加项目依赖项:log4j-core到classpath
    [DEBUG]加入线程Thread [test.java._tools.BuildTestEnvironment.main(),5,test.java._tools.BuildTestEnvironment]
    [警告] java.lang.ClassNotFoundException:test.java._tools.BuildTestEnvironment
    在java.net.URLClassLoader $ 1.run(URLClassLoader.java:366)
    在java.net.URLClassLoader $ 1.run(URLClassLoader.java:355)
    在java.security.AccessController.doPrivileged(本机方法)上
    在java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    在java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    在java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    在org.codehaus.mojo.exec.ExecJavaMojo $ 1.run(ExecJavaMojo.java:281)
    在java.lang.Thread.run(Thread.java:724)
    [INFO] ----------------------------------------------- -------------------------
    [INFO]建立失败
    [INFO] ----------------------------------------------- -------------------------
    [INFO]总时间:2.602 s
    [INFO]完成于:2014-05-15T14:38:50-05:00
    [INFO]最终内存:12M/152M
    [INFO] ----------------------------------------------- -------------------------

    [DEBUG] Invoking : test.java._tools.BuildTestEnvironment.main()
    [DEBUG] Plugin Dependencies will be excluded.
    [DEBUG] Project Dependencies will be included.
    [DEBUG] Collected project artifacts [joda-time:joda-time:jar:2.3:compile, net.sf.jt400:jt400:jar:6.7:compile, junit:junit:jar:4.11:compile, org.hamcrest:hamcrest-core:jar:1.3:compile, com.fasterxml.jackson.core:jackson-core:jar:2.3.0:compile, com.fasterxml.jackson.core:jackson-databind:jar:2.3.0:compile, com.fasterxml.jackson.core:jackson-annotations:jar:2.3.0:compile, org.hamcrest:hamcrest-all:jar:1.3:compile, org.apache.logging.log4j:log4j-api:jar:2.0-rc1:compile, org.apache.logging.log4j:log4j-core:jar:2.0-rc1:compile]
    [DEBUG] Collected project classpath [C:\workspace\VSP_UnitTest\target\classes]
    [DEBUG] Adding to classpath : file:/C:/workspace/VSP_UnitTest/target/classes/
    [DEBUG] Adding project dependency artifact: joda-time to classpath
    [DEBUG] Adding project dependency artifact: jt400 to classpath
    [DEBUG] Adding project dependency artifact: junit to classpath
    [DEBUG] Adding project dependency artifact: hamcrest-core to classpath
    [DEBUG] Adding project dependency artifact: jackson-core to classpath
    [DEBUG] Adding project dependency artifact: jackson-databind to classpath
    [DEBUG] Adding project dependency artifact: jackson-annotations to classpath
    [DEBUG] Adding project dependency artifact: hamcrest-all to classpath
    [DEBUG] Adding project dependency artifact: log4j-api to classpath
    [DEBUG] Adding project dependency artifact: log4j-core to classpath
    [DEBUG] joining on thread Thread[test.java._tools.BuildTestEnvironment.main(),5,test.java._tools.BuildTestEnvironment]
    [WARNING] java.lang.ClassNotFoundException: test.java._tools.BuildTestEnvironment
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:281)
    at java.lang.Thread.run(Thread.java:724)
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2.602 s
    [INFO] Finished at: 2014-05-15T14:38:50-05:00
    [INFO] Final Memory: 12M/152M
    [INFO] ------------------------------------------------------------------------

    推荐答案

    尝试将BuildTestEnvironment.javasrc.test.java._tools移到src.main.java._tools 即您的课程将是src.main.java._tools.BuildTestEnvironment

    try moving BuildTestEnvironment.java from src.test.java._tools to src.main.java._tools i.e. your class will be src.main.java._tools.BuildTestEnvironment

    我尝试运行您提供的方案.它失败了,就像你说的,但是我将Java文件从测试中移到了主文件中……它是RAN:)

    i tried running the scenario you provided. it failed like you said, however i move the java file to main from test .... it RAN :)

    干杯:)

    这篇关于Maven在测试阶段之前运行类:exec-maven-plugin exec:java不执行类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆