Can't make gradle to import Lombok dependencies - java

According to its documentation,
A minimal build.gradle looks like this:
plugins {
id 'io.franzbecker.gradle-lombok' version '1.10'
id 'java'
}
repositories {
jcenter() // or Maven central, required for Lombok dependency
}
After applying the plugin, the Lombok annotations can be used directly
in any Java code
This is my build.gradle
group 'tests'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'io.franzbecker.gradle-lombok'
plugins {
id 'io.franzbecker.gradle-lombok' version '1.10'
id 'java'
}
lombok {
version = "1.16.4"
sha256 = "3ca225ce3917eac8bf4b7d2186845df4e70dcdede356dca8537b6d78a535c91e"
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter() // https://stackoverflow.com/questions/41319176/how-to-make-gradle-add-lombok-to-its-project-and-external-dependencies-libraries
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
When I run gradle build, it gives me
classpath
No dependencies
BUILD SUCCESSFUL
But the Lombok annotations are not available. I've also tried adding compileOnly 'org.projectlombok:lombok:1.16.18' under dependencies, with the same result. I understand that the plugin should have been applied. I'm using Intellij, but I don't think this has anything to do with the IDE, since the problem is not that the annotations are not recognized, but that they're not even available.
What am I missing?

Try the following in your build.gradle
apply plugin: 'java'
group = 'com.example'
version = '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.4'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Note that I'm using IntelliJ 2018.2 and have enabled annotations under
Preferences | Build, Execution, Deployment | Compiler | Annotation Processors.
If that does not work, then run ./gradlew clean build from the command prompt and see if there's a build error.
If you don't see any errors then lombok can be used as follows
package com.example.java8;
import lombok.Data;
#Data
public class Country {
private final String country_iso_code;
private final String name;
}

Related

how to exclude a folder from checkstyle of Gradle on Intellij

I am new for IntelliJ and Gradle, but I have enough experience on Java and Eclipse. I generated some java classes from wsdl file. I put them under src/main/resources with a folder name - "generatedsources".
I try to prevent checkstyle for this folder and its subfolders like src/main/resources/generatedsources/* with gradle on IntelliJ.
I tried some lines such;
task checkstyle(type: Checkstyle) {
source 'src'
// include '**/*.java'
// exclude '**/gen/**'
// exclude '**/R.java'
// exclude '**/BuildConfig.java'
exclude 'src/main/resources/generatedsources/**'
}
But I'm failed again.
build.gradle;
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'checkstyle'
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'
sourceCompatibility = 1.7
version = '1.0'
...
buildscript{
repositories{
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.10'
}
}
task checkstyle(type: Checkstyle) {
source 'src'
// include '**/*.java'
// exclude '**/gen/**'
// exclude '**/R.java'
// exclude '**/BuildConfig.java'
exclude 'src/main/resources/generatedsources/**'
}
EDIT - After recommendations(but still failed!):
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'checkstyle'
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'
sourceCompatibility = 1.7
version = '1.0'
description = """BLABLABLA_application"""
war.archiveName = "BLABLABLA.war"
configurations{
deployerJars
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.3'
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.3'
compile group: 'org.springframework', name: 'spring-core', version:'4.0.0.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version:'4.0.0.RELEASE'
compile 'log4j:log4j:1.2.17'
compile 'com.sun.xml.ws:jaxws-rt:2.2.8'
compile 'org.jvnet.jax-ws-commons.spring:jaxws-spring:1.9'
compile group: 'org.springframework', name: 'spring-webmvc', version:'4.0.0.RELEASE'
providedCompile group: 'javax.servlet', name: 'servlet-api', version:'2.5'
testCompile group: 'junit', name: 'junit', version: '4.11'
deployerJars "org.apache.maven.wagon:wagon-http:2.2"
}
jar {
manifest {
attributes 'Implementation-Title': 'BLABLABLA', 'Implementation-Version': version
}
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
buildscript{
repositories{
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.10'
}
}
wsdl2java{
wsdlsToGenerate = [
["$projectDir/src/main/resources/BLABLABLA.wsdl"]
]
generatedWsdlDir = file("$projectDir/src/gen/java")
wsdlDir = file("$projectDir/src/main/resources")
}
wsdl2javaExt {
cxfVersion = "2.5.1"
}
tasks.withType(Checkstyle) {
exclude '**/your/generated/package/goes/here**'
}
checkstyleMain.exclude '**/your/generated/package/goes/here**'
"exclude" in tasks.withType and "checkstyleMain" causes an error such as "cannot resolve symbol"!
When checkstyle plugin is applied custom tasks are delivered along with it (see here), so instead of adding custom task with type Checkstyle configure the shipped one. This is how it should be done:
tasks.withType(Checkstyle) {
exclude '**/your/generated/package/goes/here**'
}
You can also configure a particular task, not all:
checkstyleMain.exclude '**/your/generated/package/goes/here**'
Also this is not good practice to put generated sources under src/main/resources. Typically such files goes to e.g. src/gen/java
I followed some comments and make it work add this in file build.gradle
checkstyle {
config = rootProject.resources.text.fromFile("${project.projectDir}/checkstyle-8.34_google_checks.xml")
toolVersion = '8.34'
ignoreFailures = false
maxWarnings = 0
}
checkstyleMain
.exclude('com/examples/gen/api/*.java')
.exclude('com/examples/gen/model/*.java')
.exclude('com/examples/gen/auth/*.java')
You can only exclude a folder that is in the source set. That is something that had not been mentioned and it caused me to struggle since I am using Intellij. The generated files are in the build/ folder which is on the same level as src/. I tried excluding the build folder for a while but there was no effect. The only solution was to specify the source folder. Since the build folder is not under src (the source folder), it worked immediately.
checkstyleMain.source = "src/main/java"
I found the solution here: Specify excludes to Checkstyle task
I had lots of issues with checkStyle & pmd + QueryDSL
A final solution for me was not to exclude generated sources, but clean them after assemble task: no sources - nothing to analyze! :)
assemble {
finalizedBy cleanQuerydslSourcesDir
}
For kotlin dsl use
tasks.withType<Checkstyle>() {
exclude("**/com/generated/*.java")
}

Gradle + Java, multiproject build.gradle

This is my file hierarchy, 'Domain Module' has no code right now, basically a wrapper for DBController and Domain.
Domain Module
.gradle
.idea
build
DBController
build
src
main
java
interfaces
IDBController.java
DBController.java
res
some SQL files
test
java
some test files
build.gradle
Domain
.gradle
build
gradle
src
main
java
Server.java
build.gradle
gradlew
gradlew.bat
settings.gradle
gradle
build.gradle
gradlew
gradlew.bat
settings.gradle
This is my build.gradle in Domain Module/build.gradle
group 'Group'
version '1.0-SNAPSHOT'
apply plugin: 'java'
targetCompatibility = 1.8
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile project(':Domain')
compile project(':DBController')
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
this is build.gradle in DOmain Module/DBController/build.gradle
group 'Group'
version '1.0-SNAPSHOT'
apply plugin: 'java'
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.postgresql:postgresql:9.3-1103-jdbc3'
}
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
dependencies {
compile('com.googlecode.json-simple:json-simple:1.1.1')
compile files('libs/json-simple-1.1.1.jar')
compile('org.postgresql:postgresql:9.3-1103-jdbc3')
}
And finally, build.gradle in Domain Module/Domain/build.gradle
group 'Group'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
jcenter()
}
}
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenCentral()
jcenter()
}
sourceCompatibility = 8
targetCompatibility = 8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
dependencies {
compile project(':DBController')
}
My main method is in Server.java, and it uses an instance of DBController. How do i assign this file in my java manifest? I've tried the simple
jar {
manifest {
attributes 'Main-Class': 'Domain.src.main.java.Server'
}
but whenever i try to execute java -jar -the generated jar in Domain Module/build/libs-
i get an error telling me it can't find the main file, and as the build gradles are now it gives me an error saying there's no reference to a main class at all.
The gist of my project is that DBController issues queries against a SQL server, and that Server.java will be a spring server. I decided to use gradle to do this so i would learn, and while i have learned alot about gradle, there is still much uncertainty.
I just figured out what was wrong.
jar {
manifest {
attributes 'Main-Class': '-insert main class here-'
}
}
This attribute assumes you're in the src/main/java dir to begin with, so if my filepath was
src/main/java/robert/util/Class.java
i would just have to say
jar {
manifest {
attributes 'Main-Class': 'robert.util.Class'
}
}
Spent so much time solving such a trivial error. My tip for anyone else is to not overlook the 'intro to gradle' sites and such. The solution was there all along.

Adding dependencies to a custom gradle plugin

I'm creating a gradle plugin that uses gson, but when I use the plugin at my client it throws this java.lang.NoClassDefFoundError: com/google/gson/Gson
I expect I am linking my dependencies in the plugin in a wrong way, but i'm not quite sure so any help would be great.
The build.gradle in the plugin
group 'nl.daanluttik.gradle'
version '0.1'
apply plugin: 'java'
apply plugin: 'maven' // the plugin to distribute to maven
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '1.7.2'
compile gradleApi()/*The gradle plugin api*/
testCompile group: 'junit', name: 'junit', version: '4.11'
}
//To distribute to maven
uploadArchives {
repositories {
mavenLocal()
}
}
A segment of the buildgradle in the client project
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath group: 'nl.daanluttik.gradle', name: 'peach', version: '0.1'
}
}
Is this really the first error? I most often see NoClassDefFoundError (in contrast to ClassNotFoundException) if some static initializer threw some exception and because of that the class could not be loaded and is not available later on.
Your missing the pom file with your dependencies. If it's just java then you can easily use the maven-publish which will generate the pom for you correctly.
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
groupId 'nl.daanluttik.gradle'
artifactId 'peach'
version '0.1'
from components.java
}
}
}
Then you can publish to the repositories (default local only) with gradle publish
Reference: https://docs.gradle.org/current/userguide/publishing_maven.html

How can I add a generated source folder to my source path in Gradle and IntelliJ?

I use thrift and it generates some source java files(interfaces) under build directory (build/generated-sources/thrift/<package name>/<class>) but under my src/main/java I have my classes which has the same package definition as in the generated java files and my classes also implements the interfaces generated by the thrift so how can I configure this in my build.gradle so it works on intelliJ as well as the build
plugins {
id "org.jruyi.thrift" version "0.3.1"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: "org.jruyi.thrift"
group 'com.hello'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.apache.thrift', name: 'libthrift', version:'0.9.3'
compile 'com.datastax.cassandra:cassandra-driver-core:3.0.0'
compile 'com.datastax.cassandra:cassandra-driver-mapping:3.0.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
compileThrift {
thriftExecutable "/usr/local/hello/bin/thrift"
sourceDir "src/main/thrift"
createGenFolder false
}
task thrift(type: Exec) {
commandLine '/usr/local/hello/bin/thrift'
}
compileJava {
dependsOn 'compileThrift'
The gradle build should work automatically.
To make it work on Intellij, try adding the following to your build.gradle.
idea.module.sourceDirs += file("$buildDir/generated-sources/thrift")
Don't forget to refresh your gradle projects.

Is there any way of making IntelliJ IDEA recognizing Dagger 2 generated classes in a Java project?

Context
I have started a personal project in java with Gradle as the build system and I want to use Dagger 2 as a DI. The main reason of doing that is to get used to that library and be able to use it easily in bigger projects.
What have I tried
I've managed to make the Google sample runs on IntelliJ IDEA
Problem
IntelliJ IDEA keeps telling me that it cannot resolve the generated class (in this case DaggerCoffeeApp_Coffee). It's a bit annoying not to know if the written code is correct (specially when you are learning to use Dagger 2).
All java classes are the same as the Google sample. Here is my build.gradle file:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.0.1'
compile 'com.google.dagger:dagger-compiler:2.0.1'
}
Question
Is there any way to make IntelliJ IDEA recognize DaggerCoffeeApp_Coffee as a generated class (and so make it possible to go to its implementation by `ctrl + left click)?
Simplest way I found:
Add idea plugin and add Dagger2 dependency like below:
plugins {
id "net.ltgt.apt" version "0.10"
}
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.11'
apt 'com.google.dagger:dagger-compiler:2.11'
}
Turn on Annotation Processing for IntelliJ: Go to Settings and search for Annotation Processors, check Enable annotation processing like below image:
Finally I made it!
I had to add the apt and the idea plugin so right now my build.gradle file look like this:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "net.ltgt.gradle:gradle-apt-plugin:0.4"
}
}
apply plugin: "net.ltgt.apt"
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.0.1'
apt 'com.google.dagger:dagger-compiler:2.0.1'
}
you must manually enable the annotation processing in IntelliJ.
From: Settings --> Build, Execution, Deployment --> Compiler --> Annotation Processors --> Enable annotation processing and Obtain processors from project classpath
then rebuild the project and you will find the generated classes in the project.
Please note that I have used this solution in a (java) android project.
I'm using version 2017.3.3 of IntelliJ IDEA, version 0.14 of the net.ltgt.apt plugin and version 2.14.1 of Dagger and as well as applying the idea plugin in the build.gradle file (as in Pelocho's answer) I found I also had to tell IntelliJ where it can find the sources generated by Dagger, as follows:
apply plugin: 'idea'
idea {
module {
sourceDirs += file("$buildDir/generated/source/apt/main")
testSourceDirs += file("$buildDir/generated/source/apt/test")
}
}
This is what I had to do in order to get Idea to work with Dagger2 and gradle.
Turn on annotation processing as shown in the answers above.
Add the following to the build.gradle file in order for Idea to see the generated classes as sources.
sourceDirs += file("$projectDir/out/production/classes/generated/")
Here's the full listing of my build.gradle
plugins {
id 'java'
id 'idea'
id "net.ltgt.apt" version "0.10"
}
idea {
module {
sourceDirs += file("$projectDir/out/production/classes/generated/")
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.dagger:dagger:2.16'
apt 'com.google.dagger:dagger-compiler:2.16'
}
sourceCompatibility = 1.8
Also, I had to add the following gradle task (to my build.gradle file) to clear out my out directory. When I moved some files around and Dagger2 regenerated the source files, the out directory wasn't being cleared out :(. I also included this task in my run configuration, so that it gets triggered before I rebuild my project.
task clearOutFolder(type: Delete) {
delete 'out'
}
Here's the solution that worked for me:
File -> Project Structure -> (select your project under list of modules) -> Open 'Dependencies' tab
Then, click on green '+' sign, select 'JARs or directory' and select 'build/classes/main' folder.
Another solution would be to link folder with build class files using 'dependencies' block inside build.gradle:
https://stackoverflow.com/a/22769015/5761849
Using IntelliJ IDEA 2019.1 and Gradle 5.4.1, this seems to be enough:
plugins {
id 'java'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation 'com.google.dagger:dagger:2.23.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.23.1'
}
I don't know the minimal versions for which this solution works, though.
I had a similar problem, I could not find out the cause for a long time.
Just launched and the result surprised me.
Intellij Idea 2018.3.6 -
build.gradle:
plugins {
id "java"
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.11'
apt 'com.google.dagger:dagger-compiler:2.11'
}
The following worked for me on IntelliJ 2021.3.3 (UE)
plugins {
id 'java'
id 'idea'
id("com.github.johnrengelman.shadow") version "7.1.2"
}
idea {
module {
sourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/main")
testSourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/test")
}
}
group 'com.codigomorsa'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'com.google.dagger:dagger-compiler:2.44'
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'com.google.dagger:dagger:2.44'
testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.44'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}
test {
useJUnitPlatform()
}

Categories

Resources