springboot 使用java+kotlin混合开发,基于maven打包失败

背景

鉴于springboot的快速开发以及kotlin的简介语法,公司新项目使用kotlin进行开发。开发工具使用IDEA,本地运行正常,但是通过maven进行项目打包的时候出现异常

工具

springboot 【2.1.2】  kotlin【1.2.7】jdk【1.8】maven【3.5】

问题分析

查阅网上资料说 运行 

mvn clean kotlin:compile package -Dmaven.test.skip=true,然而并没有什么卵用;

分析:打包问题基本可以先猜测是pom的配置问题,先查看build这块的内容

pom.xml内容如下

	。。。省略部分
    <dependency>
		<groupId>com.fasterxml.jackson.module</groupId>
		<artifactId>jackson-module-kotlin</artifactId>
	</dependency>
	<dependency>
		<groupId>org.jetbrains.kotlin</groupId>
		<artifactId>kotlin-reflect</artifactId>
	</dependency>
	<dependency>
		<groupId>org.jetbrains.kotlin</groupId>
		<artifactId>kotlin-stdlib-jdk8</artifactId>
	</dependency>
 
<build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                    <jvmTarget>1.8</jvmTarget>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

执行mvn package 打包操作,控制台报错:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.2.RELEASE:repackage (default) on project srv-base: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:2.1.2.RELEASE:repackage failed: Unable to find main class -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

Unable to find main class -> [Help 1] :说明未指定运行主类,spring-boot-maven-plugin中指定启动类路径注意:启动类为Kotlin的.kt文件时,指定的类名后需要加上Kt,【com.xxx.xxx.ApplicationKt否则包启动无法还是无法找到主类

 <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.xxx.xxx.ApplicationKt</mainClass><!-- 使用kotlin作为启动类时,com.xxx.xxx.Application 后面是需要加上Kt -->
                </configuration>
            </plugin>

再次打包没有报错,但是运行时还是找不到主类。。。。。查看生成的包内容发现只有java编写的类有生成到包目录下,kotlin文件都未正常生成到包目录中

一脸懵逼,应该是kotlin编译器配置有问题,网上查阅后发现需要指定kotlin编译的项目路径,修改pom中kotlin-maven-plugin 指定资源路径

            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <jvmTarget>1.8</jvmTarget><!-- 需要指定jdk的版本号-->
                </configuration>
                <executions>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                                <!--<sourceDir>${project.basedir}/src/main/java</sourceDir>-->
                            </sourceDirs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                                <!--<sourceDir>${project.basedir}/src/test/java</sourceDir>-->
                            </sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

再次打包-》运行,一切正常

已经被maven玩了好几次,这次又被kkotlin-maven-plugin玩了一次,记录一下避免忘记

参考链接:http://www.kotlincn.net/docs/reference/using-maven.html

 

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Spring Boot 和 Kotlin 更新 API 的详细示例: 1. 创建一个新的 Spring Boot 项目并添加必要的依赖项,如下所示: ```gradle dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.jetbrains.kotlin:kotlin-reflect") } ``` 2. 创建一个名为 `User` 的数据类,用于表示要更新的用户: ```kotlin data class User( val id: Long, val name: String, val email: String ) ``` 3. 创建一个名为 `UserController` 的控制器类,用于处理用户更新请求: ```kotlin @RestController @RequestMapping("/api/users") class UserController(private val userRepository: UserRepository) { @PutMapping("/{id}") fun updateUser(@PathVariable id: Long, @RequestBody user: User): User { val existingUser = userRepository.findById(id) .orElseThrow { EntityNotFoundException("User with id $id not found") } existingUser.name = user.name existingUser.email = user.email return userRepository.save(existingUser) } } ``` 4. 创建一个名为 `UserRepository` 的存储库接口,用于访问用户数据: ```kotlin @Repository interface UserRepository : JpaRepository<UserEntity, Long> ``` 5. 创建一个名为 `UserEntity` 的实体类,用于映射到数据库中的用户表: ```kotlin @Entity @Table(name = "users") data class UserEntity( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Long = 0, @Column(nullable = false) var name: String, @Column(nullable = false, unique = true) var email: String ) ``` 6. 运行应用程序并使用 cURL 或其他 HTTP 客户端发出更新用户的请求: ```bash curl -X PUT -H "Content-Type: application/json" -d '{"name": "John Doe", "email": "john.doe@example.com"}' http://localhost:8080/api/users/1 ``` 这将更新 ID 为 1 的用户的名称和电子邮件,并返回更新后的用户对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值