Creating Java Project in Maven

Last updated on May 27 2022
Mrinalini Pandey

Table of Contents

Creating Java Project in Maven

Maven – Creating Project

Maven uses archetype plugins to create projects. To create a simple java application, we’ll use maven-archetype-quickstart plugin. In example below, we’ll create a maven-based java application project in C:\MVN folder.
Let’s open the command console, go to the C:\MVN directory and execute the subsequent mvn command.
C:\MVN>mvn archetype:generate
-DgroupId = com.companyname.bank
-DartifactId = consumerBanking
-DarchetypeArtifactId = maven-archetype-quickstart
-DinteractiveMode = false
Maven will start processing and will create the complete java application project structure.
[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] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Batch mode
[INFO] ——————————————————————-
[INFO] Using subsequent parameters for creating project
from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ——————————————————————-

[INFO] Parameter: groupId, Value: com.companyname.bank
[INFO] Parameter: packageName, Value: com.companyname.bank
[INFO] Parameter: package, Value: com.companyname.bank
[INFO] Parameter: artifactId, Value: consumerBanking
[INFO] Parameter: basedir, Value: C:\MVN
[INFO] Parameter: version, Value: 1.0-SNAPSHOT

[INFO] project created from Old (1.x) Archetype in dir: C:\MVN\consumerBanking
[INFO] ——————————————————————
[INFO] BUILD SUCCESSFUL
[INFO] ——————————————————————
[INFO] Total time: 14 seconds
[INFO] Finished at: Tue Jul 10 15:38:58 IST 2012
[INFO] Final Memory: 21M/124M
[INFO] ——————————————————————
Now go to C:/MVN directory. You’ll see a java application project created, named consumer Banking (as specified in artifactId). Maven uses a standard directory layout as shown below −

Page 2 Image 1 6
Directory layout

Using the above example, we can understand the subsequent key concepts −

Sr.No. Folder Structure & Description
1 consumerBanking

contains src folder and pom.xml

2 src/main/java

contains java code files under the package structure (com/companyName/bank).

3 src/main/test

contains test java code files under the package structure (com/companyName/bank).

4 src/main/resources

it contains images/properties files (In above example, we need to create this structure manually).

If you observe, you’ll find that Maven also created a sample Java Source file and Java Test file. Open C:\MVN\consumerBanking\src\main\java\com\companyname\bank folder, you’ll see App.java.
package com.companyname.bank;

/**
* Hello world!
*
*/
public class App {
public static void main( String[] args ){
System.out.println( “Hello World!” );
}
}
Open C:\MVN\consumerBanking\src\test\java\com\companyname\bank folder to see AppTest.java.
package com.companyname.bank;

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 );
}
}
Developers are required to place their files as mentioned in table above and Maven handles all the build related complexities.
In the next chapter, we’ll discuss how to build and test the project using maven Build and Test Project.

Maven – Build & Test Project

What we learnt in Project Creation chapter is how to create a Java application using Maven. Now we’ll see how to build and test the application.
Go to C:/MVN directory where you’ve created your java application. Open consumerBanking folder. You’ll see the POM.xml file with the subsequent contents.
<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.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
Here you can see, Maven already added Junit as test framework. By default, Maven adds a source file App.java and a test file AppTest.java in its default directory structure, as discussed in the previous chapter.
Let’s open the command console, go the C:\MVN\consumerBanking directory and execute the subsequent mvn command.
C:\MVN\consumerBanking>mvn clean package
Maven will start building the project.
[INFO] Scanning for projects…
[INFO] ——————————————————————-
[INFO] Building consumerBanking
[INFO] task-segment: [clean, package]
[INFO] ——————————————————————-
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory C:\MVN\consumerBanking\target
[INFO] [resources:resources {execution: default-resources}]

[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\main\resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\classes
[INFO] [resources:testResources {execution: default-testResources}]

[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: C:\MVN\consumerBanking\target\surefire-reports

—————————————————–
T E S T S
—————————————————–

Running com.companyname.bank.AppTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.027 sec

Results :

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

[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: C:\MVN\consumerBanking\target\
consumerBanking-1.0-SNAPSHOT.jar

[INFO]———————————————–
[INFO] BUILD SUCCESSFUL
[INFO]———————————————–

[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Jul 10 16:52:18 IST 2012
[INFO] Final Memory: 16M/89M
[INFO]———————————————–
You’ve built your project and created final jar file, subsequent are the key learning concepts −
• We give maven two goals, first to clean the target directory (clean) and then package the project build output as jar (package).
• Packaged jar is available in consumerBanking\target folder as consumerBanking-1.0-SNAPSHOT.jar.
• Test reports are available in consumerBanking\target\surefire-reports folder.
• Maven compiles the source code file(s) and then tests the source code file(s).
• Then Maven runs the test cases.
• Finally, Maven creates the package.
Now open the command console, go the C:\MVN\consumerBanking\target\classes directory and execute the subsequent java command.
>java com.companyname.bank.App
You’ll see the result as follows −
Hello World!

Adding Java Source Files

Let’s see how we can add additional Java files in our project. Open C:\MVN\consumerBanking\src\main\java\com\companyname\bank folder, create Util class in it as Util.java.
package com.companyname.bank;

public class Util {
public static void printMessage(String message){
System.out.println(message);
}
}
Update the App class to use Util class.
package com.companyname.bank;

/**
* Hello world!
*
*/

public class App {
public static void main( String[] args ){
Util.printMessage(“Hello World!”);
}
}
Now open the command console, go the C:\MVN\consumerBanking directory and execute the subsequent mvn command.
>mvn clean compile
After Maven build is successful, go to the C:\MVN\consumerBanking\target\classes directory and execute the subsequent java command.
>java -cp com.companyname.bank.App
You’ll see the result as follows −
Hello World!

Maven – External Dependencies

As you know, Maven does the dependency management using the concept of Repositories. But what happens if dependency is not available in any of remote repositories and central repository? Maven provides answer for such scenario using concept of External Dependency.
For example, let us do the subsequent changes to the project created in ‘Creating Java Project’ section.
• Add lib folder to the src folder.
• Copy any jar into the lib folder. We’ve used ldapjdk.jar, which is a helper library for LDAP operations.
Now our project structure should look like the subsequent −

Page 8 Image 2
project structure

Here you are having your own library, specific to the project, which is an usual case and it contains jars, which may not be available in any repository for maven to download from. If your code is using this library with Maven, then Maven build will fail as it cannot download or refer to this library during compilation phase.
To handle the situation, let’s add this external dependency to maven pom.xml using the subsequent way.
<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.companyname.bank</groupId>
<artifactId>consumerBanking</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>consumerBanking</name>
<url>http://maven.apache.org</url>

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

<dependency>
<groupId>ldapjdk</groupId>
<artifactId>ldapjdk</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
</dependency>
</dependencies>

</project>
Look at the second dependency element under dependencies in the above example, which clears the subsequent key concepts about External Dependency.
• External dependencies (library jar location) can be configured in pom.xml in equivvalent way as other dependencies.
• Specify groupId equivvalent as the name of the library.
• Specify artifactId equivvalent as the name of the library.
• Specify scope as system.
• Specify system path relative to the project location.
Hope now you are clear about external dependencies and you’ll be able to specify external dependencies in your Maven project.
So, this brings us to the end of blog. This Tecklearn ‘Creating Java Project in Maven’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Maven and build a career in DevOps domain, then check out our interactive, Maven Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Maven

Maven Training

About the Course

Tecklearn has specially designed this Maven Training Course to advance your skills for a successful career in this domain. The course will cover different components of Maven and how they are used in software development operations. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Maven.

Why Should you take Maven Training?

• The average salary for “ant maven” ranges from approximately $71,430 per year for Entry Level Engineer to $126,916 per year for Development Operations Engineer. – Indeed.com
• According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps

• What is Software Development
• Software Development Life Cycle
• Why DevOps?
• What is DevOps?
• DevOps Lifecycle
• DevOps Tools
• Benefits of DevOps
• How DevOps is related to Agile Delivery
• DevOps Implementation

Maven

• Maven
• Maven Directory
• Maven Lifecycle
• Maven Dependencies
• Maven Repositories
• Phases and Goals

0 responses on "Creating Java Project in Maven"

Leave a Message

Your email address will not be published. Required fields are marked *