remove and add project dependencies with gradle/intellij - java

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 .

Related

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

Installing Mockito using Gradle

I have installed Gradle by adding the path to it into the system variables. I am quite new to Java and this is the first time that I am trying to install an external library for it. On the Mockito web-page, they say that one can:
Declare a dependency on “mockito-core” library using your favorite
build system. With Gradle one can do:
repositories { jcenter() }
dependencies { testCompile "org.mockito:mockito-core:1.+" }
So I have no idea what it means. I changed the directory in cmd to the Gradle folder and tried to execute these commands, but that is not how one is supposed to do it. Can you give me a hand here?
You have to create a build.gradle file where you can insert the dependency. I recommend using an ide like eclipse or IntelliJ which can generate a gradle project for you so you don't have to do this manually. Just install the corresponding Gradle Plugin. This also makes sure you have a correct project structure.

Can't build plugin because of circular dependencies

My plugin consists of a collection of other plugins written by myself and these plugins have circular dependencies in their class paths (and I don't see a way to remove those because the plugins rely on each other).
The plugin runs just fine when testing after I have set eclipse to mark those dependencies as warnings and not as errors. However when I try to build my plugin via the site.xml or the export wizard it always tells me
A cycle was detected when generating the classpath
raven.sqdev.preferences_0.2.0, raven.sqdev.util_0.3.0,
raven.sqdev.preferences_0.2.0.
and quits the building process.
I already found the option to allow circular dependencies in the export wizard but the result stays the same.
Has anyone an idea how I can build my plugin with these circular dependencies?

Intellij IDEA and Gradle projects

I have a new project. Should I place apply plugin: 'idea' in build.gradle and run $ gradle idea? Or should I import the gradle project directly into IntelliJ IDEA 14.1? Which one will allow me to add dependencies to build.gradle and have IDEA automatically download & know about them?
With Intellij 14 you can just open the the build.gradle file using Intellij's File --> Open. this will import the gradle project, including all dependencies.
After you change something in the build.gradle file, you can click on "refresh all gradle projects" at the top of the gradle tool window.
You may also mark "use auto-import" under the Build Tools/Gradle tab in Settings. This will resolve all changes made to the gradle project automatically every time you refresh your project.
The idea plugin is the old method of importing a gradle project into Intellij.
With the newer versions of Intellij, it has become redundant.
From my experience using the idea plugin does not always work correctly in IntelliJ and actually IntelliJ documentation guidelines are to simply import build.gradle file.
Also, Peter Niederwieser who is a Principal Software Engineer at Gradleware answered a similar question ~2 years ago mentioning the following:
If you use Gradle's idea task to generate project files, this is normal, as there is no way to tell IDEA what the class path of the build script itself is. If you instead use IDEA's Gradle integration ("Import from Gradle model"), this problem doesn't exist.
Bottom line, your safer way to go would be importing gradle project directly from IntelliJ.
You can create a new Gradle Project in IntelliJ, and it will handle all of the dependencies and integrate well with Gradle. You can see here for more info and specifics.

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]

Categories

Resources