Maven: What is its purpose? How to install it? Examples

What is its purpose?

Apache Maven is a built automation tool used normally for Java projects. Maven has a XML file called pom.xml (Project Object Model), and that file describes the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins. The nice is that Maven dynamically downloads Java libraries and Maven plug-ins from several repositories, and stores them in a local cache.

In order to work, Maven can create your project quickly with a simple command since the sell (Linux/Mac/…) or cmd (Windows).

The directory structure for a Java project is very simple:

  • Project Home:
    • src
      • main
        • java
        • resources
      • test
        • java
        • resources
    • target
    • pom.xml

A project can be generate with Maven in any directory of your computer (only it is necessary configurate the “Environment Variables” of the system).

In addiction, you can use Maven with the more powerful IDE in order to have more posibilities: Eclipse, NetBeans, IntelliJ… Maven has a plugin for Eclipse in order to allow using Maven form this IDE avoiding the use of the Maven command line interface, and NetBeans includes full Maven support.

For example, I normally generate the project with Maven, add new libraries / plug-ins in the pom.xml file, and work with Eclipse for edit the code. In order to compile the project, you can use Maven and Eclipse separalety. This is an example of a pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jmsimo</groupId>
  <artifactId>2014.01.07.1400</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>2014.01.07.1400</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongo-java-driver</artifactId>
      <version>2.10.1</version>
    </dependency>

    <dependency>
      <groupId>com.sparkjava</groupId>
      <artifactId>spark-core</artifactId>
      <version>1.1.1</version>
    </dependency>

    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.19</version>
    </dependency>
  </dependencies>
</project>

Note: For the curious, this code is of a project that is using MongoDB, Spark Java and Freemarker.

In my opinion, if you never have worked with Maven, you should try to use it :)

How to install it?

Check Java version.

Are you using JDK 1.7? In order to check it: https://josemmsimo.wordpress.com/2014/01/14/how-to-install-java-jdk-and-jre-in-linux-and-windows-with-details/

Download and install Maven: http://maven.apache.org/download.cgi

download maven

Use the link of one of the binary files.

Once downloaded, unzip the file, and put the folder in a fixed directory (you don´t have to change the place of this folder in the future, for example, in Windows you could put the folder in C:\ ).

Now, you need add the “bin” folder to the “Environment variables”. For example, working with Windows, you could have this path: C:\apache-maven-3.2.1\bin

Note: If you don’t know how add this path to the “environment variables”, review the link for install the JDK 1.7 (in one step, it is configurated the path for Java, so that do the same for Maven).

Creating a new Maven project

And here starts the good :)

Using cmd / shell, go to the folder where you want create the project. In Windows:

  • Start / Run, and type cmd (for open the cmd.exe), and click there.
  • Once with the terminal, type: mvn archetype:generate
  • It will generate several lines. At the end (maybe in 5 or 10 seconds), the system will ask: “Choose a number or apply filter…” with a number. Click enter for use the default.
  • Later it will ask the version for use. Click enter for default (the last one).
  • Later it will ask the groupId. Type, for example: com.myfirstmaven
  • Later it will ask the artifactId for the class. Type, for example: MFM
  • Later it will ask the version. Click enter for default.
  • Later it will ask the default java packet. Type, for example: com.mypackage
  • Later it will ask for confirm. Click enter for ok.

mavenCMD

Ok, now we have a project created with an excelent structure in the folder C:\Users\j (in my case).

That project is (in my case) in: C:\Users\j\MFM, where you could see:

  • src
    • main
      • java
        • com
          • mypackage
            • App.java
    • test
      • java
        • com
          • mypackage
            • AppTest.java
  • pom.xml

The App.java file has only the typicall “Hello world”.

For compile and execute it, type: mvn compile exec:java -Dexec.mainClass=”com.mypackage.App”

mavenCompile

Ok, it was pretty simple. The current pom.xml is the basic one.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

<groupId>com.myfirstmaven</groupId>
 <artifactId>MFM</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>

<name>MFM</name>
 <url>http://maven.apache.org</url>

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

<dependencies>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
 </dependency>
</dependencies>
</project>

Remember that you can use Eclipse, IntelliJ or other in order to edit. For compile, with Eclipse for example, you could compile the file Api.java indendently without problems, so that since now, you could use the IDE where you are more comfortable.

Adding new libraries

This is my favorite part. For add dependencies, you don’t need download new libraries. Maven will do it! For do it, you only need load a new dependency in the pom.xml file (you can check the first pom.xml file with JUnit, MongoDB and SparkJava and FreeMarker). In the last pom.xml file, I have the JUnit by default. For add the MongoDB, the final result will be so:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.myfirstmaven</groupId>
<artifactId>MFM</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>MFM</name>
<url>http://maven.apache.org</url>

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
 </dependency>
 <dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongo-java-driver</artifactId>
  <version>2.10.1</version>
 </dependency>
</dependencies>
</project>

Links

Leave a comment

Por favor, inicia sesión con uno de estos métodos para publicar tu comentario:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s