Java Reference
In-Depth Information
Plug-in executions with matching
ID
s
■
Plug-in configurations
■
And again, each of these elements specified in the parent
pom
get automatically speci-
fied in the child
pom
.
We discuss the
pom
s further in the upcoming sections.
10.2
Setting up a Maven project
Now that you've seen the differences between Ant and Maven, it's time to move
on and start building our projects with Maven. But first, let's examine the installa-
tion process.
Installing Maven
Installing Maven is a three-step process:
1
Download the latest distribution from
http://maven.apache.org/
and unzip/
untar it in the directory of your choice (for example, C:\maven on Windows or
/opt/maven on UNIX).
2
Define an
M2_HOME
environment variable pointing to where you've installed
Maven.
3
Add
M2
_
HOME\bin
(
M2
_
HOME/bin
on
UNIX
) to your
PATH
environment variable
so that you can type
mvn
from any directory.
You're now ready to use Maven. The first time you execute a plug-in, make sure
your internet connection is on, because Maven will automatically download from
the web all the third-party JARs the plug-in requires.
Let's navigate to the c:\junitbook2\ directory. This is our work directory, and here we
set up the Maven examples. Type the following on the command line:
mvn archetype:create -DgroupId=com.manning.junitbook
➥
-DartifactId=maven-sampling -DarchetypeArtifactid=maven-artifact-mojo
After you press Enter and wait for appropriate artifacts to download, you should see a
directory named
maven-sampling
being created. If you look inside that directory, you
should see the directory structure being created, as shown in figure 10.1.
What happened here? We invoked the
maven-archetype-plugin
from the com-
mand line and told it to create a new project from scratch with the given parameters.
As a result, this Maven plug-in created a new project with a new directory structure,
following the convention of the directory structure. Further, it created a sample
App.java
class with a main method and a corresponding AppTest.java file that's a unit
test for our application. Now, most likely, after looking at this directory structure,
you're quite familiar with which files stay in src/main/java and which files stay in src/
test/java.