Published on

Start a Java project with Maven manually (Based on Ubuntu)

Authors
public class Test {
  public static void main(String[] arg) {
    System.out.println("Hello, Java");
  }
}

When I use Neovim with the JDTLS (Java Development Tools Language Server) for writing Java code, Neovim provides reminders like the following:

Test.java is a non-project file, only syntax errors are reported.

image-20250111135420624

So I start a Java project with maven.

Install Maven

Update System Packages and install Maven, you can also use apt-get

sudo apt update && sudo apt upgrade -y
sudo apt install maven
mvn -v

Create a project manually

  1. create a empty folder
mkdir hello-maven
cd hello-maven
  1. create a pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>hello-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

  1. create project structure
mkdir -p src/main/java/com/example
cd src/main/java/com/example
  1. just write a simple application
vim App.java
package com.example;

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

  1. Navigate to the project root directory and execute the Maven command to compile and build the project.
mvn clean install
mvn exec:java -Dexec.mainClass="com.example.App"