且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Maven编译Java + Kotlin项目?

更新时间:2022-05-10 03:15:49

确保你在pom.xml的< build> 中有这个声明

Make sure you have this declaration in <build> of your pom.xml

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

确保所有文件夹(src / main& 3x中为3x在配置实际中提到的src / test),即使它们不包含任何类/资源。一旦它适合你,你仍然可以微调配置。

Make sure that all folders (3x in src/main & 3x in src/test) mentioned in the configuration actually exist, even if they don’t contain any classes/resources. You can still fine-tune the configuration once it works for you.

还要注意使用我上面提到的完全相同的顺序让编译器首先编译Java代码。

Also pay attention to use exactly the same order I mentioned above to let the compiler compile the Java code first.