Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
This video introduces the standard directory layout for a Maven project. We demonstrate this by using Maven itself to create a starter project using the archetype feature.
Install tree on MacOS
Using Homebrew:
brew install tree
Using tree on Windows
Since Windows has a built-in tree
feature, there should be nothing to install. You can start using it right away. See more here.
-
0:00
Maven, like other build tools such as Gradle,
-
0:02
has opinions on what a Java project should look like.
-
0:05
And though this can be configured to your heart's desire,
-
0:07
let's take a look at the standard directory structure of a Maven project.
-
0:11
Actually, I think the best way to go about this would be to use Maven
-
0:14
to create a project.
-
0:15
Now I realize that if you're a Java developer you likely have your favorite
-
0:19
IDE, be it IntelliJ, Eclipse, NetBeans, or what have you.
-
0:23
However, in this workshop, in order to maintain our focus on Maven, even though
-
0:27
I'll do some coding in IntelliJ, I'll use the terminal to run all Maven commands.
-
0:32
But rest assured that all major IDEs have nice plugins and support for Maven.
-
0:37
So there are convenient menu options for everything we'll do on the command line.
-
0:41
Creating Maven projects is based on the concept of an archetype.
-
0:44
Think of an archetype as a predefined template,
-
0:47
like a blueprint from which we can generate projects.
-
0:50
So let's use our terminal here and get started.
-
0:54
The first thing I'll do is change directories to the parent of where I want
-
0:57
my project to be.
-
0:58
For me, that's in Code/screencasts, excellent.
-
1:04
Next, to generate the project we'll use the Maven command.
-
1:07
That's abbreviated mvn followed by the archetype plugins, archetype.
-
1:13
The archetype plugins generate goal and the -B option.
-
1:18
More about plugins and goals later on.
-
1:21
That -B option is for batch mode.
-
1:24
It tells the plugin not to ask questions along the way.
-
1:27
We'll specify all our config options in this command.
-
1:31
And what are those config options?
-
1:32
Well, they're system properties set for the running of Maven,
-
1:36
using the -D option to define system properties.
-
1:40
So we'll specify the groupId
-
1:45
as com.teamtreehouse and
-
1:50
the artifactId as file-spy.
-
1:55
I'll tell you more about the project in a moment.
-
1:57
The group and artifact IDs help categorize and identify your project,
-
2:01
which is the main form of organization of repositories like the ever popular
-
2:06
Maven Central.
-
2:08
Now this project will be creating in this workshop will be a console application
-
2:11
that spies on a directory,
-
2:13
displaying messages in the console when files of a certain type are added.
-
2:16
We'll get to the code much later on.
-
2:18
Okay, after pressing enter to run the command, Maven will generate a Java Maven
-
2:23
project with the standard directory structure and relevant assets.
-
2:27
On my machine, I have installed a command line tool called tree
-
2:31
that's super useful for displaying directory trees in the terminal.
-
2:34
I've put some info in the teacher's notes for
-
2:36
installing tree in case you're interested.
-
2:38
So if I change directories to file-spy and run the tree command,
-
2:43
you'll see the directory structure of the generated project right here.
-
2:47
If you've done work in Gradle, you are familiar with this structure.
-
2:50
In any case, inside our project's root directory,
-
2:53
we have a source directory containing main and test directories.
-
2:58
Each of those contains a java directory, which serves as the root directory for
-
3:02
any source code in the main directory or test code in the test directory.
-
3:07
Inside those java directories we have directories for our root package that was
-
3:11
created when we specified the group ID as com.teamtreehouse.
-
3:15
If you have any resources like properties files, they'll be in the main/resources
-
3:19
directory and the test/resources directory, like this.
-
3:24
So let's make those directories.
-
3:29
Src/test/resources.
-
3:34
And with resources, your directory structure will look like this.
-
3:37
You'll have a resources directory under main and a resources directory under test.
-
3:42
If you haven't used properties files in the past,
-
3:44
then don't worry about this resources directory.
-
3:47
Now, once code is compiled, you'll find the .class
-
3:50
files in a directory name target located in the projects root directory.
-
3:54
So right under the root with pom.xml and source directory,
-
3:58
you'll see another directory called target after you compile your code.
-
4:03
And that's all there is to a Maven project.
-
4:05
Well, just kidding.
-
4:06
Not even close.
-
4:08
See this measly little file that I skipped over in the project, the pom.xml?
-
4:12
Turns out that this is everything in Maven.
-
4:14
Why don't you take a quick breather before we dive into the all-important pom,
-
4:18
which is a one-stop configuration file for your entire Maven project.
You need to sign up for Treehouse in order to download course files.
Sign up