Convert a Java project to Maven project in IntelliJ IDEA
This article shows how to convert a Java project to a Maven project in IntelliJ IDEA.
Table of contents
- 1. A Non-Maven Project
- 2. Add Framework Support – Maven
- 3. Update the default pom.xml
- 4. Reload the Maven project
- 5. References
P.S Tested with IntelliJ IDEA 2021.1.1
1. A Non-Maven Project
A non-Maven project (without pom.xml
), but the directory structure follows the Maven standard directory.
src/main/java
src/main/resources
src/test/java
src/test/resources
Later we will convert the above non-Maven project into a Java project support Maven features in IntelliJ IDEA.
2. Add Framework Support – Maven
Right clicks on the project name, clicks Add Framework Support...
, and then selects Maven
.
3. Update the default pom.xml
3.1 The IntelliJ IDEA will generate a default pom.xml
and configure the project to support Maven.
This is the default 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>groupId</groupId>
<artifactId>jersey-jetty</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
3.2 We should update the pom.xml
by adding dependencies
and plugins
. The below updated pom.xml
adds support for Java 11, and JUnit 5 for testing.
<?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.mkyong</groupId>
<artifactId>jersey-jetty</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
<junit.version>5.4.0</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>project name</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
4. Reload the Maven project
4.1 If the Maven view is hidden, we can reopen it by selecting the View
-> Tool Windows
-> Maven
4.2 Clicks the reload Maven project
icon to reload everything.
IntelliJ IDEA should configure the project to support Maven features automatically, done.