Gradle eclipse task doesn't add proper gradle nature - java

I have the following project structure:
root-gradle (build.gradle)
project-group1 (no build file)
project1 (build.gradle)
project2 (build.gradle)
...
project-group2 (no build file)
...
So happens that I have to recreate Eclipse projects often. I run command:
gradle cleanEclipse eclipse
After a number of runs "eclipse" task stops working as expected. Namely, it does not add gradle nature to the projects anymore and does not recognize
sourceCompatibility = 1.6
anymore attempting to build everything with 1.8 version of Java.
I added the following to the root build.gradle:
allprojects {
sourceCompatibility = 1.6
eclipse.project {
natures 'org.springsource.ide.eclipse.gradle.core.nature'
}
// more stuff here ...
}
This helped with the root project, but had no effect on any other project.
I added the same thing to subprojects with the same unsatisfactory result.
I have to say than even after the nature had been added to the root project and Gradle plugin options became available for the root again I still don't see the "G" icon.
So it looks like I have 2 problems with the gradle setup.
Disappearance of the gradle nature. After a few runs Eclipse stops recognizing gradle projects. I wouldn't even face the second problem if that worked properly.
Some problem with my gradle build files or projects layout as my settings don't seem to take effect on subprojects.
Missing "G" icon for the project with restored Gradle nature.

Using the Buildship: Eclipse Plug-ins for Gradle I had to use natures string org.eclipse.buildship.core.gradleprojectnature
For Example:
allprojects {
eclipse.project {
natures 'org.eclipse.buildship.core.gradleprojectnature'
}
}

Install Gradle plug-in for Eclipse
Then use Eclipse menu: File -> Import... to import the Gradle project.
It will add the Gradle Nature. And 2 extra Eclipse views, one for quickly running Gradle tasks. One for Gradle output of those executed tasks.
TIP: Before importing an existing Gradle project, create a New Gradle project, to see if the Gradle plug-in is working as expected.
New Gradle Project
Use the Eclispe menu: File -> New -> Other...
Select the wizard: Gradle -> Gradle Project
Enter the project name
Press the button Finish
This should set up a minimal Gradle project.

If there is an evaluation phase race condition, you can eliminate it by using the afterEvaluate qualifier. I would try:
allprojects {
afterEvaluate {
eclipse.project {
natures 'org.springsource.ide.eclipse.gradle.core.nature'
}
}
}

I think you must set gradle to your system environment.
Steps:
Install Gradle from gradle website.
Go to eclipse preferences set Home path in gradle EnIDE like C:\gradle-2.3
or whatever directory you have installed.
Also make sure to add path to your system environment variable.

in the settings.gradle >>
include 'Device', 'Secugen', 'Morpho', 'NIDVSADesktop'
here NIDVSADesktop is my root project.
in the root project gradle
dependencies {
compile project(':Secugen')
compile project(':Morpho')
}
here Secugen and Morpho are my 2 sub projects

Remove Gradle Nature close ecllips then go to project folder structure and run the command "./gradlew clean build -x test cleanEclipse eclipse" next open eclipse and just refresh the project

Related

remove and add project dependencies with gradle/intellij

I am a Java newbie here. I am thinking of a way to remove all the project dependencies.
For example in nodejs, we can simply remove the node_module folder and then do an npm install.
I am using Gradle and IntelliJ IDEA
How can I do the following:
Remove the dependencies from the project
Re-add the dependency again
Lastly where do I see all the dependencies along with its version number in IntelliJ
To remove dependencies from the project, simply go to your
build.gradle file and remove all the lines containing
"implementation", "complie" or anything similar.
However if you just want to remove them from cache and redownload
them simply enter this command in the terminal:
gradle clean build
or in the gradle menu which you can find in the IntelliJ Sidebar
press "Reload all gradle projects"
To see the version of the dependencies you can either use
"Dependencies" new feature of IntelliJ, or find them in the
build.gradle file.
more info here
You can Remove the already installed dependencies by using clean command
gradle clean build
The clean task is defined by the java plugin and it simply removes the buildDir folder, thus cleaning everything including leftovers from previous builds which are no longer relevant. Not doing so may result in an unclean build which may be broken due to build artifacts produced by previous builds.
As an example assume that your build contains several tests that were failed and you decided that these are obsolete thus needs to be removed. Without cleaning the test results (using cleanTest task) or the build entirely (by running the clean task) you'll get stuck with the failed tests results which will cause your build to fail. Similar side effects can happen also with resources/classes removed from the sources but remained in the build folder that was not cleaned.
This will remove the old dependencies and build them back together once again .
and about the dependencies , to check Without modules:
gradle dependencies
For android :
gradle app:dependencies
Note: Replace app with the project module name
Here is a possible solution , Another one would be to create a gradle task inside build.gradle:
subprojects {
task listAllDependencies(type: DependencyReportTask) {}
}
Then call the task with
gradle listAllDependencies
and one final simple solution is using Project report plugin
Add this to your build.gradle
apply plugin: 'project-report'
And Then generate a HTML report using:
gradle htmlDependencyReport
And here is IntelliJ IDEA specific way .
Hope i helped .

Convert Java Project to Gradle Project in Intellij

I have a very basic java project.
In Eclipse there is an option to convert a project to a maven project all you have to do is right click on the java project and click "Convert to Maven Project". So basically it creates a pom.xml file for you.
Does IntelliJ have a similar command to convert to Gradle? Searched around but it did not seem like it does.
The simple way to migrate from Maven to Gradle via Intellij IDEA is:
Install Gradle Build Tool from https://gradle.org/
Add Path to System Enviroments (like in Gradle instructions)
Open your Maven project in Intellij IDEA and then open "Terminal" tab.
Write gradle init. Wait until the building process ends and then save & close your project.
Reopen your project and click Auto-import, and wait while Gradle is running. Approximate time - 5 mins.
Close your project (you may want to commit first)
Select "New Project"
Select Gradle (and any other frameworks you need)
Enter the directory where the Idea project to be converted is and
click "Finish"
Idea should handle the rest, you may need to move your files into main/java (etc)
I don't think there's a simple way to do this in place.
Convert a regular project into a Gradle project
Open your project in IntelliJ IDEA.
Create a build.gradle file. Add the information you need.
example:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
}
As soon as you create a build.gradle file, IntelliJ IDEA recognizes the Gradle build script and displays a notification suggesting to load the project as Gradle. After you load the project, IntelliJ IDEA enables the Gradle tool window.
From: https://www.jetbrains.com/help/idea/gradle.html#convert_project_to_gradle

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

Getting Gradle dependencies in IntelliJ IDEA using Gradle build

Grade build, even from inside IntelliJ IDEA does not put the dependencies into the "External Libraries" folder, so these classes don't show up as suggestions in the editor and when I manually add them as an import there is a compile error.
How can I get IntelliJ to automatically incorporate the dependencies in my build.gradle file, for instance:
compile 'com.google.code.gson:gson:1.7.2
After adding dependencies open "Gradle" ('View'->Tool Windows->Gradle) tab and hit "refresh"
example of adding (compile 'io.reactivex:rxjava:1.1.0'):
If Idea still can not resolve dependency, hence it is possibly the dependency is not in mavenCentral() repository and you need add repository where this dependency located into repositories{}
You either need to import the project as a Gradle project from within Idea. When you add a dependency you need to open the Gradle window and perform a refresh.
Alternatively generate the project files from gradle with this:
build.gradle:
apply plugin: 'idea'
And then run:
$ gradle idea
If you modify the dependencies you will need to rerun the above again.
When importing an existing Gradle project (one with a build.gradle) into IntelliJ IDEA, when presented with the following screen, select Import from external model -> Gradle.
Optionally, select Auto Import on the next screen to automatically import new dependencies.
For those who are getting the "Unable to resolve dependencies" error:
Toggle "Offline Mode" off
('View'->Tool Windows->Gradle)
Andrey's above post is still valid for the latest version of Intellij as of 3rd Quarter of 2017. So use it. 'Cause, build project, and external command line gradle build, does NOT add it to the external dependencies in Intellij...crazy as that sounds it is true. Only difference now is that the UI looks different to the above, but still the same icon for updating is used. I am only putting an answer here, cause I cannot paste a snapshot of the new UI...I dont want any up votes per se. Andrey still gave the correct answer above:
Tried everything in this thread and nothing worked for me in IntelliJ 2020.2. This answer did the trick, but I had to set the correct path to the JDK and choose it in Gradle settings after that (as showed in figures bellow):
Setting the correct path for the Java SDK (under File->Project Structure):
In Gradle Window, click in "Gradle Settings..."
Select the correct SDK from (1) here:
After that, the option "Reload All Gradle Projects" downloaded all dependencies as expected.
Cheers.
I had this exact same error and nothing else worked. Finally, I did the following:
Close IntelliJ IDEA.
Delete .idea directory from my project folder.
Reopen IntelliJ and import the project again (as Gradle).
After the above, any new gradle dependency I added to build.gradle started appearing in External Dependencies section when I clicked the gradle refresh button.
In my case,I was getting error while refreshing gradle ('View'->Tool Windows->Gradle) tab and hit "refresh" and getting this error no such property gradleversion for class jetgradleplugin.
Had to install latest intellij compatible with gradle 5+
in my case, just re-import the jar file into correct directory .
File->project Structure->[Project setting -> Modules]

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