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:
- It’s a build tool
- Simplifies the build process
- Resolves dependencies
- Takes cares from validating to installing/deploying –> Lifecycle of Maven
- Packages build as JAR/WAR/EAR –> standard Java/JEE packaging
- Runs unit tests and integration-tests
- Problems without Maven (i.e.; advantage of using Maven)
- Generates documents (i.e.; javadoc) from source code
- Helps to create sites, reporting and documentation
- 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
Sample project –> to exhibit Maven’s project structure
Source: Team BenchResources.net
Useful Eclipse IDE shortcuts :
- Eclipse IDE – How to show line numbers ?
- Eclipse IDE – How to GO TO any line number directly ?
- Eclipse IDE – How to remove unused imports ?
- Eclipse IDE – How to clean project ?
- Eclipse IDE – How to build Java project automatically ?
- Eclipse IDE – How to comment and un-comment line & block ?
- Eclipse IDE – How to generate constructor using fields ?
- Eclipse IDE – How to generate getters and setters ?
- Eclipse IDE – How to search files ?
- Eclipse IDE – How to locate methods in Java file ?
- Eclipse IDE – How to open editor using CTRL + E ?
- Eclipse IDE – Java compiler compliance level issue
Related Articles:
- Apache Maven – Introduction
- Apache Maven – Install on Windows 7 OS
- Apache Maven – Settings.xml explanation
- Apache Maven – Proxy setting explanation
- Apache Maven – pom.xml explanation
- Apache Maven – Plugins explanation
- Apache Maven – Changing default Maven repository location in Windows 7 OS
- Apache Maven – Local, Central and Remote Repositories
- Apache Maven – Installing custom library into local repository
- Apache Maven – Transitive dependencies explanation
- Apache Maven – Exclusion of Transitive dependencies
- Apache Maven – Dependency Scopes
- Apache Maven – Skipping unit test using surefire plugin
- Apache Maven – Exclusions and Inclusions of unit test
- Apache Maven – offline execution
- Apache Maven – Co-ordinates explained
- Eclipse + Maven – Integration
- Eclipse + Maven – How to import Maven project with pom.xml ?
- Eclipse + Maven – Setting M2_REPO classpath variable in IDE
- Eclipse + Maven – M2_REPO is Non Modifiable
- Eclipse + Maven – Creating and exploring projects using archetypes
- Eclipse + Maven – Converting Web project to Maven project
- Eclipse + Maven – mvn eclipse:eclipse command
- Eclipse + Maven – Plugin execution not covered by lifecycle configuration
References:
- http://maven.apache.org/ref/3.1.0/
- http://maven.apache.org/what-is-maven.html
- http://maven.apache.org/plugins/maven-javadoc-plugin/
Happy Coding !!
Happy Learning !!