Building on the previous article in this series, we’ll use STS to create a simple Maven powered project and discuss the various components.
After installing the JDK and your new IDE, fire up STS to see a loading screen similar to the image on the right. As with all things Java, there’s a few seconds of load time; Neckbeard’s – try not to complain, you’re getting a lot for your patience 😉
After spending a few seconds spinning up, STS should prompt you to ask where you’d like your “workbench”, which is just a folder to plop your projects into. I generally create sub folders in my workbench folder, but I tend to create a lot of projects. For our purposes you’d be best served stashing your projects in any new / empty directory.
Once STS is loaded, go ahead and select “Open Dashboard” (if presented with a list of options) and prepare yourself for the noisy view that is the Eclipse workbench. The area in the center is your primary editing area, with “dockable” panels all around.
For the purposes of these articles, we’ll only be focusing on the main editor space, the Package Explorer on the right, and the Console tab at the bottom.
Creating a Project
Finally! Let’s get into the good stuff and craft a little code. So, those of you who are familiar with Eclipse (or really any modern IDE) might be thinking that getting started is easy: Simply go to File > New > Java Project, enter a project name (and perhaps select a target Java version). Sure, that would work, but don’t do this…
While a vanilla Java project might seem like a good place to throw some code, it never is. The Java ecosystem is huge and you will almost undoubtedly want to reuse someone elses code, so do yourself a favor and get in the habit of always leveraging a dependency management (and Build tool, while we’re at it) like Maven.
So, lesson number 1: whenever starting a new project, whether just throwing some code in a public static void main() or building a production system, create a Maven project via the following: File > New > Maven Project. The second step in the “New Maven Project” wizard will look like the image to the right. Check the “Create ” and continue on (I’ll describe what this means and what your alternative options are when I cover Maven in a more detail later).
Moving on to the final step in the wizard, you’ll need to enter at least two pieces for your project (which will be considered a Maven artifact): a Group Id (usually a reverse domain like co.davidwelch.examples, similar to a package) and an Artifact Id (usually lowercase and hyphen-separated, like effective-java-example). The rest of the fields are optional, except for Version, but that comes with a sensible default so just leave it be for now.
The Fruits of Your Labor
After all that, you’ll end up with a project structure similar to the image on the left, complete with empty folders and all. This might seem like a lot of tedious clicking for very little, but I’ll show you shortly why this simple recipe turns out to buy you a lot. For now, the bits and pieces to pay attention to:
- src/main/java – where your Java source files should go. This is where you should probably spend most your time.
- src/main/resource – where non-binary / non-source-code files should go. This is useful if you have configuration files, static resources, etc.
- src/test/java– where your Test Suite’s Java source files go. You should definitely be spending time here as well
- src/test/resource – similar to it’s src/main counterpart.
- pom.xml – the maven configuration file that drives your file. We’ll get familiar with this in a later article, but this is where you go to add dependencies, configure which Java runtime you’re targeting, etc.
Pro tip: select the little downward-pointing arrow located at the top right of the Project Explorer pane, then select Package Presentation > Hierarchal. That way, as you create a multitude of packages, you don’t get a lot of redundant data clouding up your view.
Off to the Races
With that, dear readers, you’re now all set to finally start slinging some code. Go ahead and create a class in that src/main/java folder and start toying around. Heck, throw a public static void main() method in there and run that beast from STS!
In the next article, we’ll look at actually writing some code and executing it in a intelligent mannor. We’ll see how to easily write & run unit tests, bring in third party code, and hopefully some other fun things.