Maven: Relationship between Lifecycle Phase & Goal - apache

I'm having a tough time seeing the "forest through the trees" on some Maven concepts here. I understand that Maven comes preconfigured with a slew of so-called "Build Lifecycle Phases" that begin with validate, and test and end with deploy.
I am coming to Maven from Ant where you organized major build stages into targets (which can depend on other targets in a linear fashion), and then you decompose your targets into procedural tasks. Maven seems to supports this but in the form of goals.
My question (since I am used to thinking of building in terms of targets and tasks) is: how do these lifecycle phases (package, verify, etc.) relate to goals? Does one need to configured which goals to run at which phase, or is this done by Maven automagically somehow?
Or, do the goals come predefined with which phase they belong in?
I'm just not seeing how one orders goals to create a custom build that works for them or their organization.
Thanks in advance for any clarity!

Many Maven plugins bind specific goal(mojo) to a specific lifecycle phase.
E.g. look for documentation of maven-jar-plugin:jar goal. It says:
Binds by default to the lifecycle phase: package.
Also, lifecycle itself is specifying what tools need to be run for each phase. For default packaging types these tools are predefined by Maven and explained here -> http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings.
But this example shows how to create totally custom lifecycle binding -> http://www.sonatype.com/people/2009/08/create-a-customized-build-process-in-maven/. As you can see you can bind any combination of plugin goals to a specific lifecycle phase.
Last, but not least, you can add plugin execution manually through the build/plugins element of your pom file. If you do that the plugins in specific phases will execute in a FIFO order.
Also, it may help you to see the Effective POM with all these defaults spelled out. Many IDE's offer this option for Maven projects, but you can also see effective pom from the command line:
mvn help:effective-pom

Maven has 3 lifecycles which are inturn executed in many phases (package, verify, etc). When these phases are executed, it calls the previous phases as well.
Goals are individual tasks. When it is called, that particular task is executed.
Check the below blog for more detailed explanation.
https://techytopics.wordpress.com/maven-simplified/

Related

How do you perform a reactor build in Maven3 without a root project?

How does one perform an ad-hoc reactor build over multiple maven projects without a root project?
This used to be possible in maven2 by invoking the mvn command with the '-r' option which would search recursively from the working directory for projects and organize them in the reactor to build in the correct order even if the projects did not share a common parent pom.xml. Optionally one could specifically include or exclude certain projects using maven.reactor.includes and maven.reactor.excludes option.
I realize that the legacy reactor mode was removed from maven3 but I can't figure out how to achieve this functionality using the 'make-like' reactor mode.
The "Maven 2"-style reactor feature has been removed following this discussion on the mailing list. The relevant JIRA issue is MNG-4260.
As mentioned in the comments above, it's best to create a top-level pom with a <modules> section that lists the directories containing projects. (If you don't always build all of your projects at the same time, you can put separate modules sections in profiles).

How can I tell Hudson to build the modules instead of the jobs?

I have a alot of jobs on Hudson, most of which are really small and consist of just a few modules. But one is big and consist of several modules.
When ever I make a commit to our subversion repository for any of those several modules in that big job, Hudson builds the entire job instead of just the module that have changed.
It doesn't matter if I just scm-polling or a subversion hook, the result is the same.
It seems to me like it would be better if the modules where built instead of the jobs since the other modules in other jobs have dependencies to the modules and not to the jobs.
Can this be configured or do I have to create several jobs instead of the big one? And if so, can I configure the big job to never build when any of it's modules are being triggered but still build when it's own pom.xml is changed?
Thanks.
Hudson has an "Incremental Build" option in the Maven area of the job configuration.
It's hidden in the "Advanced" area.
You could make use of the reactor plugin. For example:
mvn reactor:make-scm-changes
This will only build those modules that have been changed in the SCM. Follow the link for other examples.
Doesn't your compiler offers you the incremental compile option? The java 1.6 compiler usually searches for class and source files and decides using the timestamp to determine whether to use the source or class file. Just leave out the clean goal when building your code.
Another option would be to first run a batch/shell script to determine what files changed and delete the corresponding class files so that the compiler incrementally builds the class files that are missing.

Setting up a standard directory layout using Maven

I'm new to Maven and have skimmed over the documentation as I am following the Hibernate tutorial at http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#tutorial-firstapp-mvn.
I have installed Maven and successfully setup a web-app but this does not contain all of the standard directories mentioned in the tutorial. Am I going mad?
When building my Maven project I am using the maven-archetype-webapp. This gives me the arh-webapp\src\main\resources and arh-webapp\src\main\webapp directories but I'm missing quite a few directories mentioned on the link http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html.
Surely I don't have to manually add these? If not then the Hibernate documentation does not mention what archetype to use in order to achieve the directory structure used in their tutorial. Please can someone enlighten me.
What archetype do I need to use in order to have the above directory plus the src/main/java directory? If there is no such archetype then can easily append these using Maven? and how?
Surely you'll have to manually add these.
Just create those directories that according to the Maven convention are missing. Remember, a Maven Archetype is just a starting point to save you time configuring your workspace. After encountering many problems in some Archetypes myself I've been accustomed to just use a basic-web-app-archetype and then customize it myself, as a beginner with Maven you'll be better off doing that, and will learn a lot.
Regards.
Not all the directories mentioned are required for your standard web application. In fact, the reason behind the presence of the src/main/java, src/main/resources and the src/main/webapp directories is due to the archetype that you've used.
IMHO, the book titled "Better Builds with Maven" will serve you better; the Sonatype books on Maven might also help. The complete Maven documentation is also available as a PDF file, for future reference.
But just in case, you need some clarity on the terms used, here's some:
Archetype: A pattern for projects. Simple web applications (with no dependencies on other frameworks/libraries) have their own archetypes, so do applications using Spring, Hibernate, Seam, etc. Some archetypes will result in creation of different directories, as they are coded that way. You might be able to change the directory structures in most cases, although I cannot vouch for every archetype. For instance, it is quite possible to place your sources in 'src' instead of 'src/main/java', although this requires additional configuration in the POM.
Lifecycles, Phases and Goals: A Maven build lifecycle is a series of phases, with each phase executing a set of goals. Maven can be commanded to execute a build phase, which results in execution of all phases until and including the specified phase.
Maven plugins: Maven plugins contain one or more goals. Goals need not be bound to phases, but usually you would bind them to particular phases. Plugins are the basis for everything operational in Maven; you're using plugins even though you are just compiling the application (the Maven compiler plugin is a core plugin that is present in the Maven distribution).
I hope the above helps, but I would suggest that the reference books be followed.

Maven multi-module project with many reports: looking for an example

Is there an open source project that can serve as a good example on how to use the maven site plugin to generate reports? I would prefer it to
consist of many modules, possibly hierarchically structured
use as many plugins as possible (surefire, jxr, pmd, findbugs, javadoc, checkstyle, you name it)
the reports should be aggregated: if some tests fail you want to have a single page that shows all modules with failing tests, not only a gazillion individual pages to check
include enterprisey stuff (WAR, EAR etc), but this is not so important.
The idea is to have something where you can gather ideas on how it is done and what is possible.
I gave up trying to aggregate reports of a complex multi-modules project with the maven-site-plugin. For this, I use Sonar, it's much more powerful (with features like evolution of metrics over time, aggregation, neat drill down, etc) and just works. Have a look at Nemo, the online demo instance and cry.
For an example see http://www.bartswennenhuis.nl/2013/12/maven-aggregate-reports-for-multi-module-projects/. Findbugs does not support aggregate reports.
I don't think there is such a project, if there is I want to know it as well. In order to find things in maven you have to know what you're looking for(which is not exactly the same with what you want to accomplish).
If its any help I'm building 13 module project with MAVEN, use cobertura maven plugin, surefire, javadoc, etc .. it works as charm, why are you asking this question, you want to determine the capabilities of maven or ?
this is actually a response to your question. please take a look at the Apache Directory project. it contains two big blocks: the directory server and the tooling support (Eclipse based).
you can find the SVN repository of the Apache Directory Studio (this is a complete directory tooling platform intended to be used with any LDAP server) here: http://svn.apache.org/repos/asf/directory/studio/trunk/
take a look at the POM file ( http://svn.apache.org/repos/asf/directory/studio/trunk/pom.xml ) of this multi module project. it consists out of lots of modules, uses most of the plug-ins you're using and it also aggregates some of the reports.this
You can use Violations Maven Plugin to aggregate Findbugs (and many other static code analysis) reports.
It needs to run after the analysis. It will parse their report-files and present them in one unified report. It can, optionally, fail the build depending on number of violations found.

How to display a list of available goals?

I'm rather new to Maven and I often find myself wanting to see what's actually there in terms of goals.
So, is there a command which lists all available goals for e.g. a given prefix?
Since Maven is an open system of plugins, the best answer is probably "Google" ;-). If you mean all build lifecycle phases, they are static, and can be found at http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html and at other places.
Then, for a given plugin, the help plugin can be used to get the possible goals and all their parameters:
mvn help:describe -DgroupId=org.apache.maven.plugins \
-DartifactId=maven-war-plugin \
-Ddetail=true
But this doesn't really answer your question, especially the "for a given prefix" part. For this, the best solution might be to use **auto completion with BASH (**not sure it will be exhaustive though). See for example the Guide to Maven 2.x auto completion using BASH. To get bash completion working under Windows, you'll need CYGWIN. See Maven Tab Auto Completion in Bash for detailed setup steps (and a "better" working auto completion script).
A shorter way
As an alternative, you can also use the -Dplugin parameter to display the list of available goals.
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-war-plugin\
-Ddetail=true
See Maven help plugin.
More and more Maven plugins propose an help goal as alternative to the verbose
mvn help:describe command.
You can read from the Maven doc:
Recent Maven plugins have generally an help goal to have in the
command line the description of the plugin, with their parameters and
types
That is really more natural and pleasant to use.
It works of course for Maven core plugins.
Some examples :
to list goals of the dependency plugin :
mvn dependency:help
to have detail about the javadoc goal of the javadoc plugin :
mvn javadoc:help -Ddetail -Dgoal=javadoc
And it works also for third party plugins.
For example, to list goals of the spring-boot-maven-plugin :
mvn org.springframework.boot:spring-boot:help
[INFO] Spring Boot Maven Plugin 2.0.0.RELEASE Spring Boot Maven
Plugin
This plugin has 6 goals:
spring-boot:build-info
Generate a build-info.properties file based the content of the
current MavenProject.
spring-boot:help
Display help information on spring-boot-maven-plugin. Call mvn
spring-boot:help -Ddetail=true -Dgoal= to display
parameter details.
spring-boot:repackage
Repackages existing JAR and WAR archives so that they can be
executed from the command line using java -jar. With layout=NONE can
also be used simply to package a JAR with nested dependencies (and
no main class, so not executable).
.....
Or to get detailed information about the build goal of the dockerfile-maven-plugin :
mvn com.spotify:dockerfile-maven-plugin:help -Ddetail -Dgoal=build
[INFO] Dockerfile Maven Plugin 1.3.6
Adds support for building Dockerfiles in Maven
dockerfile:build
Available parameters:
- archive
The archive configuration to use for the Docker info JAR. This can be used
to embed additional information in the JAR.
....
You could note that the syntax to get a detailed output of the help and to focus on a specific goal (-Ddetail -Dgoal=myGoal) is exactly the same as this used for the core maven plugins.
Of course some esoteric plugins may not provide the help goal but in most of well designed plugins this is present.