Updated February 9, 2023
Definition of Java Project Maven
Java project maven has used the archetype plugin to create the projects. For creating an application of java we are using the plugin of maven archetype quickstart. We can also create the maven project from the command line by executing the archetype: generate command on the maven tool. To create a simple maven project by using the command line tool we need to run the archetype: generate command.
Introduction to Java Project Maven
To create the project in maven, we can also create it by using IDE tools. We can also generate the maven project by using Eclipse IDE, spring tool suite, and other tools. At the time of creating a maven project through the command line, first, we need to install the maven tool in our system. We can check the maven installation by checking the version of maven. We can check the version of maven by using the mvn –version command.
Maven is a very popular tool used to manage java projects. This tool helps us to create, built, test, and deploy our application in java. It is very popular for java applications that are running through the command line or we can also run it through the IntelliJ IDE, Eclipse, or spring tool suite. We can create the project template of maven by using the archetype plugin.
Key Takeaways
- To create the maven project in java first we need to set up the maven environment in our system. We need to install maven to create the java project.
- Maven is used for creating java projects. We can also create maven java projects by using different type’s tools.
How to Create Java Project in Maven?
The below steps shows how we can create the java project in maven as follows:
To create a project we need to install java and maven in our system.
1. To create a java project in Maven, we require to have Java installed on our system. We already had Java installed on our system, so we did not need to do so again. We are checking the installation with the following command.
java -version
2. After installing and checking the version of java, now we need to check the maven installation. In the below example, we are downloading the maven file from the official site.
3. After downloading the maven file, we must extract it in order to use maven in our environment. We can check the version of apache maven by using the below command.
mvn –version
4. After installing java and maven, now in this step we are creating the project by using the archetype as follows. This is a project template used to create a new project by using maven in java.
mvn archetype:generate -DgroupId=com.test.examples -DartifactId=maven_test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
5. After creating the project, we can check the structure of the project as follows. To check the structure of the project we are opening the project in the spring tool suite. The project structure starts with the source file. Also, it contain the pom.xml file into it. This file contains all the configuration and dependencies of the project.
6. After opening the project into the spring tool suite, now in this step we are checking the pom.xml file. The newly created file only contains the structure it will not contain the dependency packages. We need to add the required dependency packages as per our requirements.
7. After checking the dependency file now in this step, we are adding the following dependencies. We are adding the codec and JUnit dependency as follows.
Code:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
8. After adding the JUnit and codec dependency now in this step we are adding the code with the SHA-256 algorithm as follows.
Code:
import org.apache.commons.codec.digest.DigestUtils;
public class App
{
public static void main(String[] args)
{
if (args.length < 1)
{
System.err.println ("Input-");
System.exit(0);
}
System.out.println (sha256hex(args[0]));
}
public static String sha256hex(String ip) {
return DigestUtils.sha256Hex(ip);
}
}
9. After creating the class now in this step, we are creating the test JUnit method as follows.
Code:
import com.test.examples.App;
public class AppTest {
private String INPUT = "123456";
@Test
public void testLength () {
Assert.assertEquals (64, App.sha256hex(INPUT).length());
}
}
10. After adding the file now in this step, we are running the project by using the maven command. We are executing the mvn package command.
mvn package
11. After executing the mvn package command now we are executing the mvn clean package command as follows.
mvn clean package
Java Applications Project Maven
The below steps shows how we can create a maven java application project as follows:
We are creating the maven applications project in the spring tool suite as follows.
1. In the first step we are opening the spring tool suite. We are opening the spring tool suite on windows OS.
2. After opening the spring tool suite, now in this step we are creating a new maven project. For creating a maven project we need to open the file and in the file need to select a new tab and then need to select the maven project.
3. After opening the new project tab now we need to click on the next button, in this step we need to select the archetype as follows.
4. After selecting the archetype now in this step, we need to click on the next button as follows.
5. In this example we are selecting the groupid, artifactid, and version of the maven project as follows.
6. After defining all the fields we need to click on the finish button, then it will create a new maven project as follows.
7. In the below example we are checking the pom.xml file of the maven project as follows.
8. After checking the pom.xml file now in this step we are running the maven project as follows.
Conclusion
To create a project in maven, we can also create it by using IDE tools. We can also generate the maven project by using Eclipse IDE, spring tool suite, and other tools. Java project maven has used the archetype plugin for creating the projects. For creating a simple application of java we are using the plugin name maven archetype quickstart.
Recommended Articles
This is a guide to Java Project Maven. Here we discuss the introduction, how to create java project in maven and java applications project maven. You can also look at the following articles to learn more –