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

本文翻译自:IntelliJ - Convert a Java project/module into a Maven project/module

I have a project on Bitbucket. 我有一个关于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. 为了将项目检索到新机器上,我在IntelliJ中使用了Version Control> Checkout from Version Control。

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项目的选项。)


#1楼

参考:https://stackoom.com/question/W49Q/IntelliJ-将Java项目-模块转换为Maven项目-模块


#2楼

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

#3楼

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”选项。


#4楼

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混淆内容根结构可能很重要

#5楼

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(如果尚不存在),然后单击选项卡左上方的刷新。


#6楼

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项目”来解决此问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值