Guy Yafe
Guy Yafe

Reputation: 1021

IntelliJ Idea with Gradle: Best Practices

I am developing a Java application using IntelliJ Idea 14.1.4. If it would have been solely Java application, I would have known exactly how to structure the project in Idea: A single Java project, containing several modules: One for each part of the application (JAR).There will be at most 4-5 JARs.

The dependencies between the modules are also known: Protocol does not depend on anything, everything else depends on Infrastructure, and so on. Next, I would like to use Gradle scripting for managing the project. So my question is what is the best practice to structure the code in Idea?

Should I create a single Gradle Project and a Gradle module for each of the JARs?

Should I create a single Java project (or maybe empty project) and Gradle modules for each of the JARs?

Should I create a single Gradle project and each of the modules will be a Gradle's sub-project? Maybe it will be better to have an empty project and several Gradle modules because not all of the JARs are closely coupled?

Since I have never used Gradle before, I would appreciate any guidance for the best practices when combining both Gradle and Idea.

Thanks,

Guy

Upvotes: 1

Views: 1134

Answers (1)

Sebi
Sebi

Reputation: 8993

As long as possible, I would keep the code in one source repository. On the root, I would have an "empty" project not outputting anything. All your jar projects would be sub-projects (in Gradle terms). You include them via the settings.xml file located in the root project.

Each sub-project has its own build.gradle file. In those files, you can easily define the dependencies between your sub-projects, e.g.:

dependencies {
    compile project(':subProject3')
}

For convenience, I often create a special export task to put all artefacts in one export/ folder on the root level so that you don't need to go through all those sub-folders to get your stuff.

task export(type: Copy) {
    from project(':subProject1').jar
    from project(':subProject2').jar
    from project(':subProject3').war
    into 'export/'
}

IntelliJ Ultimate 14 works fine with this approach. You can simply hit Make to compile everything. You might also want to configure your project settings to run gradle jar or gradle export during a make if you prefer.

Upvotes: 2

Related Questions