This is about how to start a Java application project properly with Gradle and Git and IntelliJ IDEA.
You should have Gradle on your path or know its path.
If Gradle is not already on your path and you are on Windows I suggest Chocolatey to get it on your path.
There are separate videos about that.
I am using Gradle 6.6.1.
Make a directory for the project.
This can be in the terminal like here with Commander or cmd.exe
or it can be in a file manager like Windows Explorer or XYPlorer.
The directory should be meaningful
because Gradle will use it by default for the Gradle project name and Java package name.
cd into the directory and git init
git should be on your path. If its not, I again recommend Chocolatey to get it on your path.
The version of git doesn't matter.
git init makes a local repository within your project directory
so project changes will be stored as you commit.
Nothing we have done so far is Java-specific. These steps would apply to any kind of development project.
Now do a gradle init.
This will do 3 main things:
1. it will setup the Gradle files required for a build. More on that in a sec.
2. it will create sample Java code - both main code and unit tests.
3. it will create git files in your working directory that most projects would want
First it asks the type of project.
If you are creating code to run -- with a main method -- you want the application.
The other likely choice is library -- when you are writing code that will be used as a part of another project.
Next is what language do you want to code in. For this example we will use Java. It is the most common.
Next is the build script language.
We'll choose Groovy for this example.
Groovy is still more widely used and definitely more documented, so it is a good choice as a beginner.
It is possible to switch later.
Next is the testing framework for unit tests
since gradle init will generate sample unit tests.
At the time of this recording, I think JUnit4 is used about 80% of the time.
There is a ton of documentation on JUnit4, so it is a good choice.
But I'd recommend JUnit5 because the documentation is still very good
and JUnit5 is a better choice for Java 8 and beyond.
Now the project name and source package name.
This is why you should choose a good name for the project directory.
If you did, then the recommended defaults here are good recommendations.
Here is what our project directory looks like now.
We'll look at these files in IDEA now.
Open the project in IntelliJ IDEA.
In the open dialog either select the directory or the build.gradle within your project directory.
The project is ready, but I clean it up a little based mostly on style.
First, I delete settings.gradle because it is redundant in this setup.
You can always bring it back for multiproject builds
or if you need the Gradle project name to be different than the directory name.
For now, it is noise, so delete it.
Now delete the comments from build.gradle.
The comments are explanatory if you are first learning Gradle, so go ahead and read them.
But they are not appropriate to clutter up code.
Same kind of thing for the comments in .gitignore.
While in the .gitignore, add .idea.
.idea is user-specific and IDE-specific --- both of which you never want to commit to a git repository.
If someone else clones your repo with IDEA, these files will be regenerated specific for that developer.
Same kind of thing for the comments in .gitattributes
Now lets make sure all the code compiles - both the main code and the unit test code.
Run the Gradle task testClasses.
I used "Run Anything" here which is Ctrl + Shift + A on Windows.
You can also double-click testClasses from the Gradle view on the right-edge.
Now lets run all the unit tests. Remember this sample unit test code was generated by the gradle init task.
It is a good idea to run all the unit tests before committing any code.
The project is in good shape. It's time to commit.
The top 3 directories (.gradle, .idea, and build) will be ignored by git.
All of the rest of the files should be committed, but we haven't git added them yet so let's do that.
I just right-clicked on the highlighted files.
Now commit to the local repository.
Write a good message
so the person reading it can understand the state of the project
or the change made by reading this message.
Click Commit.