By Vincent Massol, Tim O'Brien
Book Price: $29.95 USD
£20.95 GBP
PDF Price: $23.99
Cover | Table of Contents | Colophon
http://maven.apache.org/ and
select Downloading from the Getting Maven menu on the left navigation
menu. This will take you to a page which will let you select a
Windows Installer package, a .zip file, a
tar'd .bzip file, or a
tar'd .gzip file. Download the
distribution appropriate for your platform.MAVEN_HOME, pointing to the location of
your Maven installation. You will then need to add
%MAVEN_HOME%\bin to your PATH
by selecting Control
Panel→System→Advanced and clicking
the Environment Variables button. Prepend
%MAVEN_HOME%\bin to your PATH
variable, and go to the command prompt by running
cmd.exe. If Maven has been installed
successfully, you should see the following output on the command
line:http://maven.apache.org/ and
select Downloading from the Getting Maven menu on the left navigation
menu. This will take you to a page which will let you select a
Windows Installer package, a .zip file, a
tar'd .bzip file, or a
tar'd .gzip file. Download the
distribution appropriate for your platform.MAVEN_HOME, pointing to the location of
your Maven installation. You will then need to add
%MAVEN_HOME%\bin to your PATH
by selecting Control
Panel→System→Advanced and clicking
the Environment Variables button. Prepend
%MAVEN_HOME%\bin to your PATH
variable, and go to the command prompt by running
cmd.exe. If Maven has been installed
successfully, you should see the following output on the command
line:C:\dev\mavenbook\code>maven -v
_ _ _ _
| \/ |_ _ _Apache_ _ _ _ _
| |\/| / _` \ V / -_) ' \ ~ intelligent projects ~
|_| |_\_ _,_|\_/\_ _ _|_||_| v. 1.0.2genapp goal, selecting
the default template, and supplying some information about your new
project:C:\dev\mavenbook\code\genapp\test-application>maven genapp _ _ _ _ | \/ |_ _ _Apache_ _ _ _ _ | |\/| / _` \ V / -_) ' \ ~ intelligent projects ~ |_| |_\_ _,_|\_/\_ _ _|_||_| v. 1.0.2 Attempting to download commons-jelly-tags-interaction-20030211.143817.jar. 4K downloaded Enter a project template to use: [default] [Enter] Please specify an id for your application: [app] test-application Please specify a name for your application: [Example Application] Test Application Please specify the package for your application: [example.app] mdn.testapp build:start: genapp: [copy] Copying 1 file to C:\dev\mavenbook\code\genapp\test-application\src\java\ mdn\testapp [copy] Copying 3 files to C:\dev\mavenbook\code\genapp\test-application\src\test\ mdn\testapp [copy] Copying 1 file to C:\dev\mavenbook\code\genapp\test-application\ [copy] Copying 2 files to C:\dev\mavenbook\code\genapp\test-application\ BUILD SUCCESSFUL
default application
template, and you are supplying an application ID, application name,
and package for the new project. The default application template
creates a single class, mdn.testapp.App, with a
static main function and two JUnit tests.maven.proxy.host = proxy.company.com maven.proxy.port = 80
80 on the proxy.company.com
machine. If you are using a proxy server that requires
authentication, you will need to specify two additional properties:maven.proxy.username = tobrien maven.proxy.password = myp@ssw0rd
maven.proxy.ntlm.username = tobrien maven.proxy.ntlm.password = myp@ssw0rd
App class.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
<project> <extend/> <pomVersion/> <id/> <name/> <groupId/> <currentVersion/> <organization/> <inceptionYear/> <package/> <logo/> <gumpRepositoryId/> <description/> <shortDescription/> <url/> <issueTrackingUrl/> <siteAddress/> <siteDirectory/> <distributionSite/> <distributionDirectory/> <repository/> <versions/> <branches/> <mailingLists/> <developers/> <contributors/> <licenses/> <dependencies/> <build/> <reports/> <properties/> </project>
contributors,
developers, dependencies,
reports, and repository. The
labs in this chapter will provide the details, but you should use the
previous XML excerpt to place elements in the proper sequence within
your jar:jar goal from
the JAR plug-in as follows:C:\dev\mavenbook\code\genapp> maven jar:jar
jar before the colon separator classifies this
goal as belonging to the JAR plug-in. To see a list of all the
goals in the JAR plug-in,
enter the following command:C:\dev\mavenbook\code\genapp> maven -P jar
_ _ _ _
| \/ |_ _ _Apache_ _ _ _ _
| |\/| / _` \ V / -_) ' \ ~ intelligent projects ~
|_| |_\_ _,_|\_/\_ _ _|_||_| v. 1.0.2
Goals in jar
= = = = = = = = = = = =
[jar] Create the deliverable jar file.
deploy ......................... Deploy a jar to the remote repository
deploy-snapshot ................ Deploy a snapshot jar to the remote
repository
install ........................ Install the jar in the local repository
install-snapshot ............... Install a snapshot jar in the local
repository
jar ............................ Create the deliverable jar file.
snapshot ....................... Create a snapshot jar, ie '
id-YYYYMMDD.hhmmss.jar'
Plugin for creating JAR files. Requires Maven 1.0 RC2.
C:\dev\mavenbook\code\genapp\test-application> maven -g | more
maven test, you will receive the following output:java:compile:
[echo] Compiling to C:\dev\mavenbook\code\genapp\test-application/target/classes
[echo]
java:jar-resources:
[...]
java:compile or the
java:jar-resources goals? Running maven
-X test will display the full debugging output for all
goals executed in a Maven build. Let's try it, and
focus on the three goals listed earlier. Running maven -X
test produces the following output:[...]
java:compile:
[echo] Compiling to C:\dev\mavenbook\code\genapp\test-application/target/classes
[javac] [DEBUG] fileset: Setup scanner in dir
C:\dev\mavenbook\code\genapp\test-application\src\java with
patternSet{ includes: [ ] excludes: [**/package.html] }
[javac] [VERBOSE] mdn\testapp\App.java omitted as mdn/testapp/App.class is up
to date.
java:jar-resources:
[DEBUG] FileSet: Setup scanner in dir
C:\dev\mavenbook\code\genapp\test-application\src\conf with
patternSet{ includes: [*.properties] excludes: [ ] }
[VERBOSE] app.properties omitted as app.properties is up to date.
[...]
java:compile task may
look familiar. It is the output of Ant's
echo and javac tasks. As
explained in Section 2.1, Maven frequently uses Ant
tasks to perform common operations such as copying, deleting,
compiling, and creating JAR files.http://www.ibiblio.org/maven/. Load this URL
in a web browser and you will see a series of directories; the
directory we are interested in is
springframework, and the
structure
of the subdirectories under springframework
follows:http://www.ibiblio.org/maven
/springframework
/jars
spring-core-1.1.4.jar
spring-dao-1.1.4.jar
spring-web-1.1.4.jar
dependency—groupId,
artifactId, and version. You
can add dependencies to both artifacts by replacing
the dependencies element
in test-application/project.xml with the
following XML:<dependencies>
<dependency>
<groupId>springframework</groupId>
<artifactId>spring-core</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>springframework</groupId>
<artifactId>spring-web</artifactId>
<version>1.1.4</version>
</dependency>
</dependencies>
jar goal and take a look at the
output of Maven; it should contain something that looks like this:Attempting to download spring-core-1.1.4.jar. 266K downloaded Attempting to download spring-web-1.1.4.jar. 111K downloaded
jar goal:SNAPSHOT as part of the version
name. Every time you execute a Maven goal, Maven will check for a new
version of the dependency from the remote repository. Maven will
download the dependency if the remote repository has a newer version
than the local repository. For example, the following dependency
would always download the latest 1.2 development JAR of
spring:<dependency> <groupId>springframework</groupId> <artifactId>spring</artifactId> <version>1.2-SNAPSHOT</version> </dependency>
-o command-line option. For example, if you do
not have a network connection, but would like to execute the
test goal, run maven -o test.
Maven will then execute the test goal without
checking for dependencies. If your project does not depend on
SNAPSHOT builds, you should be able to
disconnect your environment without having to add the
-o flag. If you do rely on
SNAPSHOT builds, you will need to use the
-o flag, as Maven will attempt to check for the
presence of a newer SNAPSHOT every time it
executes a goal. In this case, the project will not build
successfully without the use of the -o flag.build:start. This goal
is executed before any other goal and does not perform any action. If
you run build:start, Maven will grab any
dependency referenced from the project.xml file.maven console at the command prompt. This should
produce the following output:_ _ _ _
| \/ |_ _ _Apache_ _ _ _ _
| |\/| / _` \ V / -_) ' \ ~ intelligent projects ~
|_| |_\_ _,_|\_/\_ _ _|_||_| v. 1.0.2
The following commands are available:
list - list all available goals
help - this message
<goalname> - attain a goal
quit - quits the console
test-application 1.0 >
java:compile. Maven will execute the
java:compile goal and return you to the prompt to
wait for another goal. To run two goals in sequence, you may enter
them at the prompt, separated by a space—for example,
clean test. This is known as
"goal chaining" and it is a way for
you to specify a series of goals you want Maven to obtain, in order.
To exit the Maven Console, type quit, and to see a
list of available goals, type list.java:compile goal very quickly
in the Maven Console, didn't it? When you use the
Maven Console you are executing a goal in an existing JVM. When you
run Maven from the command line, you have to wait for the JVM to
start up every time you want to run a goal. If you are not convinced
of the performance improvement, try it for yourself. Run the
java:compile goal from the command line 10 times
in a row, and then run the same java:compile goal
from the Maven Console 10 times. Take note of the time difference,
and you will see that the JVM startup time begins to increase. Use
the Maven Console if you find yourself frequently running Maven
goals, as it saves time by starting a JVM once.eclipse
plug-in:C:\dev\mavenbook\code\genapp\test-application> maven eclipse
build:start:
eclipse:generate-project:
[echo] Creating C:\dev\mavenbook\code\genapp\test-application/.project ...
eclipse:generate-classpath:
[echo] Creating C:\dev\mavenbook\code\genapp\test-application/.classpath ...
[echo] Contains JUnit tests
[echo] Setting compile of src/test to target/test-classes
Plugin 'cactus-maven' in project 'Test Application' is not available
[echo] Setting default output directory to target/classes
eclipse:
[echo] Now refresh your project in Eclipse (right click on the project and select
"Refresh")
BUILD SUCCESSFUL
Total time: 2 seconds
MAVEN_REPO to point to the local Maven
repository. You can set MAVEN_REPO using Maven, by
executing the following at the command line:maven -Dmaven.eclipse.workspace=c:\eclipse\workspace eclipse:add-maven-repo
http://mevenide.codehaus.org/mevenide-ui-eclipse/update/index.html)
is an
Eclipse
plug-in which allows you to use Maven from within Eclipse. You can
download it from an Eclipse Update site by following these
directions:Mevenide into
the Name field, and the location of the Eclipse Update site into the
URL field. The Eclipse Update site for Mevenide is http://mevenide.codehaus.org/release/eclipse/update/site.xml.
maven ant will create
a build.xml file which contains targets to
gather dependencies, build, and test your application. Take a look at
the output of running the default jar target:C:\dev\mavenbook\code\genapp\test-application>ant
Buildfile: build.xml
init:
[mkdir] Created dir: C:\dev\mavenbook\code\genapp\target\lib
get-deps:
[get] Getting: http://www.ibiblio.org/maven/springframework/jars/spring-core-1.1.4.jar
[get] Getting: http://www.ibiblio.org/maven/springframework/jars/spring-web-1.1.4.jar
compile:
[copy] Copying 1 file to C:\dev\mavenbook\code\genapp\target\classes
junit-present:
[echo] = = = = = = = = = = = = = = = = = = WARNING = = = = =
= = = = = = = = = = = = = = = = = = =
[echo] Junit isn't present in your ${ANT_HOME}/lib directory. Tests not executed.
[echo] = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = = = = = = = = = = = = = = = = = =
compile-tests:
internal-test:
test:
jar:
[jar] Building jar: C:\dev\mavenbook\code\genapp\test-application\target\
test-application-1.0.jar
BUILD SUCCESSFUL
Total time: 2 seconds
get-deps target which executes the Ant
http://maven.apache.org/using/migrating.html.copy, move,
delete, and junit, Maven offers
reusable build processes. Maven is a "build
container" which allows you to reuse build processes
over a series of projects. Take unit testing as just one example.
With Ant, you would perform a JUnit test by including the following
in your project's javadoc goal and Maven will
generate project documentation. Here is the output of the execution
of the javadoc goal:C:\dev\mavenbook\code\genapp\test-application>maven javadoc
_ _ _ _
| \/ |_ _ _Apache_ _ _ _ _
| |\/| / _` \ V / -_) ' \ ~ intelligent projects ~
|_| |_\_ _,_|\_/\_ _ _|_||_| v. 1.0.2
build:start:
xdoc:init:
maven-javadoc-plugin:report:
[mkdir] Created dir: C:\dev\mavenbook\code\genapp\test-application\target\
javadoc\src
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source files for package mdn.testapp...
[javadoc] Constructing Javadoc information...
[javadoc] Standard Doclet version 1.5.0_01
[javadoc] Building tree for all the packages and classes...
[javadoc] Generating C:\dev\mavenbook\code\genapp\test-application\target\docs\apidocs\
constant-values.html...
[javadoc] Copying file C:\Documents and Settings\tobrien\.maven\cache\
maven-javadoc-plugin-1.7\plugin-resources\stylesheet.css to file C:\dev\mavenbook\code\
genapp\test-application\target\docs\apidocs\stylesheet.css...
[javadoc] Building index for all the packages and classes...
[javadoc] Building index for all classes...
[delete] Deleting directory C:\dev\mavenbook\code\genapp\test-application\target\
javadoc\src
BUILD SUCCESSFUL
Total time: 7 seconds
mailingLists element:<mailingLists>
<mailingList>
<name>Maven User List</name>
<subscribe>users-subscribe@maven.apache.org</subscribe>
<unsubscribe>users-unsubscribe@maven.apache.org</unsubscribe>
<archive>http://marc.theaimsgroup.com/?l=turbine-maven-user</archive>
</mailingList>
<mailingList>
<name>Maven Developer List</name>
<subscribe>dev-subscribe@maven.apache.org</subscribe>
<unsubscribe>dev-unsubscribe@maven.apache.org</unsubscribe>
<archive>http://marc.theaimsgroup.com/?l=turbine-maven-dev</archive>
</mailingList>
</mailingLists>
contributor and a developer
element to project.xml:<developers>
<developer>
<name>Vincent Massol</name>
<id>vmassol</id>
<email>vmassol@apache.org</email>
<organization>Apache Software Foundation</organization>
<roles>
<role>Author</role>
<role>Developer</role>
</roles>
<url>http://www.massol.net</url>
<timezone>+1</timezone>
</developer>
</developers>
<contributors>
<contributor>
<name>Tim OBrien</name>
<email>tobrien@apache.org</email>
<organization>Apache Software Foundation</organization>
<roles>
<role>Author</role>
<role>Developer</role>
</roles>
<url>http://www.oreillynet.com/pub/au/1738</url>
<timezone>-6</timezone>
</contributor>
</contributors>http://svn.apache.org/repos/asf/struts/core/trunk:<repository>
<connection>
scm:svn:http://svn.apache.org/repos/asf/struts/core/trunk
</connection>
<developerConnection>
scm:svn:https://svn.apache.org/repos/asf/struts/core/trunk
</developerConnection>
<url>http://svn.apache.org/repos/asf/struts/core/trunk</url>
</repository>
connection
element tells Maven about the read-only location of
the SCM. scm identifies this URL as being an SCM
location, svn tells Maven that this URL will be
for a Subversion repository, and the final section of the URL is the
location to the project's trunk. You may also
specify the developerConnection; you use this
element when you need to segment your audience into people without
write access to source code, and people with write access.url element supplies a URL that can be used to
browse the repository. In the case of Struts, they have elected to
point to the Subversion repository itself, as it can be browsed with
a regular web browser. The Struts team could also elect to point to
the ViewCVS instance configured to point to the
ASF Subversion repository, which can be found at the following URL:
http://cvs.apache.org/viewcvs.cgi/struts/core/trunk/?root=Apache-SVN.C:\dev\mavenbook\code\genapp\test-application> maven site
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="jaxen" repository="jaxen" href="http://jaxen.org">
<title>jaxen: universal java xpath engine</title>
<body>
<links>
<item name="The Werken Company" href="http://www.werken.com/"/>
</links>
<menu name="jaxen">
<item name="Overview" href="/index.html"/>
<item name="FAQ" href="/faq.html"/>
<item name="Releases" href="/releases.html"/>
<item name="CVS Access" href="/cvs-usage.html"/>
<item name="Status" href="/status.html"/>
</menu>
</body>
</project>reports
element in project.xml. Here is a
reports element with several available reports
activated:<reports> <report>maven-changelog-plugin</report> <report>maven-changes-plugin</report> <report>maven-checkstyle-plugin</report> <report>maven-clover-plugin</report> <report>maven-cruisecontrol-plugin</report> <report>maven-developer-activity-plugin</report> <report>maven-faq-plugin</report> <report>maven-file-activity-plugin</report> <report>maven-license-plugin</report> <report>maven-linkcheck-plugin</report> <report>maven-javadoc-plugin</report> <report>maven-jdepend-plugin</report> <report>maven-jira-plugin</report> <report>maven-junit-report-plugin</report> <report>maven-jxr-plugin</report> <report>maven-pmd-plugin</report> <report>maven-simian-plugin</report> <report>maven-tasklist-plugin</report> </reports>
reports element. A Maven project that does not
specify the reports element generates a set of
default reports: jdepend, Checkstyle, changes, changelog,
developer-activity, file-activity, license, javadoc, jxr, junit,
linkcheck, and tasklist. When you add a reports
element to your project's
project.xml file, you must list all reports you
wish to have generated.reports element lists
all
these fancy reports, but you probably want to know what all of these
reports provide. Table 1-1 provides a brief
description of some of these
reports.|
Report plug-in
|
|---|
http://www.mavenbook.org web site. You can
also check out the Weather project from a Subversion repository at
http://www.mavenbook.org/svn/mdn/code/.http://maven-plugins.sourceforge.net/maven/.
However, this repository is synced every few hours to the ibiblio
repository at http://www.ibiblio.org/maven/. The ibiblio
repository is the default Maven remote repository and the one that
will be used if you don't tell Maven otherwise.
Should you still wish to add the remote repository to your Maven
configuration, you should modify your
build.properties
file (or
project.properties file if you want to share the
settings with others) and include the following property:maven.repo.remote=http://www.ibiblio.org,http://maven-plugins.sf.net/maven
http://www.ibiblio.org/maven/maven-plugins/plugins/)
is version 0.7. To install it, you need to use the
plugin:download goal of the Plugin plug-in,
passing it some properties, as shown shortly.groupId, an
artifactId, and a version when
you create a dependency in your project.xml
file, you need to pass the same properties to the Plugin
plug-in so that it knows the exact location of the plug-in you want
to download and install. Issue the following command from any
directory to install version 0.7 of the Axis plug-in:C:\>maven plugin:download -DgroupId=maven-plugins ^http://weather.gov/xml/. You are going to use
the Maven Axis plug-in to generate classes which will retrieve a
forecast from this web service. To do this, you will need to
customize the behavior of the Axis plug-in.maven
-P
axis should list the goals available in the
plug-in; one of them should be axis:wsdl2java. You
are going to use this goal to generate a client library from the Web
Service Description Language (WSDL) for
the weather
web service.http://weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl.
A WSDL document is an XML document which
completely describes the methods available in a SOAP service. The
Axis plug-in is going to use this XML document to create a client
library. For the purposes of this lab, this WSDL document is stored
in weather/src/wsdl/weather.wsdl. The Axis
plug-in will generate Java source files for all WSDL documents found
in this directory.