Skip to content

Instantly share code, notes, and snippets.

@IdelsTak
Last active October 12, 2023 06:29
Show Gist options
  • Save IdelsTak/c0528e8046ff0856e4e29feaa1a582df to your computer and use it in GitHub Desktop.
Save IdelsTak/c0528e8046ff0856e4e29feaa1a582df to your computer and use it in GitHub Desktop.
Create a simple Java project using Maven on the terminal

Creating a Simple Java Project Using Maven in Terminal

In this tutorial, we will walk through the process of creating a simple Java project using Maven from the terminal.

Prerequisites

  • Java Development Kit (JDK) installed on your system
  • Maven installed on your system

Steps

1. Open Terminal

Open your terminal or command prompt.

2. Create a Project Directory

Navigate to the directory where you want to create your Java project using the cd command:

cd /path/to/your/project/directory

3. Create the Maven Project

Run 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=false

Replace com.example with your desired Group ID and my-java-project with your desired Artifact ID.

4. Navigate to the Project Directory

Navigate into the newly created project directory:

cd my-java-project

5. Configure Maven Properties

Open 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.source and maven.compiler.target properties 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.

6. Build the Project

Use Maven to clean and build your project:

mvn clean install

This command will compile your project, run tests, and package it into a JAR file.

7. Review the Output

You should see output indicating that the build was successful and that tests were executed.

8. Explore the Project

Inside your project directory, you'll find the source code under src/main/java, with a simple Java class named App.java.

9. Customize Your Project

You can modify the source code in App.java and add more Java classes as needed.

Conclusion

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment