Why Run Maven on Command Line?

Maven targets such as mvn clean install and mvn clean test site and even mvn site-deploy can be run

  1. in a Java IDE (like ItelliJ or Eclipse)
  2. in a Continuous Integration Server (like Jenkins or Continuum) or
  3. on a command line (like Shell or DOS)

Why use the command line – isn’t the IDE and the CI Server enough?

Why Run Maven3 on the Command Line

It is incredibly difficult to configure Maven, your IDE and your CI Server in one generic way. Each has their own plugins – none are united. The next section spells out the problems.

When the Maven build works (say) on IntelliJ but fails in (say) Jenkins – or the other way round – you need an intermediary.

Also the command line is the purest form of execution environment. Jenkins (any CI Server) can add dependencies so something works but then doesn’t work in production.

IDEs are notorious for giving your build a “helping hand” – over the cliff-edge. A build working in an IDE means nothing.

Sometimes developers forget to check in some artifact, java file, change or dependency. Your IDE does not complain. And some continuous integraion servers are configured to just pull down an update rather than the whole kit and caboodle. This often happens to save time and bandwidth when the codebase is large.

Maven Site, Javadocs, Cobertura, Surefire

It’s not easy creating a Maven site visible through the continuous integration process. It’s not easy getting the java documentation (javadoc) into a browsable location using the CI server.

Are you looking for a generic way to view the maven site, with surefire test results, and cobertura coverage?

Generic meaning it works on the IDE as well as the CI server.

Configuring all that is troublesome. You need this plugin for the IDE, that plugin for the CI server and if that wasn’t enough – you need to fight with the Maven POM to get the maven plugins configured correctly.

These 3 (Maven, IDE and CI Server) form an unholy alliance. Don’t fight them. Let them each do what they are good at.

Use Maven on the Command Line

Use the command line to create your maven project website. Use the command line to create the javadocs, the surefire reports, the code coverage and code quality reports.

This gives you the power to create the website where you like – Apache area, user home and so on.

Use the Command Line from a CI Server

Comand Line from CI Server sounds odd – it’s not.

Just put the commands below into a small shell script. Create a new CI Server (Jenkins) job to run the script periodically (daily, twice daily, weekly).

Now your maven site and all the goodies are created hassle free. You’ve bypassed the IDE and the CI Server – and you’ve made yourself (and your team) more agile, more productive, more responsive.

Running Maven on the Windows Command Line

To run a build on the windows command line – you go to a temporary folder and use this command. You can also add these commands to a script and give it to your CI Server to run. This way if failures occur your CI dashboard will let you know.

svn export https://www.build-business-websites.co.uk/assets/buildable/LaunderIt Launderette-20160606

This subversion (source control manager) command pulls down the software (plus artifacts) to be built. It does not create a working copy because we do not need to check anything back in.

Plain Old Maven

Look – a folder called Launderette-20160606 is now present. Go into it and run plain old maven with this command.

mvn clean test

[INFO] ------------------------------------------------------------------------
[INFO] Building The Data Laundering Framework 0.0.2
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ launderette ---
[INFO] Deleting C:\Users\Salina\asset-crate\consumable\build-v0.0.2\launderette-v0.0.2
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] Utilities for Enterprise Applications .............. SUCCESS [ 17.671 s]
[INFO] Envelopes Carry (Money) the Data to Launder ........ SUCCESS [  0.763 s]
[INFO] Launderettes are a Collection of Machines .......... SUCCESS [  2.704 s]
[INFO] Machines Must be Assembled to Fulfill a Purpose .... SUCCESS [  5.660 s]
[INFO] Transfer Data From Pillar to Post .................. SUCCESS [  0.064 s]
[INFO] The Data Laundering Framework ...................... SUCCESS [  0.004 s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 27.041 s
[INFO] Finished at: 2016-05-27T12:55:11+01:00
[INFO] Final Memory: 26M/187M
[INFO] ------------------------------------------------------------------------

If JUnit test failures occur the surefire report folders (or Site) will spill the beans.

Command Line Maven3 Summary

With a command line build you can apportion blame when of the two (IDE and CI Server) works, instead of both.

Creating maven sites, checking out test coverage reports with Cobertura, checking code quality with PMD, CheckStyle and so on is best done using the command line. (And you can cheat and get the CI Server to run your small script).

Get maven running on the command line, and then it’s lights out, and away you go!

Leave a Reply

Your email address will not be published. Required fields are marked *