2015-06-09 72 views
1

我有big-project/中的gradle項目和big-project/lib/中的子項目。Gradle構建,然後忽略子項目

big-project/big.project.Main class I import big.project.lib.Utils,它在big-project/lib/子項目中定義。

當我嘗試運行gradle build,這是發生了什麼:

:lib: 
compileJava UP-TO-DATE 
:lib:processResources UP-TO-DATE 
:lib:classes UP-TO-DATE 
:lib:jar 
:compileJava 
big-project/src/main/java/big/project/Main.java:3: error: package big.project.lib not exist 
import big.project.lib.Utils; 
        ^
1 error 
:compileJava FAILED 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':compileJava'. 
> Compilation failed; see the compiler error output for details. 

* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 

BUILD FAILED 

Total time: 6.243 secs 

出於某種原因,gradle這個編譯子項目的代碼,然後決定忽略它。 我該如何解決這個問題?

這些都是我的gradle這個文件:

big-project/settings.gradle

include 'lib' 

big-project/build.gradle

evaluationDependsOnChildren() 

allprojects { 
    apply plugin: 'java' 

    sourceCompatibility = '1.8' 
    version = '1.0' 

    group = 'big.project' 
} 

apply plugin: 'war' 

dependencies { 
    compile project(':lib') 
} 

big-project/lib/build.gradle是空


我的確學習了Gradle手冊: Chapter 56. Multi-project Builds,但沒有任何用處。

+0

通常在多項目構建中,根項目不包含源代碼,僅用於協調構建。嘗試爲您的根源代碼添加另一個子項目。從技術上講,你的解決方案應該基於什麼「解耦項目」說明,但由於解耦最終將是強制性的,所以現在最好這樣做(這種轉換可能是你的問題的原因) –

+0

我試圖將該代碼移動到「大項目/ product',但我仍然得到相同的錯誤。我重訪了部分[56.9。解耦項目](https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:decoupled_projects)和[56.7。項目庫依賴關係](https://docs.gradle.org/current/userguide/multi_project_builds.html#javadependencies_2),但這並沒有幫助;無論我把'compile project(':lib')'放到'big-project/product/build.grade'還是放在'big-project/build.gradle'中。 (PS:我確實將'product'項目添加到'big-project/settings。gradle') – derabbink

+1

不幸的是,在沒有看到構建腳本的情況下,我所能做的就是指出一個示例[project](https://github.com/ben-manes/caffeine),瞭解多項目構建的外觀。 –

回答

1

TL; DR:我的big-project/lib/src文件夾設置不正確(源代碼位於錯誤的子目錄中)。如this answer所述,Java源需要位於src/main/java

感謝對這個問題的評論,我能夠在正確的地方尋找解決方案。正如所建議的,我看了一個正在運行的示例項目。在我使用Eclipse時,我基於flat-java-multiproject示例生成了一個新的Gradle項目。這使我能夠消除我的build.gradle文件作爲錯誤的來源。

接下來,我決定檢查lib/項目的build/工件,並發現.jar文件不包含任何.class文件。這解釋了我在問題中報告的編譯器錯誤。這導致我看到所有的.java來源是否在正確的位置,如上所述,他們不是。解決這個問題,修復了編譯器錯誤。

整個癥結在於,當我在eclipse中編寫代碼時,似乎找到了所有對lib項目類的引用。