BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Apache Maven 3 : Installing and creating a project - 2020

Duke 512




Bookmark and Share





bogotobogo.com site search:




Maven 3 Setup

This will be very brief tutorial. We'll install Maven 3 plugin and then create a Maven Project on Ubuntu 14.04.

Let's download the Maven from Download Apache Maven 3.2.5:

Maven325.png


Here are the downloaded Maven files:

apache-maven-325-folder.png Maven_Tree_View.png



Setting environment variables - m2home

We need to set environment variables. First one is M2_HOME:

$ export M2_HOME=/home/k/java/apache-maven-3.2.5

Then, the path to bin so that we can execute the several commands in the bin folder:

$ export PATH=/home/k/java/apache-maven-3.2.5/bin:${PATH}





Simple test

To check the version of Maven:

$ mvn --version
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T09:29:23-08:00)
Maven home: /home/k/java/apache-maven-3.2.5
Java version: 1.7.0_65, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-40-generic", arch: "amd64", family: "unix"




Creating a project using Archetype

An archetype is a very simple artifact, that contains the project prototype we wish to create. To create a project, we'll use mvn archetype:generate command. In other words, we're creating a project from an existing template.

When we run the command, maven does following things (maven archetype:generate):

  1. Downloads maven-archetype-plugin's latest version.
  2. Lists all archetype's that can be used to create a project from. If we defined an archetype while calling the command, maven jumps to step 4.
  3. By default, maven chooses maven-archetype-quickstart archetype which basically creates a maven Hello World project with source and test classes. If we want to create a simple project, we can just press enter to continue. If we want to create a specific type of application, we should find the archetype matching our needs and enter the number of that archetype, then press enter.
  4. Since archetypes are templates and they intend to reflect current best practices, they can evolve in time, thus they have their own versions. Maven will ask us which version of the archetype we want to use. By default, maven chooses latest version for us. So, if we agree to use the latest version of an archetype, we just press Enter.
  5. Every maven project (and module) has its groupId, artifactId and version:
    1. groupId: Identifier of the organization or group that is responsible for this project. For exmple, org.hibernate, log4j, and org.springframework.boot.
    2. artifactId: Identifier of the artifact being generated by the project. This must be unique among the projects using the same groupId. For example, hibernate-tools, log4j, spring-core, and so on.
    3. version: Indicates the version number of the project. For example, 1.0.0, 2.3.1-SNAPSHOT, and 4.3.6.Final.
  6. Finally, maven will ask us the package structure for our code. A best practice is to create our folder structure that reflects the groupId, thus Maven sets this as default.




mvn archetype

Now that we learned what is the mvn archetype, let's run the command:

First we may want to make a folder myapp:

$ mkdir myapp
$ cd myapp

Then, issue the command.

$ mvn archetype:generate
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
...
1240: remote -> us.fatehi:schemacrawler-archetype-plugin-dbconnector (-)
1241: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 523: 
Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:

So, far we chose default and the latest version which is 1.1.

Then, it will ask couple of properties such as groupId, artifactId, and version:

Define value for property 'groupId': : com.k.bogotobogo
Define value for property 'artifactId': : MavenTestApp
version: 1.0-SNAPSHOT
package: com.k.bogotobogo
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.k.bogotobogo
[INFO] Parameter: packageName, Value: com.k.bogotobogo
[INFO] Parameter: package, Value: com.k.bogotobogo
[INFO] Parameter: artifactId, Value: MavenTestApp
[INFO] Parameter: basedir, Value: /home/k/java/apache-maven-3.2.5/bin/myapp
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/k/java/apache-maven-3.2.5/bin/myapp/MavenTestApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3:24.271s
[INFO] Finished at: Sat Jan 31 20:40:16 PST 2015
[INFO] Final Memory: 18M/164M
[INFO] ------------------------------------------------------------------------




App directory

Now that Maven's done, let's see what's in the newly created app directory:

k@laptop:~/java/apache-maven-3.2.5/bin/myapp$ ls
MavenTestApp
k@laptop:~/java/apache-maven-3.2.5/bin/myapp$ cd ..
k@laptop:~/java/apache-maven-3.2.5/bin$ ls
m2.conf  mvn  mvn.bat  mvnDebug  mvnDebug.bat  mvnyjp  myapp

myapp_folder.png



POM

As we saw in the previous section, under MavenTestApp we have pom.xml file.

pom stands for Project Object Model. It is the basic unit in Maven. It always resides in the base directory of the project as pom.xml. The pom contains information about the project and various configuration detail used by Maven to build the project(s) as well as the goals and plugins. While executing a task or goal, Maven looks for the pom in the current directory. It reads the pom, gets the configuration information, then executes the goal.

Here is our new pom 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.k.bogotobogo</groupId>
  <artifactId>MavenTestApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenTestApp</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>

The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way we want. The pom is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively.

Note that the pom has also the dependency section. In our simple case, it only depends on junit 3.8.


Dependencies declared in our project's pom.xml file often have their own dependencies called transitive dependencies.

The picture below provides an example of transitive dependencies. Notice that transitive dependencies can have their own dependencies. As we might imagine, this can quickly get complex, especially when multiple direct dependencies pull different versions of the same JAR file.

Dependencies-Transitive.png

Picture source : Introducing Maven

Maven uses a technique known as dependency mediation to resolve version conflicts.

Simply stated, dependency mediation allows Maven to pull the dependency that is closest to the project in the tree. In the figure, there are two versions of dependency B: 0.0.8 and 1.0.0. In this scenario, version 0.0.8 of dependency B is included in the project, because it is a direct dependency and closest to the tree. Now look at the three versions of dependency F: 0.1.3, 1.0.0, and 2.2.0. All three dependencies are at the same depth. In this scenario, Maven will use the first-found dependency, which would be 0.1.3, and not the latest 2.2.0 version.






src folder

Let's look into the src folder. Maven generated two folders for us: main and test.


main_test.png

Maven places Java source code in the src\main\java folder. This makes it easier to understand and navigate any Maven project.

Here is the generated "Hello World!" java source code ():

package com.k.bogotobogo;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}











Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Java Tutorials



Java Tutorial Home

Basics - Compiling and Launching

Inner Classes

Constructor

Enums

Static & Finally

Default and Protected

Polymorphism

Exception Handling

Exception Handling II

String Class

Threads

Threads II - State Transition

Threads III - Synchronization

Object Class

File I/O

Serialization

ArrayList

Autoboxing

Java Graphics Interface I - Basics

Java Graphics Interface II - Labels, Text Fields, Layouts

Java Graphics Interface III - paintComponent

TCP Sockets Server/Client

Scala - Functional Java Programming

Apache CXF install

Tomcat 7 Ubuntu 14 Install on Amazon EC2 instance

What is Apache Maven?

Maven life cycle

Eclipse Maven 3 plugin on Ubuntu 14.04

Apache Maven 3 - Setting up and creating a project

Apache Maven 3 - Compile, build, and install a Maven project

Apache Maven 3 - Dependencies

Apache Maven 3 - Web Application

Apache Maven 3 - Plugins (compiler)

Apache Maven 3 - Plugins (Jetty)

Eclipse CDT / JNI (Java Native Interface) / MinGW



Spring Framework

Hello World App with Spring 4 & Maven 3 - Part I




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Spring Boot



Spring Boot : Hello world with Mavan 3

Spring Boot : Hello world with Gradle 2

Spring Boot (Gradle 2) : Hello world with Authentication

Spring Boot : Deploying War file to Tomcat 8's webapps

How to Setup Apache as Reverse Proxy for Tomcat Server using mod proxy

Maven : mvn command cheat sheet

Spring-Boot REST API with CORS App Maven war file deploy to Tomcat

Spring-Boot / Spring Security with AngularJS - Part I (Introduction)

Spring-Boot / Spring Security with AngularJS - Part II (Dynamic resource load from Angular)

Spring-Boot / Spring Security with AngularJS : Part III (Form-based Authentication)





JUnit & Maven Tutorial



JUnit 4 Introduction (Hello World)

JUnit 4 Test with Eclipse Luna (Hello World)

JUnit 4 Test with Maven (Hello World)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master