IntelliJ - Convert a Java project/module into a Maven project/module
Asked Answered
M

13

450

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.)

Mesozoic answered 4/10, 2011 at 1:36 Comment(0)
I
806

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.

Incertitude answered 4/10, 2011 at 2:7 Comment(11)
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.Mesozoic
Cool. But you shouldn't have to add that; it's already the default.Incertitude
What happens if I don't have that option?Papa
@LukeyBoylsXen Check your plugins. Should be there, though.Incertitude
which "plugins"? I have 2 maven plugs (came default with IntelliJ). Still no "Maven" option in Add FrameworksSeise
@Seise It's quite likely the IDE has changed a bit in the last five years.Incertitude
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.Seise
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.Middlebreaker
@Middlebreaker There's probably an archetype for JSF projects, and using an archetype is one of the options IIRC.Incertitude
@DaveNewton that was my first idea, but i'm having issues with that, i made a question about it.Middlebreaker
@Seise - 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 automaticallyNawab
N
82

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.

Notum answered 27/3, 2017 at 20:15 Comment(0)
R
37
  1. Open 'Maven projects' (tab on the right side).
  2. Use 'Add Maven Projects'
  3. Find your pom.xml
Rizo answered 22/11, 2016 at 13:50 Comment(3)
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.Revere
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.Intelligible
Great hint, I was moving projects under the parent and "Add framework support" was not available for me. Your solution works very nice.Hydrus
H
10

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

Hirsute answered 12/8, 2020 at 6:9 Comment(0)
E
7

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
Econometrics answered 7/4, 2017 at 7:25 Comment(0)
M
5

I could not find the option 'Add Framework Support' at all. I figured out if you click on the project, then press Ctrl+Shift+A, a dialog pops up.

Start typing 'Add Framework Support' and the option will appear in a list. Click that option then you can add Maven support.

Monarchist answered 5/12, 2023 at 14:40 Comment(0)
T
3

This fixed it for me: Open maven projects tab on the right. Add the pom if not yet present, then click refresh on the top left of the tab.

Tinker answered 18/8, 2017 at 7:54 Comment(0)
L
3

I had a different scenario, but still landed on this answer.
I had imported my root project folder containing multiple Maven projects but also some other stuff used in this project.
IntelliJ recognised the Java files, but didn't resolve the Maven dependencies.

I fixed this by performing a right-click on each pom and then "Add as maven project"

Ledbetter answered 6/3, 2018 at 9:49 Comment(0)
A
3

I have resolved this same issue by doing below steps:

  1. File > Close Project

  2. Import Project

  3. Select you project via the system file popup

  4. Check "Import project from external model" radio button and select Maven entry

  5. And some Next buttons (select JDK, ...)

Then the project will be imported as Maven module.

Agnesse answered 3/4, 2019 at 19:37 Comment(0)
C
1

The easiest way is to add the project as a Maven project directly. To do this, in the project explorer on the left, right-click on the POM file for the project, towards the bottom of the context menu, you will see an option called 'Add as Maven Project', click it. This will automatically convert the project to a Maven project

Ceruse answered 18/7, 2019 at 8:57 Comment(0)
P
1
  1. Right-click on the module
  2. Select "Add framework support"
  3. Click the "Maven" option.

Note: This will convert the current project into maven driven project create the pom.xml file automatically. The dependencies can be added then.

Enable Auto Import of Dependencies

(InteliJ IDEA 14.1.1 on Ubuntu)

Settings > Build, Execution, Deployment > Build Tools > Maven > Importing > [X] Import Maven projects automatically

Run as maven project

Creating a new Run/Debug Configuration of type maven and adding the command clean install

Finally clicking the green button to run that Run/Debug Configuration, this will run build the project using apache maven.

Prognathous answered 26/4, 2022 at 10:13 Comment(1)
there is no maven optionCoalesce
S
1

Using IntelliJ Idea 2022.3.3, here is the easiest way you can do it:

  • Right click on root of your project in Explorer on the left side.
  • Select "Add framework support" (3rd option from the last as shown in image)
  • Choose Maven enter image description here
Spendthrift answered 21/5, 2023 at 12:29 Comment(4)
there is no maven option, nor maven tool window as others are suggesting when you have a plain java project, so none of this is workingCoalesce
@Coalesce which version of intellij idea are you using? Did you follow the steps I mentioned in my answer above?Spendthrift
the latest, and yes I did follow. There is simply no maven option when you click on Add framework supportCoalesce
@Coalesce I encountered similar issue. I had to follow these steps to add Maven support but it's far from obvious: jetbrains.com/help/idea/…Coupon
H
0

Update on version Intellij IDEA 2020.2.3

Settings -> Build,Execution,Deployment -> Build Tools -> Maven -> Importing and click "create module groups for multi-module Maven projects".

Then Apply and you can see that option in "Add Framework Support..."

enter image description here

Holozoic answered 18/7, 2021 at 1:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.