【问题标题】:My maven module cannot find class in my other maven module though I am setting it as dependency尽管我将其设置为依赖项,但我的 Maven 模块无法在我的其他 Maven 模块中找到类
【发布时间】:2015-07-09 07:34:05
【问题描述】:

我有两个 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 确实存在于我在tests 模块中设置为依赖项的maven 模块ws-ejb 中。那么有什么问题呢?

编辑

这是位于tests maven 模块中src/test/java/com/myproj/MyTest.java 下的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...");}
}

【问题讨论】:

  • 所以com.myproj.beans 来自那个战争?为什么一定是战争?不能是 JAR 吗?
  • 好吧,ws-ejb 是一个 WAR,所以我应该将它指定为一个 WAR。

标签: java maven maven-compiler-plugin maven-module


【解决方案1】:

由于您没有发布任何示例代码,我只能猜测。但我的猜测是您的测试代码不在项目的测试文件夹中。那么这是什么意思?这意味着 maven 只是尝试编译和/或运行您的测试项目,这会导致异常。尝试从您的依赖项中删除范围属性,然后重试。仅当您将测试文件夹与例如 JUnit 一起使用时,测试范围才会起作用。如果您的代码位于“主”文件夹中,则这将不起作用,因为在运行时无法访问依赖项。如果有不清楚的地方或者您有一些我没有得到的信息,请发表评论:)
亲切的问候
编辑:对此进行一些扩展:我强烈建议在作为测试的工件中为给定的工件编写测试代码。这样,如果测试失败,您将在有错误的工件中遇到 maven-build 失败。要了解它是如何工作的,您只需为特定类添加 JUnit 测试(大多数 IDE 都会支持您)
编辑 2:好的,我做了一个快速的 googlesearch:this article 可以提供有关如何使用这些类的答案。我希望这能解决问题:)

【讨论】:

  • 感谢您的回答。我在tests maven 模块中的测试代码位于测试文件夹中。看看我上面的编辑。我尝试删除范围属性,但出现同样的错误。
  • 谢谢!!测试 pom.xml 中的 &lt;classifier&gt;classes&lt;/classifier&gt; 和 ws-ejb pom.xml 中的 &lt;attachClasses&gt;true&lt;/attachClasses&gt; 是诀窍!
  • 真的很有趣!我当时也确实学到了一些东西,不知道你必须这样做!不客气:)
猜你喜欢
  • 2023-04-07
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
  • 2014-07-01
相关资源
最近更新 更多