How to create maven project with command line with example| Eclipse | Intelli

Maven is a build tool for java projects. It speeds up the java development build process. maven is one of the Java tools for continuous build integration.

Maven has predefined phases or goals. The goals are similar to ant targets. the developer will invoke goals to do their tasks.

You can check my other posts on maven commands.

First, Please make sure that you download maven from the Apache software and install it. If you are not sure how to do it, you can check install maven. What are the things to consider while making your java projects to implement Maven as the build tool?

  1. project object model file
  2. java project folder structure

Create a maven project using the command line

First, Create a maven template project using the maven archetype plugin.

This archetype plugin has different templates for web applications, java applications, and enterprise applications.

archetype:generate is an actual goal to provide with
archetypeArtifactId=maven-archetype-quickstart which generates jar based java application

 mvn archetype:generate -DgroupId=com.techrocksz -DartifactId=HelloWorld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

The output of this mvn command:-

E:\techrocksz>mvn archetype:generate -DgroupId=com.techrocksz -DartifactId=HelloWorld -DarchetypeArtifactId=maven-archetype-quickstart
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate](aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate]
[INFO] Generating project in Interactive mode
Define value for version:  1.0-SNAPSHOT: : 1.0.0-SNAPSHOT
Confirm properties configuration:
groupId: com.techrocksz
artifactId: HelloWorld
version: 1.0.0-SNAPSHOT
package: com.techrocksz
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using the following parameters for creating OldArchetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.techrocksz
[INFO] Parameter: packageName, Value: com.techrocksz
[INFO] Parameter: package, Value: com.techrocksz
[INFO] Parameter: artifactId, Value: HelloWorld
[INFO] Parameter: basedir, Value: E:\techrocksz
[INFO] Parameter: version, Value: 1.0.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: E:\techrocksz\HelloWorld
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30 seconds
[INFO] Finished at: Fri Dec 02 10:47:29 GMT+05:30 2011
[INFO] Final Memory: 8M/254M
[INFO] ------------------------------------------------------------------------

The above command created the HelloWorld application,

First Go to the Project directory structure.

In the root folder, we have a folder named src and file pom.xml. and also we have the following subdirectories

  • src\\main\\java :- This folder contains all your java classes
  • src\\test\\java : This folder contains all your java test-related classes
  • and also we have com\techrocksz\ and com\techrocksz\ files which is a HelloWorld application and its test class.

Sample created pom.xml with command line:-

Maven uses pom.xml which contains all details about your project as well as the configuration details.

pom.xml is a project object model configuration file.

<project xmlns:xsi="" xmlns="" xsi:schemalocation="">

In the above, groupId: represents the package of the project where the ExampleDemo-1.0.0.jar file is created.

artifactId: is the project name as well as the jar/war name of the module.

packaging specifies whether this module is a web application i.e war or a java module i.e jar. if the packaging option is not specified, maven considers this as a jar module.

dependencies: tag specifies what are dependencies this project has. here as we have written App, by default JUnit dependency is added as part of the sample project.

Running Maven Project:

Here are the steps to run a maven project and install all dependencies.

  • First, go to the application using cd HelloWorld.
  • Next, run the mvn clean install command to clean, compile and install dependencies.

maven execution process:

As we already saw that repositories location and dependencies are configured in pom.xml

When you run mvn clean install for the first time, it tries to download different plugins and dependencies from the repository and copy those dependencies to a folder (the user profile of your document and Settings folder(i.e C:\Documents and Settings\username\.m2\repository).

This is time-consuming for the first time as it tries to download all your plugins to your local repository. Next time onwards, it will download those already downloaded jars and execute them quickly.

Here is the output of the command.

`E:\techrocksz\HelloWorld>mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building HelloWorld
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Deleting directory E:\techrocksz\HelloWorld\target
[INFO] [resources:resources]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\techrocksz\HelloWorld\src\main\resources
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to E:\techrocksz\HelloWorld\target\classes
[INFO] [resources:testResources]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\techrocksz\HelloWorld\src\test\resources
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to E:\techrocksz\HelloWorld\target\test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: E:\techrocksz\HelloWorld\target\surefire-reports

 T E S T S
Running com.techrocksz.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] [jar:jar]
[INFO] Building jar: E:\techrocksz\HelloWorld\target\HelloWorld-1.0.0-SNAPSHOT.jar
[INFO] [install:install]
[INFO] Installing E:\techrocksz\HelloWorld\target\HelloWorld-1.0.0-SNAPSHOT.jar to C:\Documents and Settings\kinturu\.m2\repository\com\techrocksz\HelloWorld\1.0.0-SNAPSHOT\HelloWorld-1.0.0-SNAPSHOT.j
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Fri Dec 02 11:58:46 GMT+05:30 2011
[INFO] Final Memory: 15M/254M
[INFO] ------------------------------------------------------------------------

Finally, your module HelloWorld-1.0.0-SNAPSHOT.jar is created.

Now Any other app is ready to use this module. Finally, learning maven is started.

How to configure Proxy Settings in the maven application

settings.xml has to be created in folder C:\\Documents and Settings\\username\\.m2\\.

Here is a sample settings.xml example

    <!-- Http Proxy setting configuration-->
    <!-- HTTPS Proxy configuration details -->

How to create a java web application in eclipse?

In Eclipse, It is very easy to create a project in eclipse

Here are step by steps

  • In the File menu, Select New Project, It opens a new window as seen below Maven eclipse web application archetype

Select Maven - Maven Project

  • Click the Next button, Select defaults in the next window, An window shows Select an Archetype as seen below
Maven eclipse web application archetype
  • Next window, Please enter Group Id, Artifact Id, and version details
Maven eclipse web application creation

Please leave a comment if you have any questions.