IntelliJ - Convert a Java project/module into a Maven project/module

239,292

Solution 1

Right-click on the module, select "Add framework support...", and check the "Maven" technology.

enter image description here

enter image description here

(This also creates a pom.xml for you to modify.)

If you mean adding source repository elements, I think you need to do that manually–not sure.

Pre-IntelliJ 13 this won't convert the project to the Maven Standard Directory Layout, 13+ it will.

Solution 2

A visual for those that benefit from it.

enter image description here

After right-clicking the project name ("test" in this example), select "Add framework support" and check the "Maven" option.

Solution 3

  1. Open 'Maven projects' (tab on the right side).
  2. Use 'Add Maven Projects'
  3. Find your pom.xml

Solution 4

Just follow the steps:

  1. Right click to on any module pox.xml and then chose "Add as Maven Project"

enter image description here

  1. Next to varify it, go to the maven tab, you will see the project with all maven goal which you can use:

enter image description here

Solution 5

I want to add the important hint that converting a project like this can have side effects which are noticeable when you have a larger project. This is due the fact that Intellij Idea (2017) takes some important settings only from the pom.xml then which can lead to some confusion, following sections are affected at least:

  1. Annotation settings are changed for the modules
  2. Compiler output path is changed for the modules
  3. Resources settings are ignored totally and only taken from pom.xml
  4. Module dependencies are messed up and have to checked
  5. Language/Encoding settings are changed for the modules

All these points need review and adjusting but after this it works like charm.

Further more unfortunately there is no sufficient pom.xml template created, I have added an example which might help to solve most problems.

<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>Name</groupId>
<artifactId>Artifact</artifactId>
<version>4.0</version>
<properties>
    <!-- Generic properties -->
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
    <!--All dependencies to put here, including module dependencies-->
</dependencies>
<build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <testSourceDirectory> ${project.basedir}/src/test/java</testSourceDirectory>

    <resources>
        <resource>
            <directory>${project.basedir}/src/main/java</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <annotationProcessors/>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Edit 2019:

  • Added recursive resource scan
  • Added directory specification which might be important to avoid confusion of IDEA recarding the content root structure
Share:
239,292
Neil Traft
Author by

Neil Traft

A software engineer with &gt;10 years of professional experience in Python, Java, and C++. I have experience in robotics, machine learning, cloud computing, and statistical data analysis. In a prior life I had some experience in 3D visualization, user interfaces, and Android apps. I hold a master's degree in computer science with a focus on robotics. ntraft.com @ntraft

Updated on July 08, 2022

Comments

  • Neil Traft
    Neil Traft almost 2 years

    I have a project on Bitbucket. Only the sources are committed. To retrieve the project onto a new machine, I used Version Control > Checkout from Version Control from within IntelliJ.

    It then asks whether I would like to create a new project from this source, to which I reply Yes. So far, so good. It creates a nice little Java project for me, consisting of a single module.

    However, my goal in pulling this project into IntelliJ was to turn it into a Maven project. I cannot find any option anywhere that will let me do this!

    Is there a way to have IntelliJ just generate a basic empty pom.xml for me, with a name and an artifactId and a repository? Or, is there a way to import the project as a Maven project in the first place? (Whenever I try to create a project from existing source, it only gives me the option of a Java project.)

  • Neil Traft
    Neil Traft over 12 years
    Exactly what I was looking for! And yeah, I meant adding the default maven2 "Central" repository element. But I can add that myself, no big deal.
  • Dave Newton
    Dave Newton over 12 years
    Cool. But you shouldn't have to add that; it's already the default.
  • Admin
    Admin over 9 years
    What happens if I don't have that option?
  • Dave Newton
    Dave Newton over 9 years
    @LukeyBoylsXen Check your plugins. Should be there, though.
  • Nick
    Nick about 7 years
    which "plugins"? I have 2 maven plugs (came default with IntelliJ). Still no "Maven" option in Add Frameworks
  • Dave Newton
    Dave Newton about 7 years
    @Nick It's quite likely the IDE has changed a bit in the last five years.
  • Nick
    Nick about 7 years
    Re: no "Maven" option: i believe it is due to working on an IntelliJ plugin for which there is no maven support. @Dave Newton - you make a good point, always need to consider that for older questions and answers.
  • MrSir
    MrSir over 6 years
    sorry for reviving this old post but is there an automatic way to put the jsf dependencies into the pom ? The pom gets created but it's empty, i would like to have all the proper dependencies of a jsf project inside the pom.
  • Dave Newton
    Dave Newton over 6 years
    @MrSir There's probably an archetype for JSF projects, and using an archetype is one of the options IIRC.
  • MrSir
    MrSir over 6 years
    @DaveNewton that was my first idea, but i'm having issues with that, i made a question about it.
  • Petronella
    Petronella over 5 years
    @Nick - Maven can be added as a framework to be selected, or made visible in the "Add Framework Support" by changing the intellij Settings: File -> Settings-> Build, Execution, Deployment -> Build Tools -> Maven -> Importing: tick Import Maven projects automatically
  • Sylvester
    Sylvester about 5 years
    When I had a parent director git URL when many sub projects, the sub projects were not recognized as Maven. This answer helped. To make it clear, this functionality is available on the Maven Tool Window.
  • vphilipnyc
    vphilipnyc almost 4 years
    This answer helped me to re-associate a Gradle project back to Maven. Just needed to unlink it (use the minus sign) on the analogous Gradle menu.