In my last blog entry I walked you through the setup of a Java/Maven/Git development environment on OS X using Homebrew. In this entry I will walk you through a project using this new developer setup.

A Quick Spring Project

Let’s put our new tools to work by creating a starter application that will use Spring CLI, Maven, Git and Java.  This will just be a simple Spring Boot Java executable with a unit test that uses Maven to build.  We will add it to it’s own Git repository after.

Create Spring Boot Application

The following will create a new Spring Boot Java application with Web MVC enabled.  Then we will use Maven to run the default unit test, which tests if the Spring context loads.  Maven will compile the code before the test executes.

spring init -dweb demo
cd demo
mvn test

After the project downloads resources and compiles and runs the unit test you should see a ‘BUILD SUCCESS’ message.
Let’s create a quick little Spring MVC controller just so we can see the application running.  You’ll create a new Java file called HelloController.java in the location of src/main/java/com/example which should match the package structure created by the Spring init command.  Continuing on the command line in the ‘demo’ directory, just type the following.

pico src/main/java/com/example/HelloController.java

Now, enter the following content for our new HelloController.  This is certainly crude.  You wouldn’t want to implement Spring MVC controllers this way, but we want to cut to the chase and get something going without creating a bunch of files.

package com.example;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public void myHelloReponse(HttpServletResponse response) throws IOException {
        response.getWriter().print("Hello there!");
    }
}

Finally, go ahead and run the code.  With the Spring Boot Maven Plugin, compiling and running takes place with one command.

mvn spring-boot:run

OR, you can compile into a JAR and run using Maven directly.

mvn package
java -jar target/demo-0.0.1-SNAPSHOT.jar

Finally, go to this URL in the browser and see your hello message: http://localhost:8080/hello

Add to Git Repository

Go ahead and stop your application by hitting Control-C, then we’ll create a Git repository for our project and commit our changes.  As part of this we’ll add a ‘gitignore’ file to avoid committing the compiled resources.

# Add gitignore file -- we'll simply add the target directory
touch .gitignore
echo '/target' >> .gitignore

Next, create the Git repository, add all the files and perform the commit.

git init
git add .
git commit -m "initial commit"

There are further configuration steps that can be performed like changing author name and email to the Git configuration, the instructions for which are shown after commit.

Get to Work

You have no more excuses.  You’re ready to get some work done.  Now get to it.

Resources

Homebrew: http://brew.sh
Cask: http://caskroom.io