Installing and creating your first Maven Project in windows

Hi,

In this article today, we will see how to configure Maven in your windows machine and start using it.

1. Have the Maven installed in your PC. You can get the jar file from the apache site. Download the zip file and extract it to a folder e.g, D:\apache maven\apache-maven-3.2.2. Going forward, this path will be referred as maven installation directory.

2. Create environment variables that point to your maven installation. As in,

MAVEN_HOME=D:\apache maven\apache-maven-3.2.2
M2_HOME=D:\apache maven\apache-maven-3.2.2

In this article, mykong explains why we need to set up two variables. Along with this, ensure that JAVA_HOME is set to the right installation directory in your machine.

http://www.mkyong.com/maven/how-to-install-maven-in-windows/

3. Now go the path %MAVEN_HOME%/conf/ and open the file settings.xml. The %MAVEN_HOME% is your maven installation directory.

4. The xml tags present in the settings.xml file are empty with no values. We need edit the file and put some valid tags in order for maven installation to work. A sample edited file looks like below:

<?xml version=”1.0″ encoding=”UTF-8″?>
<settings xmlns=”http://maven.apache.org/SETTINGS/1.0.0&#8243;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd”&gt;
<localRepository>${user.home}/.m2/repository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>
<pluginGroups></pluginGroups>
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>A.B.C.D</host>
<port>80</port>
<nonProxyHosts>toople*|*docs</nonProxyHosts>
</proxy>
</proxies>
<servers></servers>
<mirrors></mirrors>
<profiles>
<profile>
<id>mavenProfile</id>
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/context/groups/public-jboss</url&gt;
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
</repository>
<repositories>
<pluginRepositories>
<pluginRepository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url&gt;
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepositories>
</profile>
</profiles>
</settings>

As you can see, the required tags are added to the settings.xml file. The value of the proxy server is given as A.B.C.D due to security reasons. In some situations you will be required to add the tags <username> and <password>, as it depends on the way your PC connects to the network. The value in the localRepository tag refers to a path in
your PC. It will be pointing to “C:\Users\<Your_Login_Name>\.m2\repository\”. While using maven against any specific goals or plugins, maven will check if the relevant jars needed to complete the execution are present in the above path. If not, it will fetch the details from the repository location and copies them into the above path.

5. Now, create a top level directory which will hold all the maven projects. E.g., D:\Maven_Projects

6. Open the command prompt window and navigate to the newly created directory path and run the below command.

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DInteractiveMode=false

7. If all the previous steps are successfully preformed, the above command will finish successfully and you should be able to see new directory structure created with a file named App.java which is your Hello World file.

8. Now package the content into a jar file using the below command. On successful completion, we shall see a new jar file created under my-app\target\my-app-1.0-SNAPSHOT.jar

mvn package

9. Run the program using the below java command.

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

which produces the follwowing output:

Hello World!

This concludes that your maven installation is working fine and you can start of by creating maven projects.

Thanks,

Kalyan

Leave a comment