<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=299788&amp;fmt=gif">

Up and Running with Homebrew - part 2

Development Process, Software Development, Spring, Git, Java

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;
public class HelloController {
public void myHelloReponse(HttpServletResponse response) throws IOException {
response.getWriter().print(&amp;amp;amp;quot;Hello there!&amp;amp;amp;quot;);
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' &amp;amp;amp;gt;&amp;amp;amp;gt; .gitignore
Next, create the Git repository, add all the files and perform the commit.
git init
git add .
git commit -m &amp;amp;amp;quot;initial commit&amp;amp;amp;quot;
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.


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

TAGS: Development Process, Software Development, Spring, Git, Java

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Subscribe to Our Newsletter

Recent Blog Posts