5

In the build lifecycle in maven, I encountered the validate phase described as validate the project is correct and all necessary information is available to complete a build
1. what are the check points for a project to be correct?
2. what are these necessary information that needs to be available in completing a build?
Can you provide example for both?

1 Answer 1

6

Short Answer

  1. It's up to you
  2. It's up to you

Long Answer

Why? Because by default (ie. on a clean, empty project with no user-defined plugin goals), mvn validate does nothing.

Why? A build phase is comprised of plugin goals. No goals for a phase? The phase does nothing.

From the the Build Lifecycle Basics section of the Introduction to the Build Lifecycle guide:

Furthermore, a build phase can also have zero or more goals bound to it. If a build phase has no goals bound to it, that build phase will not execute. But if it has one or more goals bound to it, it will execute all those goals

This means that unless you declare a plugin goal that is bound to the validate phase, the validate phase will do nothing.

The definition that you've found isn't saying that something will happen during that build phase, but rather if you want an action to occur in that phase (ie. step) of the build lifecycle, then that is the phase to bind the goal to.

validate the project is correct and all necessary information is available to complete a build

This is a guideline for how to organize your plugins so that they execute efficiently and in the expected order, at the expected time.

For example, say we have a new, empty Maven project with no build plugins defined. If we were to run mvn verify, Maven will click through each build phase up to and including the verify phase. We might expect the output to be empty, however there are default Built-in Lifecycle Bindings, so our output will include all default goals:

[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ MavenTest ---
...
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ MavenTest ---
...
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ MavenTest ---
...
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ MavenTest ---
...
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ MavenTest ---

For you, a metric of a "correct" project may be that the styling of the code meets some criteria, so say we add the maven-checkstyle-plugin to our otherwise empty Maven project:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.17</version>
    <executions>
        <execution>
            <id>verify-style</id>
            <phase>validate</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

...and then re-run mvn verify:

[INFO] --- maven-checkstyle-plugin:2.17:check (verify-style) @ MavenTest ---
...
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ MavenTest ---
...
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ MavenTest ---
...
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ MavenTest ---
...
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ MavenTest ---
...
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ MavenTest ---

Because we have declared a plugin and told it to execute as part of the validate phase, the validate phase will now actually do something.

To further illustrate the point, we could remove the plugin again and run mvn validate, and see that there is nothing that happens, because there are no default bindings for the validate phase:

C:\workspace\MavenTest>mvn validate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenTest 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.078s
[INFO] Finished at: Thu Jun 30 11:56:54 CDT 2016
[INFO] Final Memory: 2M/15M
[INFO] ------------------------------------------------------------------------

As for an example of checking that "necessary information" is available, one situation may be validating that certain config files in a project have certain information. Again, that is really up to you based on your use case.

1
  • splendid explanation, thanks; it is based upon your configuration what those criteria would mean
    – Dee
    Commented Jul 1, 2016 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.