Maven Lifecycle Phases - Fitting in Code Analysis and Other Tools
The build management portion of Maven operates on a type of Template Pattern. Maven moves from lifecycle-phase to lifecycle-phase until there a step failure or until all steps are complete. The following diagram lists the build lifecycle phases. The orange squares represent the main targets that people run. Every phase is executed starting with Validate until the requested end phase is reached.
Selecting a code coverage maven phase can take some thought.
For example
- "mvn validate" runs just the Validate phase.
- "mvn compile" runs Validate, Initialize, Generate Sources, Process Sources, Generate Resources, Process Resources and Compile.
Each Maven Plugin executes with in a phase. The Surefire unit test plugin, as an example, typically runs the tests in the Test phase. This means that unit tests don't run if Validation, Compilation, class processing or any of the other preceding phases run with errors.
Maven plugins can execute in their default phase or in any phase of your choosing. Lifecycle phase selection affects if and when the plugin runs. You have to decide which mvn command you wish to run the plugin in and which types of failures obviate the need for that plugin.
Code Quality
Teams can use Maven to generate code quality metrics with a variety of plugins. These plugins can be used to fail the build based on minimum quality values. Failed metrics execution aborts the process at whatever lifecycle phase they run in. Most code quality Maven plugins have default phases but teams may wish to move them based on how they value different phases. Code quality tools can be run early prior to completing major operations or they can be run late so that some of the standard operations complete/fail prior to getting into code quality analysis.
Static Analysis Phase Selection
Static analysis tools include Checkstyle and PMD. They can run prior to or after compilation. Some static analysis tools, like Findbugs, run against the compiled binaries. Those should be run after the compilation phase.
When should source code static analysis be run? It is really up to the team based on the priority order for failing a build.
- It can be run prior to compilation, possibly in the Validate phase. This lets a build skip a long compilation cycle when the code doesn't meet a minimum bar.
- It can be run after compilation, possibly in the Process Classes phase. This lets the analyzer only run against code that is known syntactically correct because the code is known to compile
Code Coverage Phase Selection
Code coverage tools work by instrumenting the source code or binaries prior to test execution. This means you wish to put the code coverage plugin last after other quality plugins, especially if you have generated code, JAXB, GWT or some other.
- Some teams just bind it to the Test phase. This has the advantage of only running the tests once. It has the disadvantage of subtly changing the code under tests as the code coverage instrumentation makes minor changes to the test binaries.
- Some teams bind it to the Verify phase. This has the advantages of failing the build prior to code coverage generation. It also has the advantage of executing the unit tests without any binary changes. Code coverage in the Verify phase has the downside of requiring the tests to be run twice, once for the test phase and once for the post-test phase.
Generating Reports in the Site Targets
Metrics are usually generated in the Build lifecycle. They are usually aggregated into web page reports as part of mvn site web sitegeneration cycle. It is almost mandatory that maven metrics reports require two different executions. This means metrics are generated in one pass and then formatted for viewing in another pass:
- mvn clean install
- mvn site
Profiles
Maven profiles let you change the plugins executed based on a profile name. You can use the default profile for normal developer builds and use some custom profile to execute different plugins.- Teams that execute quality metrics as part of all builds don't have a lot of work to do.
- Teams that execute policy metrics in only some special builds may wish to create a custom CI policy that only executes as part of the the CI build.
Comments
Post a Comment