Close

Maven Concepts and Kick Start Example

[Last Updated: Feb 12, 2017]

Maven Concepts

  1. Project Management: Maven is a Java project management tool for developers.
  2. Build tool: Performs all sorts of build tasks e.g. compilation, running tests, packaging into jar/war, deploying to a server etc
  3. Dependency Management: We do not have to download dependent jars, we just have to declare what dependencies we need. Maven downloads and uses them during any build task.
  4. Best practices build patterns: Maven is an attempt to apply build patterns to promote productivity and other charachteristics by the use of best practices.
  5. Convention over Configuration: Maven provides reasonable defaults without requiring unnecessary configuration. When a Maven project is created, Maven creates default project structure and build process like compile, test, package or clean steps. User can customize them though.
  6. Plugin-based Architecture: Maven consists of a core framework which provides basic project-processing capabilities and build-process management, and a host of plugins which are used to execute the actual build tasks. Some plugins are bundled with Maven distribution. We are free to install other non-bundled plugins provided by Maven or third parties. We can even write our own plugins. This is how Maven is expandable with new functionality

Installing Maven on Windows

Tools and versions used
  1. JDK 1.8
  2. Maven 3.0.4
  3. Windows 7

Steps

  1. Download Maven(In this example I downloaded apache-maven-3.0.4-bin.zip) in zip format from here and unzip it in a directory say straight to D:\ so you will get D:\apache-maven-3.0.4
  2. Go to Advance system settings in windows and open Environmental Variables dialog. Make sure JAVA_HOME system variable is set, if not then set it as JAVA_HOME=C:\Program Files\Java\jdk1.8.0_51
  3. Set another system variable M2_HOME=D:\apache-maven-3.0.4
  4. Add Maven bin folder to existing System variable Path=.....C:\Program Files\Java\jdk1.8.0_51\bin;%M2_HOME%\bin
  5. Open cmd and type mvn -version. You will get following output
    D:\>mvn -version
    Apache Maven 3.0.4 (r1232337; 2012-01-17 02:44:56-0600)
    Maven home: D:\apache-maven-3.0.4
    Java version: 1.8.0_65, vendor: Oracle Corporation
    Java home: D:\Java\jdk1.8.0_65\jre
    Default locale: en_US, platform encoding: Cp1252
    OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"

Creating First Maven Project

New projects can be built using existing templates, called Archetype. The projects can also be constructed manually but thanks to Archetype Plugin which makes life much easier. Let's use maven-archetype-quickstart which contains a sample Maven project.
Open command prompt and go the folder location you want to create the project and run this command:

mvn    archetype:generate   -DgroupId={ project-group-id}    - DartifactId={ project-name}    - DarchetypeArtifactId= maven-archetype-quickstart   - DinteractiveMode= false

Replace project-group-id and project-name with suitable values. I replaced them with com.logicbig and maven-first-example. And here we got our first maven project. By the way, don't worry about understanding the mvn command we used, everything will be explained later in other tutorials.

Dependencies and Technologies Used:

  • JUnit 3.8.1: JUnit is a regression testing framework written by Erich Gamma and Kent Beck. It is used by the developer who implements unit tests in Java.
  • JDK 1.8
  • Maven 3.0.4

Maven First Example Select All Download
  • maven-first-example
    • src
      • main
        • java
          • com
            • logicbig
      • test
        • java
          • com
            • logicbig
    • pom.xml

    Compiling and Running

    In the same folder where we created the project, run this commandObject to compile the project

    mvn  compile

    Maven will create a directory target which contains all compile classes, Jar files etc.

    There are various ways to Run the Java main method. We can go to the target directory and run it with standard java command. Another quick maven way is to use plugin Exec Maven Plugin. Just run this command in the root folder, maven will download the plugin(if it's the first time) and execute the main method

    mvn  exec:java -Dexec.mainClass="com.logicbig.App"

    It will generate this ouput:

    D:\maven-first-example>mvn exec:java -Dexec.mainClass="com.logicbig.App"
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building maven-first-example 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ maven-first-example ---
    Hello World2!
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2.304s
    [INFO] Finished at: Wed Oct 28 22:05:39 CDT 2015
    [INFO] Final Memory: 8M/85M
    [INFO] ------------------------------------------------------------------------

    OK, so far not a big deal!! But now we know how to start a project with Maven. We will see in upcoming tutorials the real power of Maven when we will use external dependencies and more.

    Understanding Maven Directory Structure

    Maven has a Standard Directory Structure In our example, we saw App.java is under src/main/java. So our Java src files will always go there. That's one of the maven defaults. We can change the src directory but we have to override the default. How can we do that? There comes pom.xml. All default configuration can be overridden there. pom.xml is the heart of the maven project. This is the only mandatory file to run any mvn command. The standard directory structure is not defined by a specific Archetype. Archetype implementation have to follow Maven standard directory structure or they are also free to override the defaults. Archetype plugins actually are there to automate the project creation. In our example we saw AppTest.java is under src/test/java which is another default for test source files. There are more default directories not created by the Archetype we used. For example src/main/resources. If we need to use some resources e.g. properties file, we can extend our project and add resources directory manually or we can look for some other Archetype plugin.


    Importing Maven project into IDE

    Maven is not dependent on any IDE but each major IDE provides Maven support. For example you can import the same project into Intellij(File->New->Project from Existing sources..) or even you can create the new Maven project from within Intellij. One of advantages of using an IDE is, we can execute java main methods directly from IDE without running maven command, in those cases the IDE is responsible to resolve dependencies etc defined in pom.xml
    In our examples we won't be focusing on any IDE. The idea is we should learn new concepts in the most generic/original way so that we will be able to work in all sort of environments.

    See Also