Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing Groovy from Java with Gradle and Spring Boot results in"cannot find symbol"

I have a Spring Component DummyStorageRepository and interface StorageRepository written in Groovy

class 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']
        }
    }
}
like image 438
Vad1mo Avatar asked Nov 06 '14 21:11

Vad1mo


Video Answer


1 Answers

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 compiler

Reasoning:

In order to reference groovy code in java files AND java code in groovy files, you can't have a task dependency on compileJava in compileGroovy, or vice versa, so joint compilation using compileGroovy has to do both.

References:

Gradle User Guide - Groovy Plugin

Gradle compileGroovy dependency graph

like image 138
Will Buck Avatar answered Nov 15 '22 01:11

Will Buck