How to make GitHub's Immutables work in IntelliJ + Gradle
Asked Answered
W

2

13

I use GitHub's Immutables library for Android development, and now I want to give it a try on the backend side as well.

In Android, all I need to to in order to use the library is this:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // immutable entities generation
    provided "org.immutables:value:2.5.5" // for annotations
    provided "org.immutables:builder:2.5.5" // for annotations
    provided "org.immutables:gson:2.5.5" // for annotations

    ... other dependencies
}

When I attempt to copy the above dependencies into build.gradle of my Java project, I get this error:

Error:(24, 0) Gradle DSL method not found: 'provided()'

I tried to replace provided with compileOnly and compile, but then the implementations of the interfaces annotated with @Value.Immutable are not generated.

How do I make it work?

Wivina answered 17/11, 2017 at 13:40 Comment(0)
W
13

Found the answer. Sharing in case it will be helpful to anyone (or myself in the future).

First of all, I had to enable annotation processing in IntelliJ as described here (though the option is now located in Settings > Build, Execution, Deployment > Compiler > Annotation Processors).

After that the following code started actually generating the implementation:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // immutable entities generation
    compile "org.immutables:value:2.5.5" // for annotations
    compile "org.immutables:builder:2.5.5" // for annotations
    compile "org.immutables:gson:2.5.5" // for annotations

    ... other dependencies
}

However, I still couldn't automatically import the implementation into source files.

In order to do allow the discovery of the generated classes, I had to right-click on the generated folder in the main package and then Mark Directory As > Generated Sources Root.

Wivina answered 17/11, 2017 at 14:4 Comment(1)
For newer versions of Gradle it is worth following https://mcmap.net/q/851329/-how-to-make-github-39-s-immutables-work-in-intellij-gradleBufordbug
A
17

I can't add a comment (too low rep), but for future readers, I want to extend Vasiliy's answer.

In my case, (gradle wrapper in version 5.2.1) following code auto-magically discovers generated sources:

dependencies {
    def immutablesVersion = "2.8.2"
    annotationProcessor "org.immutables:value:$immutablesVersion" // <--- this is important
    compileOnly "org.immutables:value:$immutablesVersion"
}

I don't need to change anything in IDE annotation processor options, it just works out of the box.

Aneroid answered 26/1, 2020 at 11:9 Comment(1)
As this is an extension to an existing answer that gives additional information to answer the question, its perfectly legitimate to post it as an answer. Even if you had the comment privilege, an answer would still be justified. Just as a general tip, if you directly reference another answer, you can link to it as I did by editing your post. I know this might not seem important if there is only one other answer, but more can follow in the future so its nice to have :)Sandor
W
13

Found the answer. Sharing in case it will be helpful to anyone (or myself in the future).

First of all, I had to enable annotation processing in IntelliJ as described here (though the option is now located in Settings > Build, Execution, Deployment > Compiler > Annotation Processors).

After that the following code started actually generating the implementation:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // immutable entities generation
    compile "org.immutables:value:2.5.5" // for annotations
    compile "org.immutables:builder:2.5.5" // for annotations
    compile "org.immutables:gson:2.5.5" // for annotations

    ... other dependencies
}

However, I still couldn't automatically import the implementation into source files.

In order to do allow the discovery of the generated classes, I had to right-click on the generated folder in the main package and then Mark Directory As > Generated Sources Root.

Wivina answered 17/11, 2017 at 14:4 Comment(1)
For newer versions of Gradle it is worth following https://mcmap.net/q/851329/-how-to-make-github-39-s-immutables-work-in-intellij-gradleBufordbug

© 2022 - 2024 — McMap. All rights reserved.