Eclipse and Git on ubuntu

How to Stage, UnStage and Commit a Java Maven Project to a Git Repo in Eclipse on Ubuntu

In this post we will revisit the basic concepts discussed in the previous post while introducing the implementation of those same features in an Integrated Development Environment (IDE).

In the last post which can be found here Git Fundamentals in Ubuntu and we discussed initializing a new repository with git init, adding project artifacts to the staging area with git add and committing project artifacts to the repository with git commit. This time we will use all these same basic git commands in the Graphical User Interface (GUI)of the Eclipse IDE. Although the examples and screenshots here will be in Eclipse the functionality is largely the same in any IDE that supports git

So, the first thing we’re going to do is create a new Maven project that we can add to a new git repository. We will then initialize the new repository using the repository view in Eclipse once we have initialized our new repository, we will add our Maven project to the repo and stage our project artifacts. Once our project files are staged, we will add a commit message and commit our project artifacts to the get repository so let’s dig into it.

First we will create a new Maven Project called StackApp.

In Eclipse, in the File Menu select File > New > Other.  This will bring up the new project wizard. In the new project wizard expand the Maven folder and select Maven Project then click OK.

In the new Maven project dialogue click Next. Since this is going to be a simple demo we are going to create a Simple Maven Project and skip archetype selection so check the checkbox then click next. In the Group ID text box type PDM then in the Artifact ID text box type StacApp. Once our Group ID and Artifact ID have been updated we can click Finish to create our Maven Project.

Now we’ve got our Stack App Maven Project in Eclipse, but our new Stack App doesn’t really have much of anything in it.  We’ll start with good programming practices by using Test Driven Development (TDD) starting with a Unit Test in a new Java JUnit Test Case.

In the Package Explorer right click on the src/test/java folder and select New > Other to bring up the “Select a wizard” dialog box.

In the Select a wizard dialog expand the Java/JUnit folder and select JUnit Test Case.

This will bring up the New JUnit Test Case dialog.  In the JUnit Test Case Dialog change the source folder from StackApp/src/test/java to StackApp/src/main/java, set the name to StackTest, check the setUp() checkbox and click Finish.

Now a JUnit prompt will come up asking us if we want to add the JUnit library to the build path. Accept the defaults and click.  Rename the provided sample test isEmptyTest.  In this test, we’re going to check for the existence of the stack class by creating a new instance of it in the variable called stack with a lower case S.  Using assertTrue we will verify that when we call the stack objects isEmpty method that it returns true.

At this point neither the Stack class nor the isEmpty method exist. As a result we get red squigglies on the Stack class itself.  We can get rid of those red squigglies by simply creating a class called Stack using the code generation tools. We just need to make sure that the class is created in the main folder rather than the test folder.

Hover your mouse over the Stack class reference with the red squigglies until the context menu appears (or right click and select quick actions) then select Create class ‘Stack’

Create Stack Class

In the New Java Class Dialog change the source folder from StackApp/src/test/java to StackApp/src/main/java and click Finish.

New Class D

Now that the Stack class has been created let’s go back to our StackTest and we should see that the red squigglies have been removed from the Stack class, but they’ve moved down to the reference to the isEmpty method on the stack object.

isEmpty Red Squigglies

Before the Stack class was created the system didn’t know that it had no isEmpty method definition.  Again, we’re going to use the code generation tools to resolve this issue by creating the isEmpty method.

Hover your mouse over the isEmpty method until the context menu appears then select Create method ‘isEmpty’ inside type ‘Stack’.  

This will create a new isEmpty method in the Stack class that returns false by default. In order for our test to pass, we have to return true. Before we do that, we run our test and see it fail.  Running the failing test ensures that we have a test and system under test that at least compiles and doesn’t throw any errors.

Stack-isEmpty Method

In the Package Explorer right click on StackTest.java and select Run As > JUnit Test from the context menu.  In the Save and Launch dialog click OK to save all files and run the tests.

In the Test Explorer we should see a red bar that indicates that our test has failed due to an assertion error.  The test is expecting true but false was returned.  We can simply change the return to true and our test will pass, but that wont solve the business problem.

isEmpty Test Passed

We’ll talk more about that in another post for now let’s just change the return to true and make our test passes.  Again, we’ll right click on StackTest and run it as a JUnit test.  Accept the prompt to save files. This time we get a green bar, which indicates that our test has passed.

Now that we have a semi functional Maven Project we can add it to a Git repository.

Add an existing repo

In the Git Repositories window we are going to Add an existing local Git repository to our Eclipse instance.  The repository we created in the last post should appear in the list as Stack Utility. Check the checkbox for our existing repo and click Add

Add Git Repositories Dialog

It should now appear in the Git Repositories window with the Working Tree that contains the Stack.java class that we created in the previous post. Now we can and add our StackApp maven project to the StackUtility Repository.

In the Package Explorer right click on the StackApp project and in the context menu select Team > Share Project. In the Share Project we can select the StackUtility Repo we just added by selecting it from the Repository dropdown then click Finish.

Share Project

Now that the project is associated with the Repo we can confirm in the terminal window by running Git status.  We should see untracked files .\StackApp (in red).

StackApp untracked bash

In Eclipse you should also notice the Unstaged Changes in the Git Staging window.  We can select all of the Unstaged files and drag them to Staged Changes in the Git Staging window.  This is the equivalent of running Git Add . in Git Bash

Unstaged Changes Eclipse

In the Git Bash terminal window run Git Status again to confirm that we have staged files that are waiting to be committed. If you recall, from the previous demo, we could commit those files by doing a Git Commit and adding a Commit Message in the Bash terminal (Git Commit -m “Commit Message”). We are going to the same thing in the Eclipse by typing our Commit Message into the Commit Message text box in the Git Staging window and pressing the Commit button

Commit Stack App Eclipse

Now the files have been committed, there is nothing in the Staged or Unstaged area. And if we go back to the terminal and run a Git Status, we can see that there is nothing left to commit in our working tree is.

This post was exactly the same as the last post only different!  The difference is we accomplished the same tasks use the Eclipse GUI instead of the Git Bash terminal.  In the next post we will go a little deeper into Git features like branching, cloning and pushing to remote GitHub repos.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: