You have a new project with one class and a unit test. Next, let's
build the project and run the App
class.
Create a JAR file containing this application's classes by executing the jar:jar
goal. The JAR plug-in defines a
shorthand goal named jar
which
depends upon the jar:jar
goal.
Executing either goal will have the same result. All plug-ins define
such a shortcut; for example, the test
goal executes the test:test
goal from the Test plug-in.
Execute the jar
goal with maven jar
:
C:\dev\mavenbook\code\genapp\test-application>maven jar
_ _ _ _
| \/ |_ _ _Apache_ _ _ _ _
| |\/| / _` \ V / -_) ' \ ~ intelligent projects ~
|_| |_\_ _,_|\_/\_ _ _|_||_| v. 1.0.2
Attempting to download junit-3.8.1.jar.
118K downloaded
build:start:
java:prepare-filesystem:
[mkdir] Created dir: C:\dev\mavenbook\code\genapp\test-application\target\classes
java:compile:
[echo] Compiling to C:\dev\mavenbook\code\genapp\test-application/target/classes
[echo]
[javac] Compiling 1 source file to C:\dev\mavenbook\code\genapp\test-application\
target\classes
java:jar-resources:
Copying 1 file to C:\dev\mavenbook\code\genapp\test-application\target\classes
test:prepare-filesystem:
[mkdir] Created dir: C:\dev\mavenbook\code\genapp\test-application\target\
test-classes
[mkdir] Created dir: C:\dev\mavenbook\code\genapp\test-application\target\
test-reports
test:test-resources:
test:compile:
[javac] Compiling 3 source files to C:\dev\mavenbook\code\genapp\test-application\
target\test-classes
test:test:
[junit] Running mdn.testapp.AppTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.078 sec
jar:jar:
[jar] Building jar: C:\dev\mavenbook\code\genapp\test-application\target\
test-application-1.0.jar
BUILD SUCCESSFUL
Total time: 9 seconds
Maven creates a target
directory to hold intermediate files and JAR files. Once the JAR has been
created, execute the App
class as
follows:
Note
What's with the ^ and More? prompt? This is how a DOS command line is continued.
C:\dev\mavenbook\code\genapp\test-application>java ^
More?target\test-application-1.0.jar
mdn.testapp.App Hello World!
If you want to do this again, run maven
clean
to remove the target directory and build from
scratch.
Note
Maven just works. The first time it needs to run unit tests it is smart enough to download JUnit for you if you don't have it locally. No more searching for JAR files on the Web.
When you ran the jar
goal,
Maven used the JAR plug-in to create a JAR artifact. First, Maven
figured out that it had to run a series of goals to be able to create
this application's JAR file; the JAR plug-in has a jar:jar
goal which depends on other goals
which, in turn, depend on other goals. Maven figured out that the
following sequence of goals needed to be executed: java:prepare-filesystem
, java:compile
, java:jar-resources
, test:prepare-filesystem
, test:test-resources
, test:compile
, and test:test
.
Maven saw that it needed to execute a goal from the Test plug-in which executes JUnit tests, and it checked the local Maven repository for the JUnit JAR file. Because you haven't used Maven yet, it downloaded junit-3.8.1.jar from Maven's default repository at http://www.ibiblio.org/maven/. You'll learn about the local Maven repository and Maven's powerful dependency management capabilities later in this chapter.
Get Maven: A Developer's Notebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.