In this tutorial, we will walk through the process of creating a simple Java project using Maven from the terminal.
- Java Development Kit (JDK) installed on your system
- Maven installed on your system
Open your terminal or command prompt.
Navigate to the directory where you want to create your Java project using the cd command:
cd /path/to/your/project/directoryRun the following Maven command to generate a basic Java project using the default archetype:
mvn archetype:generate
\-DgroupId=com.example
\-DartifactId=my-java-project
\-DarchetypeArtifactId=maven-archetype-quickstart
\-DinteractiveMode=falseReplace com.example with your desired Group ID and my-java-project with your desired Artifact ID.
Navigate into the newly created project directory:
cd my-java-projectOpen the pom.xml file in your project directory and add the following properties inside the <properties> section:
Note that you should change the
maven.compiler.sourceandmaven.compiler.targetproperties to suit the JDK version that you're running.
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>These properties set the Java source and target versions to 1.8 and configure the source file encoding to UTF-8.
Use Maven to clean and build your project:
mvn clean installThis command will compile your project, run tests, and package it into a JAR file.
You should see output indicating that the build was successful and that tests were executed.
Inside your project directory, you'll find the source code under src/main/java, with a simple Java class named App.java.
You can modify the source code in App.java and add more Java classes as needed.
You have successfully created a simple Java project using Maven from the terminal. By configuring Maven properties, you ensure compatibility and smooth build processes for your project. You can now continue developing your project, adding dependencies, and expanding its functionality.