qiubix
qiubix

Reputation: 1332

Maven: excluding files from compilation doesn't work in IntelliJ IDEA

I want to temporarily exclude some directory from compilation, so I configure maven-compiler-plugin:

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

Everything works fine from the command line and Eclipse, but in IntelliJ IDEA I got compilation errors in excluded directory. Any idea what might be causing the problem?

Upvotes: 5

Views: 2640

Answers (3)

Denis Kokorin
Denis Kokorin

Reputation: 895

I found this question while trying to solve Java 9 module-info compilation in IntelliJ IDEA.

I want my library compiled with Java 8, but at the same time I want it to have module-info (which can be compiled only with JDK 9+). A solution of this problem suggested by Maven: exclude module-info from compilation with JDK 8 and include if JDK 9 is used. It works with command line but IDEA fails to compile such project. It is possible to adjust IDEA Compile configuration by adding module info to Excludes, but I wanted more clear solution.

It is possible to trick IDEA by adding Maven Profile with extended list of source directories:

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

With the profile shown above IDEA itself will compile only standard src/main/java.

Upvotes: 0

Sivabalan
Sivabalan

Reputation: 3016

I had the same issue migrating maven projects. I got it working by choosing below option:

File > Settings > Build, Execution, Deployment > Build Tools > Maven > Runner

Check the 'Delegate IDE build/run actions to Maven' option.

Instruction is available here: IntelliJ documentation

Upvotes: 2

CrazyCoder
CrazyCoder

Reputation: 402055

It's an open issue, please follow for updates.

Upvotes: 3

Related Questions