Sunday, January 13, 2019

Gather all hammers inside toolbox - revisited

It's been a long time since I wrote something here. When I started this blog, one of the first thing I wrote about was how to set up development environment. It was ten years ago - see here. A lot of things changed in Java world in this time. Let's try to set up everything again using modern and most popular tools now.

What are we going to install? For today it will be:
  • Java JDK - we still have Java like in the past, however significant changes were done by Oracle last time.
  • IntelliJ Idea  - an IDE, as replacement for Eclipse
  • Apache Maven - dependency management
  • Git - version control system
As before, all things will be installed in C:\Development

===============================================
Installing Java JDK
===============================================

We all get used to that Java is free to use either for personal or commercial purpose. It was true for Java 8 and older versions until 1st January 2019. Starting from 1st Jan 2019 Oracle announced end of public updates of Java SE 8. It simply means If You use Java in commercial purpose and You want get updates for Your Java SE 8, from 1st January 2019 You have to pay for this.

Why Java 8, what about other versions like 9,10,11...? From Java 8, every three years Oracle will relese Java version known as Long Time Support (LTS). First LTS is Java 8, second is Java 11 - it means that Java 9 and 10 is no longer supported now. Changing licensing model for commercial use means that You have to pay for the updates/support for each LTS version starting from 8 (from 1st Jan 2019). See more here.

So what are options? We can:
1. Stay with latest available Java 8 from Oracle (JDK 8u192) without possibility to update anything - we don't have security updates, but we are free of charge.
2. Become Java Commerical User and pay for latest JDK (11 at the time of writing)
3. Use OpenJDK from Oracle - this is free Java for commercial use, but each new version is publicly available in 6-month interval, with 6-month support/updates available. So every half a year You have to update used OpenJDK on Your own.

Ok, let's install latest JDK 8 then.

Step 1: Download 64 bit version of JDK 8u192 or Windows from here.
Step 2: Create directory C:\Development\Java\OracleJDK\Java8_64bit and start the installer:


Remove installing public JRE and choose previously created directory:


Step 3: After succesfull install create system variable named JAVA_HOME with value C:\Development\Java\OracleJDK\Java8_64bit then modify Path system variable by adding %JAVA_HOME%\bin at the end.

Step 4: Verify Java installation by opening command line and type java -version and javac -version. You should see informationa about Java installed:


Java is installed.


===============================================
Installing IDE - IntelliJ Idea
===============================================


Step 1:
Create directory C:\Development\Jetbrains\Idea
Step 2: Download and install an application JetBrains Toolbox from here. This is an application to maintain all products from Jetbrains (IntelliJ Idea manufacturer). It allows to automatically update installed tools.


Step 3: Configure toolbox.

Choose created directory in step 1. Do not forget to  click apply after choosing it. If You have any license from Jetbrains, log in into your account now - You will not have to provide license when installing tools:


Now You can choose a tool to install:


After click on a tool, it will be installed into the previously selected directory:


Just click on tool to launch it. You can also change settings of the tool being installed, like update plan, memory consumption and so on:


IntelliJ Idea is installed. Start it, then go thru some creator in order to perform basic settings (You may leave all the options default).

===============================================
Hello World in Java - using IntelliJ Idea and installed JDK
===============================================

Step 1: Create directory C:\Development\Projects

Step 2: Run Idea. You will see initial screen for creating/opening/downloading a project. Choose create new project:


Step 2: Choose Java project type:


Idea is shipped with its own JDK. We would like to use our own JDK. Press button New ans select the JDK directory C:\Development\Java\OracleJDK\Java8_64bit. Then press Next.

Step 3: Choose project type from template: 

 
 Step 4: Choose project name and location (use projects directory created in step 1): 


Step 5: Sample application is ready. You can start is using green "play" button:



Step 6 (optional): Leave only our installed JDK for future projects. 

As mentioned above, Idea comes with built in JDK. We can disable showing this JDK as a option to select for future projects and leave only those we installed in C:\Development\Java\OracleJDK\Java8_64bit. In order to do it open project properties and find Platform SDK. Then choose JDK provided by Idea and delete it:


Then select our installed Java and change its name:


From now this is only one and thus default Java to use in Java projects. Of course You can add other JDK version like OpenJDK.

That's all. Idea is configured and ready for Java development.

===============================================
Installing Maven
===============================================

Maven will be used for dependency managment, building and running future apps. Idea comes with bundled Maven, so we do not have install our version. But similar to installing Java, we will install our own version of Maven.

Step 1: Create directory C:\Development\Maven. Then download Maven (binary zip archive) from here and unpack it to the created directory.

Step 2: Create directory C:\Development\Maven\repository for artifacts downloaded by Maven:



Step 3: Tell Maven to use created above directory as a repository. 

Locate Maven settings file named settings.xml and add a line inside:


Step 4: Add Maven's bin folder to the system PATH variable in order to use maven commands from console.

Step 5: Verify if Maven is installed correctly. 

Open command line and execute command: mvn -version. You should see similar information:


Note 1: Maven displays information about Java used. That's why You have to install Java before and set JAVA_HOME variable.

Note 2: There is no need to set up MAVEN_HOME or M2_HOME system variable like it was done for previous versins of Maven.

Step 6: Tell Idea to use installed Maven. Open settings, type Maven in search field. Idea will show settings for Maven. Choose Maven installation directory. You may notice that Idea will display recognized version of selected Maven installation and local respoitory path will change to the value set in step 3:


That's all. Maven is installed and configured in IntelliJ Idea.

===============================================
Installing Git
===============================================

Git will be used to track changes we make in our projects files. 

Step 1: Download Git for Windows from here
Step 2: Create directory C:\Development\Git. Run the downloaded git installer and choose created directory as the installation target: 


Install Git using all defaults, but choose Your favourite text editor and PATH modification:



This option will modify PATH system variable, allowing to execute git command from command line. 

Step 3: Verify installation and basic configuration. 

Open command prompt and execute command git -version to see if git responds. After that configure user and email: 


Step 4: Make IntelliJ aware of installed Git.

Open settings and type Git in search box. Go to Git settings and set path to git.exe:


That's all. Git is installed and configured in IntelliJ Idea.

===============================================
Make Java Hello World in control of Git
===============================================

Using git is beyond of the scope of this blog :) There are many great tutorials explaining the nature of git - for example this one. I will show how to add a git support to the existing project (Hello World created earlier).

Step 1: Convert existing projest into git respository. 

Open project location (C:\Development\Projects\Test) and using context menu (right mouse click) select option Git Bash Here. Git will open its terminal with path set to the project. Now enter command git init:


This will create empty repository:


The project is now maintained by Git.

Step 2: Verify git integration under IntelliJ Idea. 

Open Test project in Idea to see that it is recognized as a git project now:


Step 3 (optional): Use git command directly from IntelliJ Idea.

Idea has great integration with Git, however it is possible to use git commands entered by hand directly in Idea - just like we use them in Git Bash. We can use Idea's terminal and set it to open Git Bash under the hood. Go to the settings and find options for Termimal. Set shell path to point to the Git Bash executable script with some additional options:


Now we can execute git command in Idea's terminal:




That's all. Development environment is set up and ready for work :)