ChatGPT解决这个技术问题 Extra ChatGPT

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

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


G
Gaurav Khurana

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

https://i.stack.imgur.com/sC2ix.png

https://i.stack.imgur.com/RK3xx.png

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


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.
Cool. But you shouldn't have to add that; it's already the default.
What happens if I don't have that option?
which "plugins"? I have 2 maven plugs (came default with IntelliJ). Still no "Maven" option in Add Frameworks
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.
k
kgui

A visual for those that benefit from it.

https://i.stack.imgur.com/3GCAR.png

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


F
FazoM

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


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.
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.
I
Indrajeet Gour

Just follow the steps:

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

https://i.stack.imgur.com/PMMmb.png

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

https://i.stack.imgur.com/MrjeF.png


T
Thomas

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:

Annotation settings are changed for the modules Compiler output path is changed for the modules Resources settings are ignored totally and only taken from pom.xml Module dependencies are messed up and have to checked 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


L
LostMage

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"


Z
Zoe stands with Ukraine

I have resolved this same issue by doing below steps:

File > Close Project Import Project Select you project via the system file popup Check "Import project from external model" radio button and select Maven entry And some Next buttons (select JDK, ...)

Then the project will be imported as Maven module.


Z
ZoltanT

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.


T
Testilla

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


A
Alex Yao

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

https://i.stack.imgur.com/4ye4x.png


D
Du-Lacoste

Right-click on the module Select "Add framework support" 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.