The best practice of using IntelliJ IDEA to develop Java Web with Spring MVC, Gradle and Tomcat.


Hi guys, I'm a new Java Web Developer, and I'm so frustrated those days about working on Spring, Gradle and IDEA...

I simply want to write code in IDEA, use Gradle to solve dependencies and build (compile classes and generate a war file), click one button to compile (incrementally) compile all my codes and deploy them on the server, or on click to run all tests. I didn't work with Spring Boot because the embeded tomcat is outdated.

So I'm wondering the best practice to organize the project.

  1. I created a Gradle Project, because I can add Spring MVC facet in the module settings while I don't know how to add Gradle support after created the project.
  2. I opened the module setting, add `Web` and `Spring` then `Spring MVC` (The Spring MVC won't appear unless I select Spring first) facet. Later I received an alert that I should add all of them into `Artifacts`. I clicked fix all (seems all libraries are added to the generated layout) and apply, then the IDEA told my it can't resolve symbols of imported libraries, the External Libraries tab is empty now. I can't make the project (this is another problem) though I can run `./gradlew bulid` successfully.
  3. I re-ran `./gradlew idea` from the command line again, and those jar file appeared in the External Libraries again. I didn't add the lib directory in the project to the module settings' Library tab. I can `Make` the project now. But the Make generate those classes in `out` directory, while Gradle Build generate the classes in the `build` directory.
  4. In the `Run/Debug Configuration` dialog, I added an Tomcat Server, but the `before launch` has only two option: Make, Build artifact. The Tomcat Server runs successfully.

In fact, what I want is that

  1. The IDEA can resolve the Gradle dependencies automatically, and resolve symbols.
  2. I click run server, it will calls Gradle build (incrementally build).
  3. Deploy those classes into the Tomcat Server by Hot swapping.

Or may I ask how you do those things in your workspace? I'm hunting for a convenient best practice.

Thanks, sincerely.


Comment actions Permalink

For hotswap deployment you need to use IntelliJ IDEA incremental Make and exploded artifacts, see

In a properly configured Gradle project all the dependencies should be resolved automatically after you import it in IDEA from build.gradle file. Running "gradle idea" to get the project files generated may produce different results.

If you have a problem with the dependencies resolving after importing the project, please file a bug with the sample project attached.

Comment actions Permalink

So should I use `gradle build` within IDEA to build the project? Or wait for the IDE to resolve the dependencies and use native Make is a better solution?


Comment actions Permalink

Using native Make is faster.

Comment actions Permalink

Thanks, that sounds also a good way. Gradle will only be used for dependencies and packaging


Comment actions Permalink

H Baranov,

I still feel hard to use them together. I've got them works fine finally by randomly clicked something. There is a `lib` folder in the root and all jar files there, I don't where I imported, but there are they.

Then I added another dependency in the build.gradle, will IDEA download it and add it to the project automatically? It seems not, because I can't got a hint when I try to use it within either XML or Java Source Code.

Any idea?

Thank you.

Comment actions Permalink

After you add a dependency to build.gradle, it's either downloaded automatically (if gradle auto sync in enabled), or you need to press Refresh button in the Gradle tool window so that IDEA updates the dependencies.

Comment actions Permalink

Yes I did it.

After several tries, I found that the project build successfully after I executed 'gradle idea', and all depended jar will appeared in the Module Setting as a module. After I pressed refresh, everything disappeared and only Spring MVC libraries there under a `Spring` tab. But IDEA can't resolve it.

So after I pressed refresh, I can't build the project.

Could you please show me how to create a project in the correct way with Spring MVC and Gradle supported? In text with steps or a screencast.

Thank you a lot.

Comment actions Permalink

Follow this guide: .

Download and unpack project.

Open build.gradle in IDEA:

Setup Spring framework when suggested:

Create Tomcat run/debug configuration as follows that will deploy the exploded war artifact under /spring4 context:

After starting this configuration you will see the app in the browser:

And the request mapping test:

Comment actions Permalink

Thanks. It works perfect now.


Please sign in to leave a comment.