How to make Gradle add Lombok to its Project and external dependencies libraries? - eclipse

I created a Java project in Eclipse Neon. I used Buildship 1.0.21 to import the project as a Gradle project and ran the wrapper and init commands to generate the build, settings and wrapper files.
The project has an empty source folder because I am trying to solve a similar problem on a more complicated project and taking the divide and conquer approach - just add Lombok dependency.
Here is my build.gradle as instructed on the lombok website:
apply plugin: 'java'
dependencies {
compileOnly "org.projectlombok:lombok:1.16.12"
}
and gradle-wrapper.properties if needed:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-bin.zip /* <--- tried other versions too */
and I ran the jar file to install it on my eclipse. If I add the jar manually via "Java Build Path" it works but I want Gradle to handle this for me. When I refresh the project nothing happens - there is no lombok jar under "Projects and External Dependencies" and code relying on lombok will give errors.
There is another project I have which does have it there and I don't know what I did differently but I know it's possible.

I managed to solve this after trial and error. The build.gradle file must add the jcenter repository:
repositories {
jcenter()
}
Why this is the case and why this is not mentioned anywhere I don't know. But for now this is what works.

Related

In Eclipse: Could not find or load main class org.testng.remote.RemoteTestNG

Configuration
I have version 2019-12 (4.14.0) of Eclipse (having recently upgraded from 2019-03) with the following plugins installed (amongst others):
TestNG 6.14.3.201902250526
TestNG M2E (Maven) Integration (Optional) 6.14.3.201902250526
TestNG P2 Features 6.14.3.r201802240500
Eclipse is running against jdk1.8.0_152.
In Eclipse, my Java Build Path includes testng-6.14.3. I've tried to match the Eclipse plugin version against the version using in the project.
Under Properties -> TestNG I have all the default settings. There is no setting here for "Use Project TestNG.jar".
What Works
When I run tests from the command line with Gradle they all pass with no issue.
There are no complile errors showing in Eclipse.
What Does Not Work
When I run a test from Eclipse (by right clicking on a class) the console shows:
Error: Could not find or load main class org.testng.remote.RemoteTestNG
It appears I need a copy of testng-remote.jar. Should this have been installed as part of the plug-in, or am I meant to have included that in my build.gradle as a dependency?
Why can't I run tests from Eclipse? Do I need to install something further to resolve this error?
What I have Tried
I have tried removing and re-installing the TestNG plugin.
It seems you hit the bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=558495 in Windows.
I reverted my version back to 2019-09 because of it.
The bug causes jars which should be on the classpath not to be on the classpath. This affects projects with long classpaths.
Can you try adding it in your build.gradle.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
classpath group: 'org.testng', name: 'testng', version: '6.8.+'
} }
Change the testng version as per your usage.
I don't remember such problem for gradle but it looks similar to the problem with testing on maven(with auto-build turned off). The soultion is to rebuild project (for maven it ia alt +f5) and because of such rebuilding - cache is updated.
You have detailed TestNG gradle tutorial
By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. When we specify useTestNG(), Gradle scans for all the methods annotated with #Test and execute them.
Example 1. Below is the basic configuration for the 'test' task
plugins {
id 'java'
}
group 'com.test'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.testng', name: 'testng', version: '6.14.3'
}
test {
useTestNG()
}
For Gradle use testCompile dependencies:
repositories {
jcenter()
}
dependencies {
testCompile 'org.testng:testng:6.10'
}
For specific version you have options,
Install from update site
Select Help / Install New Software...
Enter the update site URL in "Work with:" field:
Update site for release: http://dl.bintray.com/testng-team/testng-eclipse-release/.
Make sure the check box next to URL is checked and click Next.
Eclipse will then guide you through the process.
You can also install older versions of the plug-ins here. Note that the URL's on this page are update sites as well, not direct download links.
Build TestNG from source code
you can download the source and build the distribution yourself:
git clone git://github.com/cbeust/testng.git
cd testng
./build-with-gradle

Gradle other project as dependency in eclipse

I have a web application which depends on another standalone project. Simply the web project requires a standalone project jar to be in classpath. I have built the standalone project jar with gradle and included that in web application's WEB-INF/lib folder. The project is running as expected. Now i want to make it automatic by adding that project as dependency. This is also achieved using the following code.
settings.gralde
include 'job-invoker'
project(':job-invoker').projectDir = new File(settingsDir, '../job-invoker')
build.gradle
dependencies {
compile project(':job-invoker')
.
.
}
I'm able to build the war file from command line using gradle and run it in tomcat. But i'm getting errors in eclipse. I'm not able to run the project in eclipse due to the compilation errors. Can some one please help me. Thanks in advance.
Finally i found a solution for this by installing the other project in maven local repository and adding this as a regular dependency in project. Reference code is given below.
Other project Gradle file
apply plugin: 'maven'
group = 'com.xxx.job'
version = '1.0'
Run gradle install command on this project. Then add mavenLocal() to your repositories in another project and add the dependency
compile 'com.xxx.job:job-invoker:1.0'

importing dependencies into eclipse using gradle

I've got what appears to be an adequate gradle file but eclipse refuses to recognise the dependancy I'm trying to import (despite refreshing the gradle project after implementing the dependancy)
My gradle file lacks a buildscript{} block but I'm not familar enough with gradle to implement it. My build file was autogenerated by eclipse so I don't see why it'd be an issue.
I'm trying to import "com.intrinio:sdk:0.0.1"
build.gradle:
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
compile "com.intrinio:sdk:0.0.1"
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:21.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
I think this is the lib you want to use.
As it's not deployed in a public repository, you have to manually install it. Checkout the project from github and do as below :
Installation
To install the Intrinio Java SDK to your local Maven repository, simply execute:
mvn install
Then, in your gradle file, add mavenLocal() in the repository section and it should just work fine.

Gradle dependency sources not appearing in Eclipse for the War Plugin

I am working on a Java web project that uses Gradle (version 2.1) as the build dependency tool. I use Eclipse Luna as my IDE. My OS is Mac 10.9 (Mavericks).
This is my build.gradle file (very basic and stripped down for ease of illustration):
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
sourceCompatibility = 1.8
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.google.inject', name: 'guice', version: '3.0'
}
So just one dependency, Guice. I will generate my Eclipse classpath and project files using gradle cleanEclipse eclipse. Then I will import an existing project into my workspace. I like being able to view the source code of my dependencies in my Eclipse projects, so I will open a Guice class, such as com.google.inject.Injector, using CMD-SHIFT-T. And voila, the source code of that file will appear.
But I working on a web project, so I need to be able to build a WAR file instead of a JAR file. Therefore, I am going to apply the Gradle War Plugin by replacing apply plugin: 'java' with apply plugin: 'war'. Then I rerun gradle cleanEclipse eclipse and reopen my project.
Now, instead of seeing the source code when I open up Injector, I will see the bytecode viewer with the Attach Source button. Anyone have any ideas whether it's something I'm forgetting in my build.gradle file or if it could be a bug in Gradle/Eclipse?
Note that I am not using the Gradle Integration for Eclipse Plugin because I wish to pinpoint the root cause of this issue without adding an extra layer of complexity to it. I have also checked other SOF questions about Attach Source issue with Gradle and Eclispe, such as how to tell gradle to download all the source jars and Why is Eclipse not attaching 3rd party libs source files to a WTP-faceted Gradle project?, but to no avail.

How can eclipse find 'javax.servlet' when using the gradle with plugins 'war' and 'jetty'

This must be very basic. But I just don't know how to make it work.
I'm following the book "Gradle in Action". When making the webapp the following is put in the build.gradle file
dependencies {
providedCompile 'javax.servlet:servlet-api:2.5',
'javax.servlet.jsp:jsp-api:2.1'
runtime 'javax.servlet:jstl:1.1.2',
'taglibs:standard:1.1.2'
}
I'm using eclipse. But it seems eclipse can't find javax.servlet. In cmd, I'm still able to run gradle jettyRun. Am I missing something? Thanks ahead!
apply plugin: 'eclipse-wtp'
is probably missing in your build.gradle. I suppose you have WTP (Web Tools Platform) plugins installed into your Eclipse.