我有两个 Maven 模块。一个命名tests
一个ws-ejb
。
在tests
pom.xml 中,我将 设置ws-ejb
为依赖项,因此我可以在测试中使用 EJB。
这是我的tests
maven 模块的 pom.xml 的缩短版本:
<parent>
<artifactId>myproj</artifactId>
<groupId>myproj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>tests</artifactId>
<dependencies>
<dependency>
<groupId>myproj</groupId>
<artifactId>ws-ejb</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
<type>war</type>
<dependencies>
<dependency>
...
<!-- other dependencies in the file: junit and javax.ejb -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>tests</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<plugin>
<plugins>
<build>
但是当我运行测试时,我收到一个编译错误,指出找不到我的 bean,但我将它作为依赖项,并且我的 IDE 没有抱怨丢失的 bean,但 maven 会:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:testCompile (default-testCompile) on project tests: Compilation failure: Compilation failure:
[ERROR] /home/r/projects/myproj/trunk/tests/src/test/java/com/myproj/MyTest.java [3,19]package com.myproj.beans does not exist
该包com.myproj.beans
确实存在于ws-ejb
我在模块中设置为依赖项的 maven 模块中tests
。那么有什么问题呢?
编辑
这是位于tests
maven 模块下的 MyTest.javasrc/test/java/com/myproj/MyTest.java
package com.myproj;
import com.myproj.beans.MyBean; // compilation error here. If I remove this line it works and the test is run!
//MyBean is located at `ws-ejb` maven module under src/main/java/com.myproj.beans
import javax.ejb.EJB;
import org.junit.Test;
public class MyTest {
@Test
public void test() {System.out.println("Print something...");}
}