Issues with Dependency Libraries in Eclipse Using Gradle - java

I a newbie in Gradle and trying to find my way around it but facing some issues that needs to be resolved before i can proceed my Gradle tutorials.
At the moment i have compiled the app from the command prompt and seems to be working.
The issues i have now is that i can't see the dependencies (spring libraries) that Gradle has downloaded in Eclipse. If i look into the "Project and External Dependencies" folder in eclipse under the Gradle project i have created, i can't see the spring libraries there. For instance in Maven, the moment you add a dependency and save the POM file, the dependencies are added to the library instantly. Because of this issue red-error-markers appear in the source codes that are using the spring libraries though the application runs perfect from the command line.
How do i fix this problem? Does Gradle put the libraries somewhere?
My buld.gradle file is
apply plugin: 'java'
apply plugin: 'application'
mainClassName = System.getProperty("mainClass")
repositories {
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.14'
testCompile 'junit:junit:4.12'
compile 'org.springframework:spring-context:4.0.5.RELEASE'
}
thanks.
Extra question :: is there a way to let Gradle run in eclipse so that the output of my application is displayed in the eclipse console?

Related

IntelliJ IDEA using JavaScript "version" of dependency specified in Gradle build file?

This issue just recently (past couple days) started occurring on one of my development machines.
I'm using Eclipse's Vert.x dependency for a web project:
build.gradle
dependencies {
...
// Kotlin
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
// Vert.x for web framework
compile group: 'io.vertx', name: 'vertx-core', version: '3.4.2'
compile group: 'io.vertx', name: 'vertx-web', version: '3.4.2'
...
}
This has worked fine in the past - I think the triggering action was my upgrading to IntelliJ 2017.2.2, but now:
IntelliJ cannot resolve any of the -web imports:
If I examine the Dependencies list for my Module, the JavaScript version of the dependency is shown?
How did this happen, and how can I make sure it's properly recognized as a Java dependency?
Edit: Sample project available here: https://youtrack.jetbrains.com/issue/IDEA-177950
This is a bug in the Kotlin plugin which is fixed in version 1.1.4-2. After you update the plugin, you need to delete the incorrect libraries and reimport your project from Gradle to have your project fixed.
If you face such problems, the first two things you always can do is:
(in IntellJ) File > Invalidate Caches/Restart
(in IntellJ's Gradle Bar) Press button for Refresh all gradle dependencies
If this doesn't help, please check if ./gradlew clean testClasses succeeds or also fails with such an error.

how to create a java servlet with a gradle project using intellij IDEA

I want to create a simple java servlet in intellij IDEA.
I saw this page about how to do so,
But how can I make this web-project also a gradle project?
I want to evolve my servlet and add dependencies
I want to runt he servlet later and be able to debug it with breakpoints
First use IntelliJ to create a new Gradle Project. Second create a standard project structure for a webapp like:
src/main/java/yourPackage/yourServlet.java
src/main/webapp/WEB-INF/web.xml
src/main/webapp/index.html
Add the following to your gradle.build file:
apply plugin: 'jetty'
//also applies plugin: 'war'
//and this also applies plugin: 'java'
repositories{
mavenCentral()
}
dependencies {
compile 'javax.servlet:javax.servlet-api:3.1.0'
}
Now you can just build your project with the gradle (or gradlew if you use the wrapper) build task and run it with the jettyRun task. If you don't want to use jetty, you can use the war plugin without the jetty plugin and deploy your generated war file on every server you want. The war file will be located in projectRoot/build/libs
See also the user guide of gradle: Chapter 47. Web Application Quickstart

Gradle not downloading dependencies in Java EE

I am learning to create a java ee web application. In intellij idea I created a project using the project wizard: Java Enterprise -> Web Application. Then I created a build file.gradle in the project root, and call the gradle init, gradle build in the terminal. Here is the build file.gradle
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'
repositories {
jcenter()
mavenCentral()
}
sourceCompatibility = 1.5
dependencies {
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
testCompile 'junit:junit:4.12'
}
When I add a dependency and do gradle build, they are not loaded into the project. For example, the Gson library is not available in the code. How to tell Gradle to download the libraries so I could use them? What i'm doing wrong?
IntelliJ's Gradle integration does not automatically reflect changes you make in your build script. You will have manually initiate the synchronization between build script and IntelliJ project.
Your project is not backed by gradle. The easiest way for me to fix this is to :
close and re-open the project;
the bottom-right of the screen should display (x) Event log (the digit in parentheses being the number of events raised);
click on Event log;
a tooltip pops up, click on Import Gradle project;
a window pops up, configure gradle options.
When your run gradle build does it shows the dependency downloading, if so then the dependency has downloaded but not avaialable in intellij
Quick fix is to close the project
Remove the .idea folder and the .iml file
Next re-open your intellij
Click on File -> choose New -> Project From Existing Source
Select your project build.gradle
Allow Intellij to do the rest

Build dependencies with gradle, Android Studio, ADT

I am developing Android app with gradle project. My teammates use ADT, and I do Android Studio and I would like to try my best not to force limiting IDE for others.
As far as I know, ADT needs to have local dependencies (.jar or local lib).
Also if I have main project and local library which contains same local jar files, it cannot build with gradle due to "IllegalArgumentException: already added".
Settings below works fine with gradle and Android Studio but not for ADT, support v13 is not found.
Is there any way to make build works for gradle, Android Studio and ADT ?
Please let me know if you need more information.
My project setting looks like
settings.gradle
include: ':MyProj'
include: ':libs:PagerSlidingTabStrip'
build.gradle (MyProj's)
.....
dependencies {
compile 'com.android.support:support-v13:13.0.+'
compile project(':libs:PagerSlidingTabStrip')
}
.....
build.gradle (PagerSlidingTabStrip's)
.....
dependencies {
compile 'com.android.support:support-v13:13.0.+'
}
.....
You will have to do the following:
Make an Eclipse project for MyProject and libs/PagerSlidingTabStrip
In MyProj, create a libs folder and dump in it both support-v13 and support-v4.
Do NOT change build.gradle to dep on the content of libs/ or you'll have a duplicate class file issue.
This is manageable if you have only one lib project and few dependencies. If your setup become more complex it'll become impossible to manage (hopefully by then we have Gradle support in Eclipse)

configuring existing eclipse java project to build using gradle

I have an existing Java project in Eclipse. I want to implement builds using gradle. I tried using the gradle eclipse plugin as given here but ran into numerous errors in Eclipse.
I am using gradle 1.3, and I tried running gradle from command prompt, but I get compilation errors.
So my question is, does anyone know of some good resource which offers a how-to for converting an existing java project in Eclipse to compile using gradle. I also have some dependencies on other projects. The link to the tutorial I have given is not really helpful.
UPDATE:
I can get gradle to work if my project doesn't have dependencies on other projects. However, if it does refer to some other project, I can't figure out how to reference another project? I have added the referenced project dir to repositories but I still get "class does not exist" errors. My gradle file is as below:
apply plugin: 'java'
apply plugin: 'eclipse'
version='1.0-SNAPSHOT'
def repositoryPath = 'C:/Users/AMoody/workspace/temp/temp-protocols/'
repositories{
mavenCentral()
flatDir {
dirs repositoryPath
}
}
dependencies{
compile 'org.slf4j:slf4j-api:1.+'
compile 'junit:junit:4.+'
testCompile 'junit:junit:4.+'
}
Okay, So if you have simple java project which has dir/file structure as below
Step-1: create file name build.gradle in project root directory as below
Step-2: add following gradle script in build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'someJar'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
jar {
manifest {
attributes 'Main-Class': 'com.test.Run'
}
}
dependencies {
compile 'log4j:log4j:1.2.16'
}
Step-3: close or delete from eclipse this project and import it again while selecting Gradle project as below
Now your project hierarchy will look like this. (Also note its added Gradle Dependencies while importing project)
Step-4: Create source foler src/main/java in project and move your all packages in that source folder.
Step-5 last but not the least: Cheers :)
So now your Simple Java project has converted into Gradle project!
The first couple of chapters of the Gradle User Guide, along with the many (Java) samples in the full Gradle distribution, should get you started. It doesn't matter all that much whether your current build is Eclipse or something else; to be successful with Gradle, you'll first have to master its fundamentals.
Once you have the Gradle command line build working, you can give the IDE integration another shot. Depending on your preferences, you can either generate IDE files with Gradle's Eclipse plugin (again see the samples in the full Gradle distribution), or use the Eclipse Gradle Integration for a more integrated IDE experience.
Check out the Gradle homepage for additional resources. When faced with a concrete question about Gradle's build language, consult the Gradle DSL Reference. A full-text search over the single-page Gradle User Guide can also be quite effective. Last but not least, make sure to visit the Gradle Forums.
Try Eclipse integration for Gradle: http://marketplace.eclipse.org/content/buildship-gradle-integration

Categories

Resources