Maven Project Setup Guide

amit_singh_bisht

Amit Singh Bisht

Posted on June 26, 2024

Maven Project Setup Guide

maven logo

Step-by-Step Guide to Creating a Maven Project

Using Maven Archetype
You can create a new Maven project by running the following command:

mvn archetype:generate
Enter fullscreen mode Exit fullscreen mode

This command will prompt you to enter various details about your project and generate the pom.xml file. Below is an example interaction:

3444: remote -> za.co.absa.hyperdrive:component-archetype_2.12 (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 2155: 

If you press enter, it will select (2155: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)

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
7: 1.3
8: 1.4
Choose a number: 8: 

If you press enter, it will select (8: 1.4)

Define value for property 'groupId': com.example.project
Define value for property 'artifactId': my-app
Define value for property 'version' 1.0-SNAPSHOT: : 
Define value for property 'package' com.example.project: : com.example.project.myapp

(press enter or any value that you want)


REMEMBER :- groupId, artifactId, version, package can be changed at later stage as well

Confirm properties configuration:
groupId: com.example.project
artifactId: my-app
version: 1.0-SNAPSHOT
package: com.example.project.myapp
 Y: : (press enter)


[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.example.project
[INFO] Parameter: artifactId, Value: my-app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.example.project.myapp
[INFO] Parameter: packageInPathFormat, Value: com/example/project/myapp
[INFO] Parameter: package, Value: com.example.project.myapp
[INFO] Parameter: groupId, Value: com.example.project
[INFO] Parameter: artifactId, Value: my-app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Project created from Archetype in dir: /Users/amitb/Desktop/code/lambdatest/professional-services-java-cucumber/my-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  00:50 min
[INFO] Finished at: 2024-06-26T10:37:56+05:30
[INFO] ------------------------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode

Skipping the Questionnaire

To skip the interactive questionnaire and create a Maven project with one command, use:

mvn archetype:generate -DgroupId=com.example.project -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Enter fullscreen mode Exit fullscreen mode

Example output:

mvn archetype:generate -DgroupId=com.example.project -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] >>> archetype:3.2.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< archetype:3.2.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] 
[INFO] 
[INFO] --- archetype:3.2.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /Users/amitb
[INFO] Parameter: package, Value: com.example.project
[INFO] Parameter: groupId, Value: com.example.project
[INFO] Parameter: artifactId, Value: my-app
[INFO] Parameter: packageName, Value: com.example.project
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/amitb/my-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.300 s
[INFO] Finished at: 2024-06-26T10:43:15+05:30
[INFO] ------------------------------------------------------------------------

Enter fullscreen mode Exit fullscreen mode

The pom.xml Structure

Main Block

The entire pom.xml is enclosed within the following tags:

<?xml version="1.0" encoding="UTF-8"?>
<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"> 

</project>
Enter fullscreen mode Exit fullscreen mode

Basic Properties

This block defines the most basic properties of the application:

<?xml version="1.0" encoding="UTF-8"?>
<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.example.project</groupId>
   <artifactId>my-app</artifactId>
   <version>0.1.0</version>
   <packaging>jar</packaging>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
   </properties>

   [...]

</project>
Enter fullscreen mode Exit fullscreen mode

Dependencies Block

For this project, I am importing the JDBC library for accessing SQLite databases:

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.20.0</version>
        </dependency>
    </dependencies>
Enter fullscreen mode Exit fullscreen mode

Key Points

  1. groupId: A unique base name of your company or group.
  2. artifactId: A unique name of the project you are developing.
  3. version: The version of the project you are developing.
  4. packaging: Type of artifact (e.g., jar, war).
  5. dependencies: External libraries required for your project.

With this setup, you can easily manage dependencies and keep your project up to date with the latest versions.

💖 💪 🙅 🚩
amit_singh_bisht
Amit Singh Bisht

Posted on June 26, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related