如何指定maven编译器插件顺序

soc*_*qwe 3 java maven kotlin

我正在使用maven进行混合java和kotlin项目.

我现在面临的问题是,maven-compiler-plugin在编译之前运行kotlin-maven-plugin.

[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @annotation --- 
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 11 source files to /Users/hannes/workspace/tikxml/annotation/target/classes 
[INFO] --- kotlin-maven-plugin:1.0.0-beta-4583:compile (compile) @ annotation 
[INFO] Kotlin Compiler version 1.0.0-beta-4583
Run Code Online (Sandbox Code Playgroud)

在我的java源代码中,我引用了用kotlin编写的类.但是javac在kotlinc之前运行.因此,maven会因编译器错误而中断.

我的pom(父pom,我使用子模块)看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>7</version>
    </parent>

    ...

    <modules>
        <module>core</module>
        <module>annotation</module>
        <module>processor</module>
    </modules>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
        <kotlin.version>1.0.0-beta-4583</kotlin.version>
    </properties>


    <build>
        <pluginManagement>
            <plugins>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.0</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
            </plugins>


        </pluginManagement>

        <plugins>

            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <source>src/main/java</source>
                            </sourceDirs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <source>src/test/java</source>
                            </sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

Chr*_*oth 6

关于使用Maven的Kotlin文档建议将kotlin-maven-plugin执行明确地绑定到process-sources阶段.由于maven-compiler-plugin绑定到compile阶段,绑定kotlin-maven-pluginprocess-sources阶段使其首先运行.

<plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <version>${kotlin.version}</version>

    <executions>
        <execution>
            <id>compile</id>
            <phase>process-sources</phase>
            <goals> <goal>compile</goal> </goals>
        </execution>

        <execution>
            <id>test-compile</id>
            <phase>process-test-sources</phase>
            <goals> <goal>test-compile</goal> </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)


hee*_*nee 6

虽然它更冗长,但您可以compile使用以下配置在阶段中编译 Java 和 Kotlin 源代码:

<plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <version>${kotlin.version}</version>
    <executions>
        <execution>
            <id>kotlin-compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>kotlin-test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5</version>
    <executions>
        <execution>
            <id>default-compile</id>
            <phase>none</phase>
        </execution>
        <execution>
            <id>default-testCompile</id>
            <phase>none</phase>
        </execution>
        <execution>
            <id>java-compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>java-test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

通常情况下,插件是在它们在POM声明的顺序执行的,但在执行default-compiledefault-testCompile是特殊的,因为他们是内置的,所以他们总是先走。上面的配置通过禁用默认编译执行来解决这个问题,并maven-compiler-pluginkotlin-maven-plugin. 通过这种方式,您可以compile在构建生命周期的阶段正确地进行所有编译。