Java 我的maven模块在另一个maven模块中找不到类,尽管我将其设置为依赖项
我有两个maven模块。一个名为Java 我的maven模块在另一个maven模块中找不到类,尽管我将其设置为依赖项,java,maven,maven-compiler-plugin,maven-module,Java,Maven,Maven Compiler Plugin,Maven Module,我有两个maven模块。一个名为的测试和一个名为ws-ejb 在testspom.xml中,我将ws-ejb设置为一个依赖项,以便在测试中使用ejb。 这是mytestsmaven模块pom.xml的缩写版本: <parent> <artifactId>myproj</artifactId> <groupId>myproj</groupId> <version>1.0-SNAPSHOT</versio
的测试
和一个名为ws-ejb
在tests
pom.xml中,我将ws-ejb
设置为一个依赖项,以便在测试中使用ejb。这是my
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>
包com.myproj.beans
确实存在于maven模块ws-ejb
中,我在tests
模块中将其设置为依赖项。那怎么了
编辑
这是MyTest.java,位于src/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...");}
}
由于您没有发布任何示例代码,我只能猜测。但我猜您的测试代码不在项目的测试文件夹中。那么这意味着什么呢?这意味着maven只是尝试编译和/或运行您的测试项目,这会导致异常。请尝试从依赖项中删除scope属性,然后重试。仅当您将测试文件夹与例如JUnit一起使用时,测试范围才会工作。如果您的代码在“main”文件夹中,这将不起作用,因为在运行时无法访问依赖项。如果有不清楚的地方或您有一些我没有得到的信息,请评论:)
亲切的问候
编辑:对此进行一点扩展:我强烈建议在工件中为给定工件编写测试代码,并将其本身作为测试。这样,如果测试失败,那么在出现错误的工件中就会出现maven构建失败。要了解这是如何工作的,您可以为特定类添加JUnit测试(大多数IDE都支持这一点)
编辑2:好的,我做了一个快速的谷歌搜索:可以提供一个关于如何使用这些类的答案。我希望这能奏效:)那么
com.myproj.beans
来自那场战争吗?为什么一定是一场战争?它不能是一个JAR吗?嗯,ws-ejb
是一场战争,所以我想我应该把它指定为一场战争。谢谢你的回答。tests
maven模块中的测试代码位于test文件夹中。看看我上面的编辑。我试图删除scope属性,但出现了相同的错误。谢谢<测试pom.xml中的code>classes和ws-ejb pom.xml中的true
就是诀窍!真有趣!我当时也学到了一些东西,不知道你必须这样做!不客气:)
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...");}
}