Getting Started With Maven
Any project development comprises of various build processes that are often repetitive and involves lot of manual activities. Such activities involves downloading dependencies, source code compilation, packaging, testing, deployment and generating documentation etc. This may be ok for small projects but becomes tedious for large projects to keep track of each build processes. This is where build automation or build tools comes handy. Popular build tools for java are Ant, Maven, Gradle etc. Today I am going to talk about Maven, one of the most widely used build tool or project management tool in java ecosystem. We will see what is maven, its key features, download and install maven tool on windows etc. Let’s getting started with Maven – Part 1.
Introduction to Apache Maven
Apache Maven is an advanced build tool to support the developer at the whole process of a software project. Maven acts as both a dependency management tool – it can be used to retrieve jars from a central repository or from a repository you set up – and as a declarative build tool. Maven allows the developer to automate the process of the creation of the initial folder structure for the Java application, performing the compilation, testing, packaging and deployment of the final product. It is implemented in Java which makes it platform-independent.
Maven relies on conventions about how project directories are laid out in order to achieve its “declarativeness.” For example, it has a convention for where to put your main code, where to put your web.xml, your unit tests, and so on, but also gives the ability to change them if you need to.
The real out-of-box strength of Maven is its dependency management. You only have to declare the dependencies and Maven will download them, setup the classpath for you, and even deploy the dependencies with your application, if required.
Apache Ant versus Apache Maven
- Ant doesn’t have formal conventions. You have to tell Ant exactly where to find the source, where to put the outputs, etc.
- Ant is procedural. You have to tell Ant exactly what to do; tell it to compile, copy, then compress, etc.
- Ant doesn’t have a lifecycle.
- Maven uses conventions. It knows where your source code is automatically, as long as you follow these conventions. You don’t need to tell Maven where it is.
- Maven is declarative; all you have to do is create a pom.xml file and put your source in the default directory. Maven will take care of the rest.
- Maven has a lifecycle. You simply call mvn install and a series of sequence steps are executed.
- Maven has intelligence about common project tasks. To run tests, simple execute mvn test, as long as the files are in the default location. In Ant, you would first have to JUnit JAR file is, then create a classpath that includes the JUnit JAR, then tell Ant where it should look for test source code, write a goal that compiles the test source and then finally execute the unit tests with JUnit.
Key features of Maven
Maven tries to avoid as much configuration as possible, by choosing real world default values and supplying project templates (archetypes).
- Can download jar files dynamically
- Can maintain multiple repositories having jar files, plugins and etc..
- Provides standard project directory structures
- Gives Maven inheritance to share jar files and plugin among the multiple projects
- Allows to develop multiple module projects
- Can generate jar, war, ear and etc.. packaging based components or Application
- Can run unit tests and can generate unit test reports
- Can generate project documentation
- Can clean and install the projects in the local servers or remote servers
Downloading and Installing Maven on Windows
The Maven project is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project. In case you want to use Maven from the command line, you need to install the Maven command line support. For this you just need to download the Maven’s zip file, and extract it to any directory of your choice, and configure the Windows environment variables.
- To download the maven as a zip file, visit official website: http://maven.apache.org/download.cgi and click the given link apache-maven-3.5.3-bin.zip as shown in the image below to begin the download and save it to any preferred folder of your choice.
- Before configuring maven make sure that JDK is installed in your system and set JAVA_HOME variable to java installation folder.
(make sure there is no semi-colon at the end)
- Now I suppose you have downloaded and unzipped maven software. I have extracted the maven zip file in the D:\MavenTuts
- Add maven bin folder to PATH Variable
Value: <Maven_Home>\bin;<existing values>;
In my case D:\MavenTuts\apache-maven-3.5.3-bin\apache-maven-3.5.3\bin;
- To verify if maven has been configured successfully, invoke mvn –version (or mvn –v or mvn –version) in the command prompt
If you get the message as shown above, it means you have successfully installed and configured Apache Maven in your windows machine. Cheers