Skip to content

Instantly share code, notes, and snippets.

@saylerb
Last active August 2, 2018 00:51
Show Gist options
  • Save saylerb/1614f5e274b8bd5bd1edc9422a2a3f1d to your computer and use it in GitHub Desktop.
Save saylerb/1614f5e274b8bd5bd1edc9422a2a3f1d to your computer and use it in GitHub Desktop.
Intellij CE setup a Gradle project

Install IntelliJ CE

  • Download IntelliJ Community Edition and copy to Applications folder
  • Open, don't import any settings.
  • You can skip the rest of the initial configuration process

Make sure you have Java Installed

  • Exercism has a pretty good page for instructions on installation http://exercism.io/languages/java/installation#mac-os-x
  • You don't need to install Gradle, since IntelliJ has a Gradle wrapper out of the box
  • I've been using Java 1.8, but I think 1.9 is the newest one (and I've heard they've finally added a REPL!)

Create a project

  • When you open up IntelliJ, there should be a dialog that is open.
  • Select Create a Project
  • The Left panel will show different types of projects. Select Gradel. Gradle is a build tool and dependency manager. Build scripts are written in Groovy, a dynamic langauge that feels like a mix of Ruby and Java.
  • Select your project SDK in the top drop down menu. The version of Java you installed should be there.
  • On the next screen, you'll see GroupID, ArtifactID, and Version. These are maven artifacts (Maven is another build tool that uses an XML configuration file) These aren't that important to us, but we have to fill them out.
    • GroupID - usually identifies the organization/team for a particular project. E.g. com.companyname.project. If it's a personal project, you can just use your name.
    • ArtifactID - the name to give to your jar without a version number. E.g. MyFirstProject.
    • Version - the version number. This would increment with each release if you were distributing the software. e.g. 1.0
  • On the next screen, make sure to check Create separate module per source set, and Use default gradle wrapper. You can use the Project JVM for Gradle JVM (the same one you specified earlier)
  • On the next screen select the location of the project to save
  • It will prompt you that the file is going to be created by IntelliJ, click yes.
  • Intially, your Gradle build will start syncing.

build.gradle

  • In the root of the project, you should see file called build.gradle that was generated for you. Gradle makes it really easy to build your project (compile your Java source files). It can also do many other things, including running your tests, and creating a JAR file containing your classes. If you're familiar with Ruby, this is like combination between a gem file and rake. The build.gradle file is written in Groovey, a dynamic language that feels like a mix of Java and Ruby.
  • If you step into the build.gradle, you'll see that it already has jUnit specified as a dependency, which is great.

Things to add to .gitignore

A simple gitignore for this project could be

.idea/
build
.gradle
!gradle-wrapper.jar
  • Ignoring the .idea/ directory is optional. JetBrains would probably reccomend publishing it, because it contains metadata about your IDE setup which can be useful for others working on the same team. In this case, I don't need to share IDE configuration, so I'm choosing to tell git to ignore it.
  • Ignore the build directory. This is where gradle will put the compiled Java classes. Since anyone who downloads your source code can build the project themselves, there's no need to publish this directory.
  • Ignore the .gradle directory.
  • Don't ignore the gradle-wrapper.jar. This can allow anyone to pull the project down and build without having to install gradle manually themselves.

Basic directory structure

By default, Gradle expects the following directory structure:

src
├── main
│   └── java
│       └── hello
│           └── Hello.java
└── test
    └── java
        └── hello
            └── HelloTest.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment