Maven 2.0.4 or later is required to build SAF2.
First, let's review some Maven basics. Maven uses the notion of a build lifecycle to which plugins can attach. (Plugins are similar to Ant tasks.) When a Maven build is invoked, we specify a point in the lifecycle up to which the build should proceed. The phase compile comes before test, and test comes before package, and package comes before install. Once we have Maven setup, we can invoke the Struts build, and specify which phase the build should use.
Installing
The install phase builds up the project ("package"), and installs any JARs it needs into your local repository (e.g. ~/.m2/repository
). Once installed, the JARs can be used by any other Maven project you build.
To run a basic install, change to the root of the source distribution, and enter
> mvn install
That's it! Maven will download all dependencies the build needs, run all unit tests, package up the JARs, and then install the new JARs locally. For your convenience, copies of the JARs can be found in the target directories of each module. For example, after the build, the main JAR can found at action2/target/action-2.0-SNAPSHOT.jar
.
It's suppose to be automatic, but you might still have to press the button
Sometimes, licensing restrictions prevent Maven for downloading all the JARs that a build might need. For example, JavaMail and Activation, can only be downloaded from Sun. When this happens, Maven will display a helpful message that explains how to install these JARs manually. After downloading the required JAR, follow the instructions to install it to the your local repository. Once installed, the JAR is availale to all your Maven builds, not just Struts.
[INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] ------------------------------------------------------------------------ [INFO] Struts Action Framework 2.0 Project ................... SUCCESS [0.688s] [INFO] Struts Action Framework 2.0 API ....................... SUCCESS [2.125s] [INFO] Struts Action Framework 2.0 ........................... SUCCESS [21.866s] [INFO] Webapps ............................................... SUCCESS [0.002s] [INFO] Blank Webapp .......................................... SUCCESS [0.982s] [INFO] Portet Webapp ......................................... SUCCESS [2.038s] [INFO] Shopping Cart Webapp .................................. SUCCESS [0.934s] [INFO] Showcase Webapp ....................................... SUCCESS [3.351s] [INFO] Starter Webapp ........................................ SUCCESS [1.013s] [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 33 seconds [INFO] Finished at: Wed May 03 18:23:38 PDT 2006 [INFO] Final Memory: 11M/43M [INFO] ------------------------------------------------------------------------
Other phases
There are other phases that can be useful when working with Maven. The package phase will just JAR (or WAR) the modules. The test phase will run only the unit tests. The compile phase will only build the source code (but not the test sources). And the clean phase will remove all artifacts, typically the entire target directory.
Build profiles
The next step to building the framework with Maven is to understand build profiles. Profiles provide different configurations for a build. There are several profiles, including default, xwork, and _thirdparty".
Profile |
Description |
---|---|
default |
Builds action-api, action, and all sample webapps |
xwork |
Includes the xwork build |
thirdparty |
Includes additional modules that cannot be part of the default build due to Apache rules |
Tbe default profile will work for most developers, but someMost developers will want to use additional profiles as they work on both XWork and other modules, such as the JasperReports integration.
Specify a profile is as simple as:
> mvn -Pprofile ...
Third Party Profile
If you want to build all the third-party add-ons not included with the default build, use the _thirdparty*_profile.
> mvn -Pthirdparty package
[INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] ------------------------------------------------------------------------ [INFO] Struts Action Framework 2.0 Project ................... SUCCESS [0.646s] [INFO] Struts Action Framework 2.0 API ....................... SUCCESS [2.389s] [INFO] Struts Action Framework 2.0 ........................... SUCCESS [22.155s] [INFO] Webapps ............................................... SUCCESS [0.002s] [INFO] Blank Webapp .......................................... SUCCESS [0.906s] [INFO] Portet Webapp ......................................... SUCCESS [1.661s] [INFO] Shopping Cart Webapp .................................. SUCCESS [0.779s] [INFO] Third Party Modules ................................... SUCCESS [0.003s] [INFO] JasperReports ......................................... SUCCESS [0.918s] [INFO] Showcase Webapp ....................................... SUCCESS [2.336s] [INFO] Starter Webapp ........................................ SUCCESS [0.571s] [INFO] JFree Chart ........................................... SUCCESS [1.972s] [INFO] Pell File Upload ...................................... SUCCESS [0.385s] [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 35 seconds [INFO] Finished at: Wed May 03 18:26:19 PDT 2006 [INFO] Final Memory: 14M/52M [INFO] ------------------------------------------------------------------------
In the second run, serveral new categories are built, including "Third Party Modules" and JasperReports. If you decide to make use of any of the third-party modules, be sure to review the license, since these modules may not be under the Apache License.
XWork profile
Struts Action Framework 2 is an extension of the XWork framework. While SAF2 adds a lot of value for web developers, much of the core functionality derives from XWork. The xwork plugins add the capability to build against a current XWork checkout, rather than a precompiled JAR.
The plugin assumes that the latest XWork code is checked out and is located at ../xwork
, relative to the Action2 folder. You can check out XWork using two CVS commands.
cvs -d :pserver:guest@cvs.dev.java.net:/cvs login cvs -d :pserver:guest@cvs.dev.java.net:/cvs co xwork
The guest user has a blank password at java.net. If you have your own java.net account, you can use your own credentials instead. (In fact, you will be checking in XWork code too, be sure that you do!)
CVS for Windows users
If you need to install CVS under Windows, download and extract the CVS binary, and place it on your PATH.
Subversion CLI
The Maven build may need to utilize Subversion. If you don't have the command line version installed, you can download it from the Subversion site, and add the bin
folder to your command path.
> mvn -Pxwork package
[INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] ------------------------------------------------------------------------ [INFO] Struts Action Framework 2.0 Project ................... SUCCESS [0.774s] [INFO] Struts Action Framework 2.0 API ....................... SUCCESS [1.969s] [INFO] XWork ................................................. SUCCESS [8.757s] [INFO] Struts Action Framework 2.0 ........................... SUCCESS [24.947s] [INFO] Webapps ............................................... SUCCESS [0.002s] [INFO] Blank Webapp .......................................... SUCCESS [1.068s] [INFO] Portet Webapp ......................................... SUCCESS [1.391s] [INFO] Shopping Cart Webapp .................................. SUCCESS [0.745s] [INFO] Showcase Webapp ....................................... SUCCESS [3.064s] [INFO] Starter Webapp ........................................ SUCCESS [0.625s] [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 44 seconds [INFO] Finished at: Wed May 03 18:31:49 PDT 2006 [INFO] Final Memory: 14M/46M [INFO] ------------------------------------------------------------------------
Now, Maven will build XWork from source and use that as the XWork snapshot for the Action2 build.
Multiple profiles
Maven can build against multiple profiles. If you are developing against both the XWork and Action2 frameworks at once, you can install both profiles with one call to Maven.
> mvn -Pthirdparty,xwork package
Using both profiles is especially important when using Maven to build your IDE project files (recommended).
Building IDE project files
Maven has a great feature that will allow you to build up your IDEA or Eclipse project files based on the project build structure. Using the Maven project files helps keep developers in-sync and efficient. The IDEA project files are pre-configured to define in the Run/Debug menu many common tasks, such as "execute all tests", "launch the showcase sample app", and so fort.
IDEA by JetBrains
First, be sure you have the latest IDEA plugin for Maven.
mvn -DconnectionUrl=scm:svn:http://svn.apache.org/repos/asf/maven/plugins/trunk/maven-idea-plugin \ -Dgoals=install \ scm:bootstrap
Aside from running phases, Maven can also run plugins, and you can even specify phases and plugins as a single command. To just build the IDEA project file, the profiles are optional, but it can be a good idea to use them anyway.
> mvn -Pthirdparty,xwork idea:idea
Maven will generate project.ipr, project.iws, and an iml file for each module in the build. Open up project.ipr in IDEA, and you shuold be good to go.
If you ever need to rebuild your projects, running the command again will update your files without overriding any information that doesn't conflict. If you do want to overwrite the project files, specify the overwrite parameter as true.
> mvn -Doverwrite=true -Pthirdparty,xwork idea:idea
Clearing the cache
If you find the xwork module causing you problems, such as displaying as "XWork" when it should be named "xwork", the problem is likely to be within IDEA. Try clearing out your IDEA system cache and then run it again
Eclipse
For Eclipse, try
> mvn -Pthirdparty,xwork eclipse:eclipse
Feedback Wanted
Many SAF2 developers use IDEA, and the Eclipse project files are not as well-tested or featureful as the IDEA versions. But as far as we know, they work!
IMPORTANT: Running the Sample Applications from Eclipse or non-IDEA IDEs
Currently the sample applications can be deployed using QuickStart. With the switch to Maven, the expected location for the JARs is no longer valid. Fortunately, QuickStart has a feature where it can read in one or more IDEA iml (module) file and use the JARs specified there. So far the Showcase application is configured to do this (see its quickstart.xml
file).
Even if you're using Eclipse, it is recommended that you generate the IDEA project files so that you can run the Showcase webapp via QuickStart.
Main class: org.apache.struts.action2.Main VM params: none Program params: quickstart Working directory: webapps/showcase Classpath: must include at least the Action2 module (Which contains the Main class)
These settings are already baked into the IDEA workspace file.
Maven Tips
A few helpful tips for using Maven are provided:
Offline mode
If you are disconnected from the Internet or simply wish to make your build faster, pass in the -o argument and Maven won't check for new modules to download.
mvn -o -Pthirdparty,xwork package
Skipping test execution
Although this shouldn't ever happen, sometimes tests do fail and you need to build the framework anyway. If there's a problem, you can pass in the skip tests parameter.
mvn -Dmaven.test.skip=true -Pthirdparty,xwork package
Of course, if you find tests are failing, please submit a patch to fix them!
Mirrors
The default mirror for Maven builds ("ibiblio") can be slow and unreliable. Maven lets you specify alternative mirrors so that you don't have to depend on ibiblio for everything.
You can add new mirrors through the Settings file (~/.m2/settings.xml).
<settings> <mirrors> <mirror> <id>dotsrc</id> <url>http://mirrors.dotsrc.org/maven2</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> </settings>
Sometimes the alternative mirrors have problems too or aren't updated at the same frequency as the main mirror. If you have trouble building, try commenting out the aternative mirror.
First time building
In some cases it has been seen that Maven will complain a module doesn't exist, even though it is part of the current build. Often, the missing module complete turins up when executing {mvn package}}. A simple fix for this is to run mvn install
instead. If you have to do this, it will probably only be a one-time thing.