首页 文章

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

提问于
浏览
338

我有一个关于Bitbucket的项目 . 只提交了来源 . 为了将项目检索到新机器上,我在IntelliJ中使用了Version Control> Checkout from Version Control .

然后询问我是否要从此源创建一个新项目,我回复是 . 到现在为止还挺好 . 它为我创建了一个很好的小Java项目,由一个模块组成 .

但是,我将此项目纳入IntelliJ的目标是将其转变为Maven项目 . 我无法在任何地方找到任何可以让我这样做的选项!

有没有办法让IntelliJ为我生成一个基本的空pom.xml,具有名称和artifactId以及存储库?或者,有没有办法将项目作为Maven项目导入? (每当我尝试从现有源创建项目时,它只给我一个Java项目的选项 . )

6 回答

  • 26

    这为我修好了:右边打开maven项目标签 . 添加pom(如果尚未存在),然后单击选项卡左上角的“刷新” .

  • 53

    对于那些从中受益的人的视觉效果 .

    enter image description here

    右键单击项目名称(在此示例中为“test”)后,选择“Add framework support”并选中“Maven”选项 .

  • 3

    我想添加一个重要的提示,即转换这样的项目可能会产生副作用,当你有一个更大的项目时,这些副作用是显而易见的 . 这是因为Intellij Idea(2017)仅从pom.xml中获取了一些重要设置,这可能会导致一些混乱,以下部分至少受到影响:

    • 更改了模块的注释设置

    • 模块的编译器输出路径已更改

    • 资源设置完全被忽略,仅从pom.xml中获取

    • 模块依赖关系搞砸了,必须检查

    • 语言/编码设置已更改为模块

    所有这些要点都需要审查和调整,但在此之后它就像魅力一样 .

    更不幸的是,没有创建足够的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>
        <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>
    
  • 1
    • 打开'Maven projects'(右侧标签) .

    • 使用'Add Maven Projects'

    • 找到你的pom.xml

  • 671

    右键单击该模块,选择“添加框架支持...”,然后选中“Maven”技术 .

    (这也会创建 pom.xml 供您修改 . )

    如果您的意思是添加源存储库元素,我认为您需要手动执行此操作 - 不确定 .

    预IntelliJ 13这将不会将项目转换为Maven Standard Directory Layout,13它将 .

  • 0

    我有一个不同的场景,但仍然落在这个答案上 .
    我导入了包含多个Maven项目的根项目文件夹,还导入了此项目中使用的其他一些内容 .
    IntelliJ识别Java文件,但没有解析Maven依赖项 .

    我通过右键单击每个pom然后“添加为maven项目”来修复此问题

相关问题