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