This is mainly for the beginners of Java to get started with Maven. This is required not only for learning Java, but also conducive to creating your self-taught Java projects. Extends Setting up Java step by step tutorial. This is an industrial strength tool to build Java applications.
Install Maven
Maven is a build, dependency management (i.e. jar files dependency) and deployment tool.
Step 1: Download and install Maven from:
http://maven.apache.org/download.html
Download the binary “zip” file “apache-maven-3.5.0-bin.zip” if in Windows or the “apache-maven-3.5.0-bin.tar.gz” if in Unix, and unpack them into the “tools” folder.
Step 2: Configure the environment variables “M3_HOME” and “path”. For example
On DOS
Previously in Java installation tutorial “environment-setup.cmd” was created under “c:\scripts”. Add Maven environment settings to that file.
1 2 3 4 5 6 7 8 9 |
REM This is sample to set up your Java SET TOOLS_HOME=C:\tools SET JAVA_HOME=%TOOLS_HOME%\jdk-8u131-windows-x64 SET M3_HOME=%TOOLS_HOME%\apache-maven-3.5.0-bin\apache-maven-3.5.0 SET MAVEN_OPTS=-Xms1024m -Xmx1G SET PATH=%PATH%;%JAVA_HOME%\bin;%M3_HOME%\bin |
Run the script:
1 2 |
c:\scripts>environment-setup.cmd |
On Unix
1 2 3 4 |
$touch .profile $vi .profile |
type the following in “.profile”
1 2 3 4 5 6 |
export TOOLS_HOME=/tools export JAVA_HOME=$TOOLS_HOME/jdk1.8.0_60 export M3_HOME=$TOOLS_HOME/apache-maven-3.5.0-bin/apache-maven-3.5.0 export MAVEN_OPTS=-Xms1024m -Xmx1G export PATH=$PATH:$M3_HOME/bin:$JAVA_HOME/bin:$M3_HOME/bin |
1 2 3 |
$source .profile |
Verify installation
Once maven has been set up correctly, you should be able to verify it by
1 2 3 4 5 6 7 8 9 |
c:\scripts>mvn --version Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0 Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T05:39:06+10:00) Maven home: C:\tools\apache-maven-3.5.0-bin\apache-maven-3.5.0\bin\.. Java version: 1.8.0_131, vendor: Oracle Corporation Java home: C:\tools\jdk-8u131-windows-x64\jre Default locale: en_US, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows" |
also, “where” in DOS and “which” in Unix.
1 2 3 |
c:\projects>where mvn C:\tools\apache-maven-3.5.0-bin\apache-maven-3.5.0\bin\mvn |
Configure Maven
You can set up the repository location by opening the settings.xml file under conf folder inside your maven installation directory “c:\tools\apache-maven-3.5.0-bin\apache-maven-3.5.0\conf”. Repository is where the dependency Java libraries will be stored.
Rename the settings.xml to settings.xml_old, and create a new “settings.xml” as shown below with a basic config.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>c:\tools\home\.m2\repository</localRepository> <interactiveMode/> <usePluginRegistry/> <offline/> <pluginGroups/> <servers/> <mirrors/> <proxies> <!-- <proxy> <id>default</id> <active>true</active> <protocol>http</protocol> <host>proxy-host</host> <port>8080</port> <username>user</username> <password>password123</password> <nonProxyHosts>localhost,127.0.0.1</nonProxyHosts> </proxy> --> </proxies> <activeProfiles> <!--make the profile active all the time --> <activeProfile>securecentral</activeProfile> </activeProfiles> <profiles> <profile> <id>securecentral</id> <!--Override the repository (and pluginRepository) "central" from the Maven Super POM --> <repositories> <repository> <id>central</id> <url>http://repo1.maven.apache.org/maven2/</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>https://repo1.maven.org/maven2/</url> <releases> <enabled>true</enabled> </releases> </pluginRepository> </pluginRepositories> </profile> </profiles> </settings> |
Local Repository
1 2 3 |
<localRepository>c:\tools\home\.m2\repository</localRepository> |
Are you behind a firewall?
If your internet access requires proxy settings, you need have that added to your settings.xml file.
1 2 3 4 5 6 7 8 9 10 11 |
<proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <username>proxyuser</username> <password>proxypass</password> <host>webproxy</host> <port>8080</port> <nonProxyHosts>local.net,some.host.com</nonProxyHosts> </proxy> |
Create a Maven project
A maven project is nothing but a Java project as we saw earlier with a predefined structure. Maven follows a certain convention in terms of the directory structure.
Step 1: Create a Java project using Maven. “com.mytutorial” is a package inside “src/main/java”.
1 2 |
c:\projects>mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simple-maven-project -DinteractiveMode=false |
Step 2: “pom.xml” file looks as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mytutorial</groupId> <artifactId>simple-maven-project</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>simple-maven-project</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> </dependencies> </project> |
The pom.xml file is where you define the dependent libraries like “hibernate”, “spring”, “junit”, etc. Change the version to the latest possible. For example: “4.9” as per
“http://repo1.maven.apache.org/maven2/junit/junit/4.9/“.
You can also search at
http://search.maven.org/
prefix it with “g:” for group as in “g:junit”, and “a:” for artifact id as in “a:junit”.
Step 3: Inspect the local repository location that we set up via the “settings.xml”.
Step 4: Create a Java “HelloWorld.java” as shown below inside “C:\projects\simple-maven-project\src\main\java\com\mytutorial” where “src\main\java” is the mvn stipulated project structure and “com\mytutorial” is the package.
1 2 3 4 5 6 7 8 9 10 |
package com.mytutorial; public class HelloWorld { public static void main(String[] args) { System.out.println("My first java code from a Maven based Java project"); } } |
Step 5: Execute the following command from the project base folder “C:\projects\simple-maven-project”. This basically looks at the “pom.xml” file in that folder.
1 2 |
c:\projects\simple-maven-project>mvn clean package |
This would have created a jar file named “simple-maven-project-1.0-SNAPSHOT.jar” inside the “C:\projects\simple-maven-project\target”. This jar file contains the packages & class files. The maven default plugins have compiled the “.java” files and packaged it up as a jar file. This is the basic power of a build tool. It can do a lot more.
Step 6: Run “com.mytutorial.HelloWorld”
1 2 3 |
c:\projects>java -cp simple-maven-project\target\simple-maven-project-1.0-SNAPSHOT.jar com.mytutorial.HelloWorld My first java code from a Maven based Java project |
Much simpler to create Java projects as jar files.
Tips:
#1. Setting up the Maven environment
Instead of running the “c:\scripts>environment-setup.cmd” in Windows, you could set up the Windows environment variables as demonstrated in setting up Java tutorial.
#2. Installing the jar file to local maven repository
Instead of “mvn clean package”, you can install it to the repository in your “c:\tools\home\.m2\repository” folder.
1 |
c:\projects\simple-maven-project>mvn clean install |
Next Java Beginner Tutorial: Setting up Eclipse IDE step by step tutorial.