A Complete Guide To Maven – Part 2

In the previous article, we have seen the maven introduction and also seen the maven features, maven life cycle, different types of POM files, maven repositories, etc. In this article, we will look for different examples of creating maven projects. We can create a maven project through IDEs or command prompt. We will see the step-by-step process to create in all ways.

Prerequisite

  • Maven Installed and Setup the environment variables (See Part 1 for complete Setup)
  • IDE Installed – Eclipse, IntelliJ.

Read the Previous Maven Article: A Complete Guide To Maven Introduction – Part 1

Maven Project using Command Prompt

To create a simple maven project, you need to open the command prompt and run the following archetype:generate syntax-

Syntax –

mvn archetype:generate -DgroupId=groupId
-DartifactId=artifactId
-DarchetypeArtifactId=maven-archetype-quickstart
-Dversion=version

In the above syntax, you can see different flags, that can be described as –

  • -DgroupId – Specifies the group Id of the maven project. Example - org.example.
  • -DartifactId – Specifies the project Id or project name. Example - SampleProject.
  • -DarchetypeArtifactId – Specifies the project initial structure that we are going to start.
  • -Dversion – Specifies the version of the project. Example - 1.0-SNAPSHOT.

Now, we are creating a new maven project using generate syntax, For so, write the below syntax on the command prompt.

mvn archetype:generate -DgroupId=com.example -DartifactId=SampleMavenProject -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0-SNAPSHOT

After hitting the command, It will start creating a project in the interactive mode which means It will show all the provided details and will ask to continue, as you can see in the console output –

C:\Shubham\Blogs\Maven>mvn archetype:generate -DgroupId=com.example -DartifactId=SampleMavenProject -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0-SNAPSHOT
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Using property: groupId = com.example
[INFO] Using property: artifactId = SampleMavenProject
[INFO] Using property: version = 1.0-SNAPSHOT
[INFO] Using property: package = com.example
Confirm properties configuration:
groupId: com.example
artifactId: SampleMavenProject
version: 1.0-SNAPSHOT
package: com.example
 Y: :

If you want to continue, press the Y key, and It will create the complete project with BUILD SUCCESS status.

C:\Shubham\Blogs\Maven>mvn archetype:generate -DgroupId=com.example -DartifactId=SampleMavenProject -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0-SNAPSHOT
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Using property: groupId = com.example
[INFO] Using property: artifactId = SampleMavenProject
[INFO] Using property: version = 1.0-SNAPSHOT
[INFO] Using property: package = com.example
Confirm properties configuration:
groupId: com.example
artifactId: SampleMavenProject
version: 1.0-SNAPSHOT
package: com.example
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\Shubham\Blogs\Maven
[INFO] Parameter: package, Value: com.example
[INFO] Parameter: groupId, Value: com.example
[INFO] Parameter: artifactId, Value: SampleMavenProject
[INFO] Parameter: packageName, Value: com.example
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Shubham\Blogs\Maven\SampleMavenProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  05:28 min
[INFO] Finished at: 2022-12-17T13:10:03+05:30
[INFO] ------------------------------------------------------------------------

If you don’t want to create in the interactive mode you can add -DinteractiveMode=false with the same maven archetype command, and It will start creating the project in batch mode. It will not ask for confirmation anymore.

mvn archetype:generate -DgroupId=com.example -DartifactId=SampleMavenProject -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0-SNAPSHOT -DinteractiveMode=false

After successfully creating the project, you can see the folder containing one pom.xml file and src folder. The folder structure is the same as follows – 

Maven Project Structure
Fig 1 – Maven Project Structure
|-SampleMavenProject
  |--src
    |---main
      |----java
        |-----com
          |------example
            |-------App.java
    |---test
      |----java
        |-----com
          |------example
            |-------AppTest.java
  |--pom.xml

As you can see, the project folder contains only three files i.e. pom.xml, App.java, and AppTest.java. Let’s open all the files –

1. pom.xml 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>SampleMavenProject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>
    <name>SampleMavenProject</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2. App.java file

package com.example;

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

3. AppTest.java file

package com.example;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest extends TestCase {
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest(String testName) {
        super(testName);
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite() {
        return new TestSuite(AppTest.class);
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp() {
        assertTrue(true);
    }
}

Compile the Maven Project

Goto the project location where pom.xml file is located, open the command prompt, and type the following command – 

mvn clean compile
C:\Shubham\Blogs\Maven\SampleMavenProject>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.example:SampleMavenProject >-------------------
[INFO] Building SampleMavenProject 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ SampleMavenProject ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ SampleMavenProject ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Shubham\Blogs\Maven\SampleMavenProject\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ SampleMavenProject ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Shubham\Blogs\Maven\SampleMavenProject\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.127 s
[INFO] Finished at: 2022-12-14T20:47:51+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project SampleMavenProject: Compilation failure: Compilation failure:
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

If you get the above error while running, mvn clean compile then add the below syntax into your pom.xml, and reload the project.

<properties>
   <maven.compiler.source>8</maven.compiler.source>
   <maven.compiler.target>8</maven.compiler.target>
</properties>
C:\Shubham\Blogs\Maven\SampleMavenProject>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.example:SampleMavenProject >-------------------
[INFO] Building SampleMavenProject 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ SampleMavenProject ---
[INFO] Deleting C:\Shubham\Blogs\Maven\SampleMavenProject\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ SampleMavenProject ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Shubham\Blogs\Maven\SampleMavenProject\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ SampleMavenProject ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Shubham\Blogs\Maven\SampleMavenProject\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.221 s
[INFO] Finished at: 2022-12-14T20:54:21+05:30
[INFO] ------------------------------------------------------------------------

After running this command, and getting BUILD SUCCESS status, you can see the target directory in your project folder. That target directory contains all the compiled class files.

Run the Maven Project

Go to the /target/classes directory And open the command prompt and type the following command – 

java com.example.App

After running this command, you can see the output on the console.

Console Output
Fig 2 – Console Output

Build JAR using Maven Lifecycle

The mvn package command completes the maven build lifecycle. You need to execute the following command in your project root folder.

mvn package

It will create the JAR file inside the target directory. The naming Structure of this JAR file will be ProjectName-version.jar. For this project, it will be – 

SampleMavenProject-1.0-SNAPSHOT.jar

You can also run the JAR file by using the command. To do so, go to the target location and execute the below command – 

java -classpath SampleMavenProject-1.0-SNAPSHOT.jar;.; com.example.App

After running this command, you can see the output on the console.

Console Output
Fig 3 – Console Output

Conclusion

We are good to end the current article i.e creating a maven project using the command prompt. I am hoping this will give you a basic idea of creating a starter maven project. In the next maven article, we will see the example through IntelliJ IDE.

If you have any issues or doubts, feel free to connect with us. We will be happy to see you there.

Resources

If you like this article please make sure to Like, Comment, and Share it with your friends and colleagues

Follow us on our social networks –