【问题标题】:Maven: excluding files from compilation doesn't work in IntelliJ IDEAMaven:从编译中排除文件在 IntelliJ IDEA 中不起作用
【发布时间】:2017-05-26 11:22:38
【问题描述】:

我想暂时从编译中排除一些目录,所以我配置了maven-compiler-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/foo/**/*.java</exclude>
        </excludes>
    </configuration>
</plugin>

在命令行和 Eclipse 中一切正常,但在 IntelliJ IDEA 中,我在排除目录中遇到编译错误。知道可能导致问题的原因吗?

【问题讨论】:

  • 为什么?将这些文件移动到其他模块或删除它们..(我希望您使用的是版本控制系统)...
  • 是的,我当然可以。但在这种特殊情况下,暂时禁用编译某些包,然后重新启用它们而不移动目录树的大部分内容要容易得多。

标签: java maven intellij-idea


【解决方案1】:

这是open issue,请关注更新。

【讨论】:

  • 谢谢!我将跟踪此问题的进展,同时我将遵循@khmarbaise 的建议。
【解决方案2】:

我在迁移 Maven 项目时遇到了同样的问题。 我通过选择以下选项使其工作:

文件 > 设置 > 构建、执行、部署 > 构建工具 > Maven > 亚军

选中'将 IDE 构建/运行操作委托给 Maven' 选项。

此处提供说明: IntelliJ documentation

【讨论】:

    【解决方案3】:

    我在尝试解决 IntelliJ IDEA 中的 Java 9 模块信息编译时发现了这个问题。

    我希望我的library 使用 Java 8 编译,但同时我希望它具有模块信息(只能使用 JDK 9+ 编译)。这个问题的解决方案suggested by Maven:使用 JDK 8 从编译中排除模块信息,如果使用 JDK 9,则包括。它适用于命令行,但 IDEA 无法编译此类项目。可以通过将模块信息添加到 Excludes 来调整 IDEA 编译配置,但我想要更清晰的解决方案。

    可以通过添加带有扩展源目录列表的 Maven 配置文件来欺骗 IDEA:

    <profile>
        <!-- This profile enables two pass compilation:
             1. Compile all sources with Java 9 target version (including module-info.java)
             2. Recompile all sources with Java 8 target version, but module-info.java
             After this all classes will have Java 8 bytecode (version 52), while
             module-info.class will have Java 9 bytecode (version 53) -->
        <id>J9-module</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <release>8</release>
                    </configuration>
                    <executions>
                        <execution>
                            <id>default-compile</id>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                            <phase>compile</phase>
                            <configuration>
                                <compileSourceRoots>
                                    <sourceRoot>${project.basedir}/src/main/java</sourceRoot>
                                    <sourceRoot>${project.basedir}/src/main/java9</sourceRoot>
                                </compileSourceRoots>
                                <release>9</release>
                            </configuration>
                        </execution>
                        <execution>
                            <id>java-8-recompile</id>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                            <phase>compile</phase>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    

    使用上面显示的配置文件,IDEA 本身将只编译标准src/main/java。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 2013-11-04
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      相关资源
      最近更新 更多