首页 > 未分类 > 关于我用Kotlin来写Quarkus的那些配置玩意儿

关于我用Kotlin来写Quarkus的那些配置玩意儿

Quarkus是什么就不说了,懂的都懂,不懂的也不用看我这篇文章()

 

额咳!

好吧,虽然说Quarkus官方网站里的示例全都是用Java写的,但是下面也说了对Kotlin有支持, 所以用起来也没遇到太多的坑。

文档里的代码用例在脑子里转换一下就差不多了。

这里算是个记录,不是什么正儿八经的教程向。

 

下面正式开始,框架为Quarkus,ORM层用的是Ktorm。

 

环境&框架版本: IDEA 2021.3、Java 17、Kotlin 1.6.10、Maven 3.8.4、Ktorm 3.2.0、Quarkus 2.5.3.final

是否支持Java&Kotlin混编:是

 

这篇博客展示的就是一个基本的项目框架,像是HTTP请求啊、基本的CRUD啊、单元测试都跑通了。

下面是Maven父子级的配置:

 

父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>
    <packaging>pom</packaging>

    <groupId>xyx.xxx</groupId>
    <artifactId>sk-system</artifactId>
    <name>sk-system</name>
    <version>v1.0.0</version>

    <properties>
        <java.version>17</java.version>
        <maven.test.skip>true</maven.test.skip>
        <maven.compiler.parameters>true</maven.compiler.parameters>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <kotlin.version>1.6.10</kotlin.version>
        <ktorm.version>3.2.0</ktorm.version>

        <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
        <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
        <quarkus.platform.version>2.5.3.Final</quarkus.platform.version>


        <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
        <compiler-plugin.version>3.8.1</compiler-plugin.version>

    </properties>

    <modules>
        <module>architecture</module>
    </modules>

    <dependencyManagement>
        <dependencies>

            <!-- kotlin -->
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-stdlib-jdk8</artifactId>
                <version>${kotlin.version}</version>
            </dependency>

            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-reflect</artifactId>
                <version>${kotlin.version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.module</groupId>
                <artifactId>jackson-module-kotlin</artifactId>
                <version>2.9.4.1</version>
            </dependency>

            <!--      quarkus      -->
            <dependency>
                <groupId>${quarkus.platform.group-id}</groupId>
                <artifactId>${quarkus.platform.artifact-id}</artifactId>
                <version>${quarkus.platform.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--  图片处理 -->
            <dependency>
                <groupId>net.coobird</groupId>
                <artifactId>thumbnailator</artifactId>
                <version>0.4.13</version>
            </dependency>

            <dependency>
                <groupId>org.ktorm</groupId>
                <artifactId>ktorm-core</artifactId>
                <version>${ktorm.version}</version>
            </dependency>
            <dependency>
                <groupId>org.ktorm</groupId>
                <artifactId>ktorm-support-postgresql</artifactId>
                <version>${ktorm.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>${quarkus.platform.group-id}</groupId>
                    <artifactId>quarkus-maven-plugin</artifactId>
                    <version>${quarkus.platform.version}</version>
                    <extensions>true</extensions>
                    <executions>
                        <execution>
                            <goals>
                                <goal>build</goal>
                                <goal>generate-code</goal>
                                <goal>generate-code-tests</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!--=默认情况下,Quarkus 不会发现另一个模块中的 CDI bean。-->
                <!-- 为多模块项目中的模块启用 CDI bean 发现的最佳方法是包含jandex-maven-plugin,除非它是已使用 quarkus-maven-plugin 配置的主应用程序模块,在这种情况下,它将自动索引。-->
                <!-- https://quarkus.io/guides/maven-tooling#multi-module-maven-->
                <plugin>
                    <groupId>org.jboss.jandex</groupId>
                    <artifactId>jandex-maven-plugin</artifactId>
                    <version>1.2.1</version>
                    <executions>
                        <execution>
                            <id>make-index</id>
                            <goals>
                                <goal>jandex</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!--  应用程序同时使用java和kotlin两种开发语言,则必须在 Java 编译器之前调用 Kotlin 编译器。  -->
                <!--  根据 maven 的方式,则需要在 maven-compiler-plugin 之前运行 kotlin-maven-plugin  -->
                <plugin>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <version>${kotlin.version}</version>
                    <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>

                    <!--      quarkus+kotlin:   https://quarkus.io/guides/kotlin           -->
                    <configuration>
                        <compilerPlugins>
                            <plugin>all-open</plugin>
                        </compilerPlugins>

                        <pluginOptions>
                            <!-- Each annotation is placed on its own line -->
                            <option>all-open:annotation=javax.ws.rs.Path</option>
                            <option>all-open:annotation=javax.enterprise.context.ApplicationScoped</option>
                        </pluginOptions>
                    </configuration>

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

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${compiler-plugin.version}</version>
                    <executions>
                        <!-- 替换 default-compile, 因为它会被 maven 特别处理 -->
                        <execution>
                            <id>default-compile</id>
                            <phase>none</phase>
                        </execution>
                        <!-- 替换 default-testCompile, 因为它会被 maven 特别处理 -->
                        <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>

                <!--build-helper-maven-plugin能够指定多个源码目录,通常它被绑定到默认生命周期的generate-sources阶段以添加额外的源码目录-->
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>3.0.0</version>
                    <executions>
                        <execution>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>add-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>src/main/kotlin</source>
                                    <source>src/main/java</source>
                                </sources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.0.0</version>
                    <!-- 绑定source插件到Maven的生命周期,并在生命周期后执行绑定的source的goal -->
                    <executions>
                        <execution>
                            <!-- 绑定source插件到Maven的生命周期 -->
                            <phase>compile</phase>
                            <!--在生命周期后执行绑定的source插件的goals -->
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12.4</version>
                    <configuration>
                        <skipTests>${maven.test.skip}</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public/</url>
            <layout>default</layout>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

 

 

子pom

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <modelVersion>4.0.0</modelVersion>
    <groupId>xyz.xxx</groupId>
    <artifactId>architecture</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <artifactId>sk-system</artifactId>
        <groupId>net.soramoon</groupId>
        <version>v1.0.0</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-config-yaml</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy-jackson</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-redis-client</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-jdbc-postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-security</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-arc</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>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
        </dependency>

        <!--test-->
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>${quarkus.platform.group-id}</groupId>
                <artifactId>quarkus-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jboss.jandex</groupId>
                <artifactId>jandex-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

 

最后,我不建议用Quarkus的时候要强行打成二进制。

太麻烦了,还不能用反射。 据我所知极个别企业项目有在生产环境用上Quarkus的,也基本都是非native, 咱们就算玩新技术也最好别去纠结这玩意…… 坑多。

 

           


EA PLAYER &

历史记录 [ 注意:部分数据仅限于当前浏览器 ]清空

      00:00/00:00