简体   繁体   English

IntelliJ - 将 Java 项目/模块转换为 Maven 项目/模块

[英]IntelliJ - Convert a Java project/module into a Maven project/module

I have a project on Bitbucket. Only the sources are committed.我在 Bitbucket 上有一个项目。只提交了源代码。 To retrieve the project onto a new machine, I used Version Control > Checkout from Version Control from within IntelliJ.为了将项目检索到新机器上,我在 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.它为我创建了一个不错的小 Java 项目,由一个模块组成。

However, my goal in pulling this project into IntelliJ was to turn it into a Maven project.但是,我将这个项目拉入 IntelliJ 的目的是将其变成一个 Maven 项目。 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?有没有办法让 IntelliJ 为我生成一个基本的空 pom.xml,其中包含一个名称、一个 artifactId 和一个存储库? Or, is there a way to import the project as a Maven project in the first place?或者,有没有办法首先将项目导入为 Maven 项目? (Whenever I try to create a project from existing source, it only gives me the option of a Java project.) (每当我尝试从现有源创建项目时,它只会给我 Java 项目的选项。)

Right-click on the module, select "Add framework support...", and check the "Maven" technology.右击模块,选择“添加框架支持...”,勾选“Maven”技术。

在此处输入图片说明

在此处输入图片说明

(This also creates a pom.xml for you to modify.) (这也会创建一个pom.xml供您修改。)

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.在 IntelliJ 13 之前,这不会将项目转换为Maven 标准目录布局,13+ 它会。

A visual for those that benefit from it.为那些从中受益的人提供视觉效果。

在此处输入图片说明

After right-clicking the project name ("test" in this example), select "Add framework support" and check the "Maven" option.右键单击项目名称(本例中为“test”)后,选择“添加框架支持”并勾选“Maven”选项。

  1. Open 'Maven projects' (tab on the right side).打开“Maven 项目”(右侧的选项卡)。
  2. Use 'Add Maven Projects'使用“添加 Maven 项目”
  3. Find your pom.xml找到你的 pom.xml

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:这是因为 Intellij Idea (2017) 仅从 pom.xml 中获取了一些重要设置,这可能会导致一些混乱,以下部分至少会受到影响:

  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资源设置被完全忽略,只取自 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.更不幸的是,没有创建足够的 pom.xml 模板,我添加了一个可能有助于解决大多数问题的示例。

<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: 2019年编辑:

  • Added recursive resource scan添加递归资源扫描
  • Added directory specification which might be important to avoid confusion of IDEA recarding the content root structure添加了目录规范,这对于避免 IDEA 重新记录内容根结构很重要

Just follow the steps:只需按照以下步骤操作:

  1. Right click to on any module pox.xml and then chose "Add as Maven Project"右键单击任何模块 pox.xml,然后选择“添加为 Maven 项目”

在此处输入图片说明

  1. Next to varify it, go to the maven tab, you will see the project with all maven goal which you can use:接下来要对其进行更改,转到 maven 选项卡,您将看到包含所有可以使用的 maven 目标的项目:

在此处输入图片说明

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.我已经导入了包含多个 Maven 项目以及该项目中使用的其他一些内容的根项目文件夹。
IntelliJ recognised the Java files, but didn't resolve the Maven dependencies. IntelliJ 识别了 Java 文件,但没有解析 Maven 依赖项。

I fixed this by performing a right-click on each pom and then "Add as maven project"我通过在每个 pom 上执行右键单击然后“添加为 Maven 项目”来解决此问题

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选中“从外部模型导入项目”单选按钮并选择 Maven 条目

  5. And some Next buttons (select JDK, ...)和一些下一步按钮(选择 JDK,...)

Then the project will be imported as Maven module.然后项目将作为 Maven 模块导入。

This fixed it for me: Open maven projects tab on the right.这为我修复了它:打开右侧的 maven 项目选项卡。 Add the pom if not yet present, then click refresh on the top left of the tab.如果 pom 尚不存在,请添加该 pom,然后单击选项卡左上角的刷新。

The easiest way is to add the project as a Maven project directly.最简单的方法是直接将项目添加为Maven项目。 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.为此,在左侧的项目资源管理器中,右键单击项目的 POM 文件,在上下文菜单的底部,您将看到一个名为“添加为 Maven 项目”的选项,单击它。 This will automatically convert the project to a Maven project这将自动将项目转换为 Maven 项目

Update on version Intellij IDEA 2020.2.3 Intellij IDEA 2020.2.3 版本更新

Settings -> Build,Execution,Deployment -> Build Tools -> Maven -> Importing and click " create module groups for multi-module Maven projects ". Settings -> Build,Execution,Deployment -> Build Tools -> Maven -> Importing并单击“为多模块 Maven 项目创建模块组”。

Then Apply and you can see that option in "Add Framework Support..."然后应用,您可以在“添加框架支持...”中看到该选项

在此处输入图片说明

  1. Right-click on the module右键单击模块
  2. Select "Add framework support" Select “添加框架支持”
  3. Click the "Maven" option.单击“Maven”选项。

Note: This will convert the current project into maven driven project create the pom.xml file automatically.注意:这会将当前项目转换为 maven 驱动项目自动创建 pom.xml 文件。 The dependencies can be added then.然后可以添加依赖项。

Enable Auto Import of Dependencies启用依赖项的自动导入

( InteliJ IDEA 14.1.1 on Ubuntu ) (Ubuntu 上的Ubuntu InteliJ IDEA 14.1.1

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

Run as maven project作为 maven 项目运行

Creating a new Run/Debug Configuration of type maven and adding the command clean install创建类型为 maven 的新运行/调试配置并添加命令全新clean install

Finally clicking the green button to run that Run/Debug Configuration, this will run build the project using apache maven .最后单击绿色按钮运行该运行/调试配置,这将使用apache maven运行构建项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM