Apache Maven – Introduction

In this article, we will learn and understand about Apache Maven – a build tool. In general, it simplifies the whole build process from validating/compiling to installing/deploying into any configured application server

Maven build tool can be extended via plug-ins

Key features of Apache Maven:

  1. It’s a build tool
  2. Simplifies the build process
  3. Resolves dependencies
  4. Takes cares from validating to installing/deploying –> Lifecycle of Maven
  5. Packages build as JAR/WAR/EAR –> standard Java/JEE packaging
  6. Runs unit tests and integration-tests
  7. Problems without Maven (i.e.; advantage of using Maven)
  8. Generates documents (i.e.; javadoc) from source code
  9. Helps to create sites, reporting and documentation
  10. Follows standard project structure (or folder structure)

1. Build tool

  • Apache Maven is a popular open source build framework that can be used to build the project in various well-known platforms. But it is highly adapted in Java/JEE platform to build project
  • Not to limit saying Maven is build tool, yes of course but it got more advantageous features like resolving dependencies and from packaging to deploying into app servers

2. Simplifies build process

  • In earlier project development categorically speaking about Java/JEE developments, developers has to resolve dependencies manually and also execute/run commands from CLI to package as JAR/WAR/EAR
  • With packaging ready to be deployed in app server, configuration management team or someone handling deployment part has to separately deploy into app server
  • With Maven, all these can be pre-configured in a xml file called pom.xml and execute/run maven commands thereby simplifying the build process

3. Resolving dependencies

  • One of big advantage of using Maven is it resolves dependencies. It’s like we have to configure required dependencies in pom.xml lying at the root directory of the project
  • Most of the Java/JEE developers uses Maven to resolve the dependencies, which download the required artifacts (dependencies) and adds directly to the class-path of the project
  • Not only that, it also downloads dependency’s dependencies which is quite cumbersome to figure out keeping version in tact in pre-Maven era

4. Packaging

  • In Maven build lifecycle, one of the phases is to package the compiled source code into deployable artifacts like WAR or EAR
  • In earlier conventional way, developers has to run separately command from CLI to package this artifact i.e.; to package as JAR or WAR, etc.

5. Testing (via test framework)

  • As we are looking at the features of the Maven, one important core features is it to run/execute test cases configured via some popular test frameworks like JUnit
  • In Maven build lifecycle before packaging it executes unit tests and integration-tests. This make sure packaged artifacts are indeed tested and there is no break in the compiled source code
  • Note: If test cases fail, then Maven build fails stating the reasons

6. Lifecycle of Maven (validating …… installing/deploying)

  • Maven follows standard lifecycle which consists of various build phases. So, when developers execute/run “install” command then all its preceding steps (i.e.; build phases) gets execute
  • Assume we are executing “install” command
  • Starting from validating, compiling, testing using test framework, packaging artifacts as JAR/WAR are all gets executed till it goes to installing newly created artifact into local maven repository
  • But, if “deploy” command is executed then its deploys to app server provided app server configuration settings are done in pom.xml

7. Problems without Maven (or Advantages of using Maven)

All above points dictates that build process made easy with the use of Maven. But assume if we aren’t using Maven, then all these tasks has to be done manually like

  • Manually downloading all required JARs
  • Adding these JARs to project’s classpath
  • Compiling source code
  • Individually writing and executing various test cases
  • Packaging from command line
  • Deploying to app server

In addition to all these points, if in future projectA is required as dependency to some other projectB –> then in that case, developer has to manually browse to projectA and add to projectB as dependency

8. Generates documents (javadoc)

  • Maven can generate javadocs for any specific project using javadoc plugin
  • The Javadoc Plugin gets the parameter values that will be used from the plugin configuration specified in the pom.xml

9. Site plugin

  • With Maven command “mvn site”, it generates documentation site and it contains all required project information. This documentation site available under “target” folder
  • This site can be deployed to app server and can read project information from web browser

10. Standard project structure (folder structure)

  • Maven follows standard project structure segregating different source code and configuration files under different folders
  • This standard directory convention helps the new entrants/developer to understand the project information in very quick time, provided new developer previously worked in Maven based Java/JEE projects
  • Note: It can be tweaked depending on our business requirements or some other specific needs, but it is recommended to follow the convention

Standard Maven project/directory structure illustrated in below figure and its explanation

  • src/test/java –> test related files, mostly JUnit test cases
  • src/main/java –> create java source files under this folder
  • generated/java/source –> generated java source files are placed here
  • src/main/resources –> all configuration files placed here
  • src/test/resources –> all test related configuration files placed here
  • Maven Dependencies or Referenced Libraries –> includes jars in the classpath
  • WEB-INF under webapp –> stores web.xml & other configuration files related to web application
  • target –> contains deployable package like WAR or JAR
1_Apache_Maven_Intro_Sample_Project_Structure

Sample project –> to exhibit Maven’s project structure

Source: Team BenchResources.net

Useful Eclipse IDE shortcuts :

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Apache Maven - Install on Windows 7 OS