Maven SpringBoot集成Kotlin

159 ·
0 ·
2021-05-13 14:16
最新一次编辑的原因:

添加依赖:

		<!-- kotlin -->
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>

添加构建插件:

                <plugin>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <configuration>
                        <args>
                            <arg>-Xjsr305=strict</arg>
                        </args>
                        <compilerPlugins>
                            <plugin>spring</plugin>
                            <plugin>jpa</plugin>
                            <plugin>all-open</plugin>
                        </compilerPlugins>
                        <pluginOptions>
                            <option>all-open:annotation=javax.persistence.Entity</option>
                        </pluginOptions>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.jetbrains.kotlin</groupId>
                            <artifactId>kotlin-maven-allopen</artifactId>
                            <version>${plugin.maven.kotlin.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>org.jetbrains.kotlin</groupId>
                            <artifactId>kotlin-maven-noarg</artifactId>
                            <version>${plugin.maven.kotlin.version}</version>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <id>kapt</id>
                            <goals>
                                <goal>kapt</goal>
                            </goals>
                            <configuration>
                                <sourceDirs>
                                    <sourceDir>src/main/java</sourceDir>
                                </sourceDirs>
                                <annotationProcessorPaths>
                                    <annotationProcessorPath>
                                        <groupId>org.springframework.boot</groupId>
                                        <artifactId>spring-boot-configuration-processor</artifactId>
                                        <version>${project.parent.version}</version>
                                    </annotationProcessorPath>
                                </annotationProcessorPaths>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

插件版本配置:

	<properties>

        <plugin.maven.kotlin.version>1.2.71</plugin.maven.kotlin.version>
    </properties>

 

编写测试服务:


@Service
open class KotlinService {

    open fun helloWord(): String {
        return "kotlin,hello upupor";
    }

}

 

编写单元测试:

    @Test
    public void testKotlin(){
        String s = kotlinService.helloWord();
        System.out.println(s);
    }

结果:

 


本作品系原创,采用《署名-非商业性使用-禁止演绎4.0 国际》许可协议.转载请说明出处
本文链接:https://www.upupor.com/u/21051314083072156672 复制

无评论内容,快来评论吧