Section 3.8. Building All Subprojects Simultaneously


3.8. Building All Subprojects Simultaneously

Now that you have seen how to split a project into subprojects and how to build each of them individually, let's talk about building them all at once, in the correct dependency order.

3.8.1. How do I do that?

The Multiproject plug-in produces a project dependency graph that allows it to execute any goal on any project under its control. The best way to use the Multiproject plug-in is to set up a master Maven project. This master project is usually set up at the top level of the project directory structure. In the case of the QOTD web application, this means creating top-level project.xml, maven.xml, and project.properties files (see Figure 3-7).

Figure 3-7. Directory structure with top-level project files (maven.xml, project.properties, and project.xml)


This top-level Maven project is a project like any other, except that it doesn't have any source directory. The project.xml file extends the common/project.xml file, as do the other subprojects:

<?xml version="1.0"?> <project>   <extend>${basedir}/common/project.xml</extend>   <artifactId>qotd</artifactId>   <name>QOTD</name>   <shortDescription>Quote Of The Day Webapp</shortDescription>   <description>     Sample Webapp displaying a Quote Of The Day   </description>

The Multiproject plug-in offers a multiproject:goal goal which executes the goal specified by the Maven goal property on all the subprojects it can find. For example, executing maven multiproject:goal -Dgoal=clean will execute the clean goal on all subprojects. However, before executing this command you need to tell the Multiproject plug-in which projects are considered subprojects. You configure this through Maven properties. Three properties control the list of projects to include/exclude, and their default values are as follows:


maven.multiproject.basedir=${basedir}

This property represents the location from which the plug-in will look for Maven projects. (In practice, the Multiproject implementation looks for project.xml files. When it finds one, it knows it has found a Maven project.)


maven.multiproject.includes=*/project.xml

This property defines which Maven project to include. The default value says to look for all project.xml files located in any directory directly under the top-level directory.


maven.multiproject.excludes=

This is the counterpart of the maven.multiproject.includes property. It says which project to exclude from the multiproject build.

If you wanted to tell the Multiproject plug-in to look for all project.xml files located in any directory depth under the top-level directory, you would write maven.multiproject.includes=**/project.xml (this follows the Ant pattern syntax; see http://ant.apache.org/manual/dirtasks.html for more details). Note that the **/project.xml pattern would also match the top-level project.xml file. You would not want this, as it would create a circular loop. Thus, you would need to exclude the top-level project.xml by specifying maven.multiproject.excludes=project.xml.

For the QOTD web application project, you want to exclude the common/project.xml file because it's not a real Maven project and you don't want the Multiproject plug-in to start building it! Your top-level project.properties file should be:

maven.multiproject.includes=*/project.xml maven.multiproject.excludes=common/project.xml

With these properties set, execute the command maven multiproject:goal -Dgoal=clean:

C:\dev\mavenbook\code\qotd>maven multiproject:goal -Dgoal=clean  _ _  _ _ |  \/  |_ _ _Apache_ _ _ _ _ | |\/| / _` \ V / -_) ' \  ~ intelligent projects ~ |_|  |_\_ _,_|\_/\_ _ _|_||_|  v. 1.0.2    build:start:    multiproject:projects-init:     [echo] Gathering project list Starting the reactor... Our processing order: QOTD Core QOTD Web QOTD Acceptance Tests QOTD Packager +---------------------------------------- | Gathering project list QOTD Core | Memory: 3M/4M +---------------------------------------- +---------------------------------------- | Gathering project list QOTD Web | Memory: 3M/4M +---------------------------------------- +---------------------------------------- | Gathering project list QOTD Acceptance Tests | Memory: 3M/4M +---------------------------------------- +---------------------------------------- | Gathering project list QOTD Packager | Memory: 3M/4M +---------------------------------------- Starting the reactor... Our processing order: QOTD Core QOTD GUI QOTD Acceptance Tests QOTD Packager +---------------------------------------- | Executing clean QOTD Core | Memory: 3M/4M +---------------------------------------- [...]

As you can see, the Multiproject plug-in has automatically discovered the right project build order: QOTD Core, QOTD Web, QOTD Acceptance Tests, and QOTD Packager. The processing order is listed twice because the Multiproject plug-in gathers the project list using a Maven Jelly tag called the reactor for both the projects-init goal and the goal specified in -Dgoal=clean. In a second step, the Multiproject plug-in executes the goal specified on all the projects; in this case, clean is executed on all subprojects.


Tip: You may have noticed the "3M/4M" memory usage in the preceding code. The first number is the memory used and the second number is the total memory available. They are displayed because there were memory problems in the past, and this was a way to track them. Most of the leaks due to Maven itself have been fixed. However, some due to external libraries still remain, but as Maven continues to mature, you are unlikely to encounter these memory leaks.


Maven. A Developer's Notebook
Maven: A Developers Notebook (Developers Notebooks)
ISBN: 0596007507
EAN: 2147483647
Year: 2003
Pages: 125

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net