[英]Eclipse Mars not finding classes
我在Eclipse中有一個Maven的多模塊項目,並且在導入某些類時遇到編譯問題。 我首先認為Maven配置存在問題,因為未找到的類來自測試包,但是后來我發現從同一模塊導入同一測試類的其他類也失敗了。 在代碼中,我的項目如下所示:
library
|-model
|-resource
|-int-test
|-... some other modules that doesn't matter
我發現有問題的第一堂課是:
// this class belongs to the test package (src/test/java) from the model module
// In my Eclipse project it's red underlined with the error message:
// The import com.library.app.commontests.author.AuthorForTestsRepository cannot be resolved
import static com.library.app.commontests.author.AuthorForTestsRepository.*;
...
// This class belongs to the int-test module (test package also)
public class AuthorResourceIntTest {...}
此類的情況相同
import static com.library.app.commontests.author.AuthorForTestsRepository.*;
...
// This class belongs to the resource module (test package)
public class AuthorResourceUTest {...}
Maven配置為打包測試類,因此我可以將其設置為其他模塊中的依賴項,這是配置:
<parent>
<groupId>com.library</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>model</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<!-- versionId setted in app/pom.xml in the pluginManagement section -->
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<parent>
<groupId>com.library</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>resource</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.library</groupId>
<artifactId>model</artifactId>
<!-- versionId setted in dependencyManagement section -->
</dependency>
<dependency>
<groupId>com.library</groupId>
<artifactId>model</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
就像我說的那樣,我首先想到的是在pom中配置錯誤,但是隨后我在同一model
包的類中發現了相同的錯誤,這是該類:
import static com.library.app.commontests.author.AuthorForTestsRepository.*;
...
// This class belongs to the same model module (also from test package)
// as AuthorForTestsRepository
public class AuthorRepositoryUTest {...}
在每種情況下,我在Eclipse中都有相同的錯誤。 AuthorForTestsRepository
類存在(我發誓),並且我的系統的其他類也有相同的錯誤(還有其他*ForTestRepository
和相應的*RepositoryUTest
, *ResourceUTest
和*ResourceIntTest
)。
這是Eclipse中的錯誤嗎? 我應該在哪里尋找解決方案。 預先感謝您的回答。
我從命令行嘗試了兩件事來測試它是否與Eclipse或Maven有關:
我從int-test
模塊運行了mvn test -PintegrationTests-wildfly
。 integrationTests-wildfly
是用於在Wildfly容器中運行集成測試的配置文件(默認情況下,我跳過了集成測試,因為它們花費的時間太長)。 容器啟動時沒有錯誤,當嘗試運行測試時,我收到以下錯誤消息:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 32.114 sec <<< FAILURE! - in com.library.app.author.resource.AuthorResourceIntTest
com.library.app.author.resource.AuthorResourceIntTest Time elapsed: 32.112 sec <<< ERROR!
java.lang.RuntimeException: Could not invoke deployment method: public static org.jboss.shrinkwrap.api.spec.WebArchive com.library.app.author.resource.AuthorResourceIntTest.createDeployment()
at com.library.app.author.resource.AuthorResourceIntTest.createDeployment(AuthorResourceIntTest.java:43)
但是我不知道這與從測試包中找不到的類有什么關系。
然后,我從主項目( app
)運行了mvn clean install -PintegrationTests-wildfly
。 model
和resource
模塊可以正確測試和編譯。 在int-test
模塊中,容器正確啟動,但是在測試數據庫中創建表時遇到問題。
我不明白的是,有些測試已經可以正常工作了,我沒有更改應該測試的代碼中的任何內容!
有一些代碼補全在Eclipse中不起作用,這是我在這篇文章中面臨的主要問題,所以我不想進入與該內容無關的項目細節。問題。
再次感謝您的回答。
最后,我將代碼移至另一個工作區,這解決了我的問題。 並非完全如此,而是從頭開始創建項目並僅移動文件( .java
, .xml
等)。 在2到3分鍾內,我必須right click -> Maven -> Update Project
來更新一些模塊。 感謝Markers View( 窗口-> Show View ),我意識到了這個選項。
不知道它是否有事可做,但是我以前的項目在我的Dropbox文件夾中,也許它與Maven有沖突,但是我不確定。
感謝您對我的問題的所有評論和看法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.