EN
VS Code - create Java Maven project
11 points
In this article, we want to show how to create and develop a Java Maven project in VS Code.
- installed VS Code (link),
- installed Java 11 (JDK) or later (link),
- installed Extension Pack for Java in VS Code (link).
Hints:
Presented project configuration provides example configuration that uses:
- Lombok preprocessor to generate automatically constructors, getters, setters, etc. in classes,
- JUnit 4 that lets to write unit tests.

1. Create a directory and go there.
e.g. C:\Projects\my-project
2. Create a Maven project file inside.
e.g. pom.xml
file:
xxxxxxxxxx
1
2
<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">
3
4
<modelVersion>4.0.0</modelVersion>
5
6
<groupId>com.example</groupId>
7
<artifactId>my-project</artifactId>
8
9
<name>My project</name>
10
<description>My project description.</description>
11
<version>0.0.1-SNAPSHOT</version>
12
<packaging>jar</packaging>
13
14
<properties>
15
<java.version>21</java.version>
16
<maven.compiler.source>${java.version}</maven.compiler.source>
17
<maven.compiler.target>${java.version}</maven.compiler.target>
18
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
20
</properties>
21
22
<dependencies>
23
24
<!-- #################### -->
25
<!-- TESTING -->
26
<!-- #################### -->
27
28
<!-- https://mvnrepository.com/artifact/junit/junit -->
29
<dependency>
30
<groupId>junit</groupId>
31
<artifactId>junit</artifactId>
32
<version>4.13.2</version>
33
<scope>test</scope>
34
</dependency>
35
36
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
37
<dependency>
38
<groupId>org.assertj</groupId>
39
<artifactId>assertj-core</artifactId>
40
<version>3.6.2</version>
41
<scope>test</scope>
42
</dependency>
43
44
<!-- #################### -->
45
<!-- PREPROCESSORS -->
46
<!-- #################### -->
47
48
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
49
<dependency>
50
<groupId>org.projectlombok</groupId>
51
<artifactId>lombok</artifactId>
52
<version>1.18.34</version> <!-- use latest dependency version -->
53
<scope>provided</scope>
54
</dependency>
55
56
</dependencies>
57
58
<build>
59
<finalName>${project.artifactId}</finalName>
60
<sourceDirectory>src</sourceDirectory>
61
<testSourceDirectory>test</testSourceDirectory>
62
<plugins>
63
<plugin>
64
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
65
<groupId>org.apache.maven.plugins</groupId>
66
<artifactId>maven-compiler-plugin</artifactId>
67
<version>3.8.1</version> <!-- update plugin version if needed -->
68
<configuration>
69
<annotationProcessorPaths>
70
<path>
71
<groupId>org.projectlombok</groupId>
72
<artifactId>lombok</artifactId>
73
<version>1.18.34</version> <!-- use latest dependency version -->
74
</path>
75
</annotationProcessorPaths>
76
<source>${maven.compiler.source}</source>
77
<target>${maven.compiler.target}</target>
78
<verbose>true</verbose>
79
</configuration>
80
</plugin>
81
<plugin>
82
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin -->
83
<groupId>org.apache.maven.plugins</groupId>
84
<artifactId>maven-jar-plugin</artifactId>
85
<version>3.4.2</version> <!-- update plugin version if needed -->
86
<configuration>
87
<archive>
88
<manifest>
89
<mainClass>com.example.Program</mainClass>
90
</manifest>
91
</archive>
92
</configuration>
93
</plugin>
94
</plugins>
95
</build>
96
97
</project>
Where:
junit
is optional and allows writing Java tests,assertj-core
is optional and provides nice assertions API,lombok
provides additional Java preprocessor (generates constructors, setters, getters, etc. in classes).
3. Create a Java source code file inside.
e.g. src/com/example/Program.java
file:
xxxxxxxxxx
1
package com.example;
2
3
public class Program {
4
5
public static void main(String[] args) {
6
7
System.out.println("Hello world!");
8
}
9
}
4. Create a test source code file inside.
e.g. test/com/example/ProgramTest.java
file:
xxxxxxxxxx
1
package com.example;
2
3
import org.assertj.core.api.Assertions;
4
import org.junit.Test;
5
6
public class ProgramTest {
7
8
9
public void test() {
10
11
String acutal = "Some text here ...";
12
String expected = "Some text here ...";
13
14
Assertions.assertThat(acutal).isEqualTo(expected);
15
}
16
17
// put more tests here ...
18
}
5. Run in the command line:
xxxxxxxxxx
1
mvn clean install
Hint: to skip tests use:
mvn clean install -Dmaven.test.skip=true
6. Open the directory in VS Code.