Intellij Idea Test Module互相依賴如何處理編譯錯誤

Intellij Idea, Test Module Dependency, 2015-11-22

今天在搭建CAS Server的時候,cas-server-support-jdbc作為其中一個module,test目錄下的QueryDatabaseAuthenticationHandlerTests 依賴 cas-server-core test下的TestUtils, 而cas-server-core 包內(nèi)的test又不會導(dǎo)出到j(luò)ar包,除非在cas-server-core 的Sources中將test目錄“Mak as Sources”而不是“Test”,但這樣就會把Junit導(dǎo)出到Jar包了,不妥,應(yīng)該如何操作最好呢?

簡單說:

你有 Module-AModule-B.
Module-B test目錄下有一個Junit Test依賴了來自Module-A test目錄下的TestUtils,出現(xiàn)編譯錯誤,怎么辦?


jdbc.JPG

core.JPG

Test Source Folders.JPG

方案一(更新了方案二啦)

  1. 打開 Project Structure... (Ctrl+Alt+Shift+S - win/linux)
  2. 先選擇Modules中被依賴的cas-server-core 中的** Path** Tab頁,記錄下“Test output path”中的路徑并拷貝. 路徑大約是這樣:

D:\Dev\CAS\mm-cas\cas-server-core\target\test-classes

  1. 繼續(xù)選擇 cas-server-support-jdbc并查看Dependencies Tab頁。
    這里可能已經(jīng)有了Scope=comiple的對cas-server-core的依賴,點(diǎn)擊+ 或者快捷(Alt+Insert)添加“Libirary...”。

  2. Choose Libraries對話框中選擇New Library... 按鈕,繼續(xù)選擇 Java 選項(xiàng),在彈出的"Select Library Files"的對話框中,輸入剛才第二步中的路徑 D:\Dev\CAS\mm-cas\cas-server-core\target\test-classes, 點(diǎn)擊OK。現(xiàn)在這里就有了cas-server-core中的Test類,輸入一個合理的名字,在Level中選擇Module Library??梢栽冱c(diǎn)擊 + 添加一下源代碼,比如 D:\Dev\CAS\mm-cas\cas-server-core\src\test,這樣你也添加好源代碼文件了。。

    Library.JPG

    module lib.JPG

  3. 一路點(diǎn)擊OK回到Project Structure, 現(xiàn)在你就有一個名為cas-server-core-test-classes的module級別的library了, 將其Scope改為Test。(參照底部的說明)

  4. 點(diǎn)擊OK,Rebuild Project,大功告成!


    成功.JPG

方案二

1 在Module-A的POM.xml中添加BUILD,創(chuàng)建test-jar
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

這個插件就是為test classes打成一個jar包的

[>>點(diǎn)擊這里查看MAVEN官方鏈接](http://upload-images.jianshu.io/upload_images/285398-b4c06ff83dd971ba.JPG?imageMogr2/auto-orient/strip%7CimageView
https://maven.apache.org/plugins/maven-jar-plugin/test-jar-mojo.html)
jar:test-jar
Full name:
org.apache.maven.plugins:maven-jar-plugin:2.6:test-jar
Description:
Build a JAR of the test classes for the current project.
Attributes:
Requires a Maven project to be executed.
Requires dependency resolution of artifacts in scope:test
.
The goal is thread-safe and supports parallel builds.
Binds by default to the lifecycle phase:package

2 在module-B的POM文件中添加對這個test-jar的依賴

   <dependency>
        <groupId>cn.myproject.test</groupId>
        <artifactId>module-A</artifactId>
        <version>${project.version}</version>
        <type>test-jar</type>
        <scope>test</scope>
    </dependency>

3 reimport maven,完美解決!


以下為相關(guān)閱讀:##

https://www.jetbrains.com/idea/help/dependencies-tab.html#dependencyscope
Scope. This setting lets you control the classpath separately for your sources and test sources, and for the build and the run phases.
(The classpath may be different when

  1. your sources are compiled
  2. your test sources are compiled
  3. your compiled sources are run
  4. your tests are run.
    The Scope option defines the classpaths in which the dependency is to be included.)

Select the necessary option from the list:
Compile. The dependency is included in the classpath for your sources and test sources at the compilation and run phases.
Test. The dependency is included in the classpath only for your test sources at the compilation and run phases.
Runtime. The dependency is included in the classpath for your sources and test sources but only at the run phase.
Provided. For your sources, the dependency is included in the classpath only at the compilation phase. This is useful when there is a container (e.g. a web container of an application server) that provides the corresponding dependency at runtime.For your test sources, the dependency is included in the classpath both at the build and run phases.
Application server libraries, normally, are included in dependency lists with the scope Provided.

順帶再來一個說明:
If a module (module A) depends on another module (module B), IntelliJ IDEA assumes that:

- The sources of module A depend on the sources in module B and don't depend on its test sources.
- The test sources of module A depend on the sources and the test sources of module B.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容