[英]How to use gradle. Java, and Groovy together?
我試圖在IntelliJ 13中使用Gradle項目,但我一直遇到以下問題:
我讀到groovy插件允許Groovy和Java在混合自己的源路徑中,但Java想要它自己的。 所以我有以下目錄結構:
我有Java和Groovy類的混合
這是我的build.gradle:
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'war'
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4")
}
}
jar {
baseName = 'my-app'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.0.0.RC4")
compile("org.springframework:spring-orm:4.0.0.RC1")
compile("org.hibernate:hibernate-entitymanager:4.2.1.Final")
compile("com.h2database:h2:1.3.172")
compile("joda-time:joda-time:2.3")
compile("org.thymeleaf:thymeleaf-spring4")
compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
compile ('org.codehaus.groovy:groovy-all:2.2.1')
testCompile('org.spockframework:spock-core:0.7-groovy-2.0') {
exclude group: 'org.codehaus.groovy', module: 'groovy-all'
}
testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
testCompile("junit:junit")
}
jacocoTestReport {
<!-- not sure this is right -->
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
這是我運行“ gradle clean build ”時出現的構建錯誤:
...src/main/java/com/product/service/FileDownloadService.java:24: cannot find symbol
symbol : class FileDownload
location: class com.product.service.FileDownloadService
private FileDownload fileDownload;
如果我創建Java,那么我不會得到任何編譯或執行錯誤。
嘗試追加文件“build.gradle”的下一行
sourceSets {
main {
java { srcDirs = [] } // no source dirs for the java compiler
groovy { srcDir "src" } // compile everything in src/ with groovy
}
}
請原諒我的英語不好。 我希望這可以幫助您解決問題。
如上所述,使用groovy插件進行編譯也將編譯java類。 我們必須確保不會在源代碼和groovy任務上觸發java編譯任務......
為此,並保留源文件夾 (例如:在eclipse中),您可以在build.gradle中使用以下改進的代碼段:
apply plugin: 'groovy'
//...
sourceSets {
main {
java { srcDirs = [] } // no source dirs for the java compiler
groovy { srcDirs = ["src/main/java", "src/main/groovy"] } // compile everything in src/ with groovy
}
}
如果你只是指定groovy { srcDir "src" }
,你的main/groovy
和main/java
文件夾在eclipse中被識別為包...
我的解決方案是將Java和Groovy類放在src / main / groovy和src / test / groovy (用於測試類)中。 從編譯器的角度來看,它會產生與其他答案中建議的sourceSets更改非常相似的內容。
從用戶的角度來看,我沒有看到將源文件保存在單獨的文件夾層次結構中的真正好處,因為它使得查找更難。 您也可以在Java和Groovy之間遷移類以選擇最佳實現。
主要的好處是它在Gradle的構建腳本中沒有任何配置,開箱即用。 所以它有助於保持簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.