
Apache Maven Cookbook
By :

Now that we have set up Maven on our favorite operating system and verified that it works fine, it is time to create a simple Java project.
Maven makes it easy to bootstrap a new project by creating a bunch of files and folders following accepted conventions.
Let's start creating the first simple project using Maven, by performing the following steps:
mvn archetype:generate -DgroupId=com.packt.cookbook -DartifactId=simple-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
You can change the groupId
and artifactId
values in the preceding command as per your requirement.
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom (4 KB at 1.4 KB/sec)
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) > generate-sources
@ standalone-pom >>>
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.packt.cookbook
[INFO] Parameter: packageName, Value: com.packt.cookbook
[INFO] Parameter: package, Value: com.packt.cookbook
[INFO] Parameter: artifactId, Value: simple-project
[INFO] Parameter: basedir, Value: C:\projects\apache-maven-cookbook
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\projects\apache-maven-cookbook\simple-project
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Did you get an while error running the preceding command to create your simple project?
One possibility is that your Maven is behind an HTTP proxy server. If so, see the Running Maven behind an HTTP proxy server recipe in this chapter.
Let's look at the folder structure that is created:
You will notice the following things:
pom.xml
is created in the root of the simple-project
folder. We will explore this file in detail in subsequent sections.src\main\java
: This is for Java source filessrc\test\java
: This is for Java test source filessrc\main\resources
: This is for resource files for the projectsrc\test\resources
: This is for resource files for the testgroupId
(org.packt.cookbook
) is created.The following are essentially Maven conventions at work:
in src\main\java
src\test\java
src\main\resources
and test resources to reside in src\test\resources
groupId
parameter (though this is not mandatory)App.java
and AppTest.java
, are also created and it is not expected that they will be used beyond testing how Maven worksThe mvn
command that we used in the Creating a simple project with Maven recipe in this chapter, tries to invoke the generate
goal of the archetype
plugin with the specified command-line parameters.
The default Maven installation has minimal features. All features of Maven are available as Maven plugins. When given a plugin name, Maven knows where to download it from and then run it.
In this case, Maven downloads the archetype
plugin. This plugin, in turn, can depend on another plugin. In this case, the latter plugin gets downloaded. This happens in a recursive fashion and, at the end of the process, all the relevant plugins required to run the specified command are downloaded.
These plugins are placed in your local repository, which is a location in your system. Once downloaded, these are never downloaded again unless deleted.