使用Gradle和Spring引用Java引用Groovy导致“无法找到符号” [英] Referencing Groovy from Java with Gradle and Spring Boot results in"cannot find symbol"
问题描述
DummyStorageRepository
和接口 StorageRepository
class DummyStorageRepository implements StorageRepository {...
}
现在在我的Application.java中,这也是一个spring启动配置文件,我有
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application实现AsyncConfigurer {
...
@Bean
public StorageRepository storageRepository(){
return new DummyStorageRepository();
}
我的Gradle.buid文件是普通的香草。
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
依赖关系{
classpath'org.springframework.boot:spring-boot-gradle-plugin:1.1.8.RELEASE'
classpath'org.springframework:springloaded:1.2.1.RELEASE'
}
}
应用插件:'groovy'
应用插件:'spring-boot'
应用插件:'project-report'
apply plugin:' eclipse'
apply plugin:'idea'
$ b targetCompatibility = 1.8
sourceCompatibility = 1.8
jar {
baseName ='service'
version ='0.0.1'
}
存储库{
mavenLocal()
mavenCentral()
}
依赖关系{
编译'org.codehaus.groovy:groovy-all:2.3.7:indy'
compile'org.springframework.boot:spring-boot-starter-执行器:1.1.8.RELEASE'
compile('org.springframework.boot:spring-boot-starter-web:1.1.8.RELEASE')
}
和我的文件结构是
src /main/groovy/com/app/repository/DummyStorageRepository.groovy,StorageRepository.groovy
src / main / java / com / app / Application.java
src / main / resources / ...
这是错误
/app/Application.java:103:error:can not find symbol
public StorageRepository storageRepository(){
^
symbol:class StorageRepository
location:class Application
如果我将Application.java转换为Application.groovy并将其移至groovy树,预期。如果我将DummyStorageRepository.groovy和StorageRepository.groovy转换为java并将其移动到java树中,情况也是如此。
我在gradle 2.1上,如果它有任何问题的话。
为什么我得到找不到符号的错误wehen引用了groovy从java?
更新:
手动添加了一个源设置到我的gradle.build,
sourceSets {
$ p $因此,这个答案的功劳应该真的去@tim_yates,但是在问题评论中给出的建议解决方案和理由是所有合法:
main {
groovy {
srcDirs = ['src / main / groovy']
}
java {
srcDirs = ['src / main / java']
}
}
测试{
groovy {
srcDirs = ['src / test / groovy','src / test / java']
}
java {
srcDirs = ['src / test / java']
}
}
}
快速解决方案:
将所有.java文件移动到
src / main / groovy
并编译groovy编译器的所有文件
推理:
为了在groovy文件中引用java文件和java代码中的groovy代码,你不能在
compileGroovy中的
,反之亦然,因此使用compileJava
compileGroovy
进行联合编译必须同时执行。
参考文献:
I have a Spring Component
DummyStorageRepository
and interfaceStorageRepository
written in Groovyclass DummyStorageRepository implements StorageRepository {... }
Now in my Application.java which is also a spring boot starter config I have
@Configuration @ComponentScan @EnableAutoConfiguration public class Application implements AsyncConfigurer { ... @Bean public StorageRepository storageRepository() { return new DummyStorageRepository(); }
My Gradle.buid file is plain vanilla.
buildscript { repositories { mavenLocal() mavenCentral() } dependencies { classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.1.8.RELEASE' classpath 'org.springframework:springloaded:1.2.1.RELEASE' } } apply plugin: 'groovy' apply plugin: 'spring-boot' apply plugin: 'project-report' apply plugin: 'eclipse' apply plugin: 'idea' targetCompatibility = 1.8 sourceCompatibility = 1.8 jar { baseName = 'service' version = '0.0.1' } repositories { mavenLocal() mavenCentral() } dependencies { compile 'org.codehaus.groovy:groovy-all:2.3.7:indy' compile 'org.springframework.boot:spring-boot-starter-actuator:1.1.8.RELEASE' compile('org.springframework.boot:spring-boot-starter-web:1.1.8.RELEASE') }
and my file structure is
src/main/groovy/com/app/repository/DummyStorageRepository.groovy, StorageRepository.groovy src/main/java/com/app/Application.java src/main/resources/...
This is the error
/app/Application.java:103: error: cannot find symbol public StorageRepository storageRepository() { ^ symbol: class StorageRepository location: class Application
If I convert Application.java to Application.groovy and move it to the groovy tree everything is compiled as expected. The same applies if I convert DummyStorageRepository.groovy and StorageRepository.groovy to java and move it into the java tree. I am on gradle 2.1 if it of any matter.
Why to I get "cannot find symbol" error wehen referencing groovy from java?
Update:
I added manually a source set to my gradle.build, just to see what paths are scanned but the result stays the same.
sourceSets { main { groovy { srcDirs = ['src/main/groovy'] } java { srcDirs = ['src/main/java'] } } test { groovy { srcDirs = ['src/test/groovy','src/test/java'] } java { srcDirs = ['src/test/java'] } } }
解决方案So the credit for this answer should really go to @tim_yates, but the suggested solution and reasons given in the question's comments are all legitimate:
Quick Solution:
Move all of your .java files under
src/main/groovy
and compile all files w/ the groovy compilerReasoning:
In order to reference groovy code in java files AND java code in groovy files, you can't have a task dependency on
compileJava
incompileGroovy
, or vice versa, so joint compilation usingcompileGroovy
has to do both.References:
Gradle User Guide - Groovy Plugin
Gradle compileGroovy dependency graph
这篇关于使用Gradle和Spring引用Java引用Groovy导致“无法找到符号”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!