DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Testcontainers With Kotlin and Spring Data R2DBC
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Kotlin Code Style: Best Practices for Former Java Developers
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers

Trending

  • Building Enterprise-Grade Real-Time IoT Dashboards with Vue 3, MQTT, and Kafka
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • Catching Data Perimeter Drift Before It Reaches Production
  • Dear Micromanager: Your Distrust Has a Job; It’s Just Not the One You’re Doing
  1. DZone
  2. Coding
  3. Languages
  4. Micronaut With Kotlin and Java With Groovy Tests

Micronaut With Kotlin and Java With Groovy Tests

Learn how to set up a simple project in Micronaut with Kotlin/Java for the source code and Groovy/Spock tests.

By 
Ravi Isnab user avatar
Ravi Isnab
·
Updated Nov. 02, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.4K Views

Join the DZone community and get the full member experience.

Join For Free

Recently, I came across Micronaut, a super light framework that fills the need for something in between Spring Boot and Vert.x.

I have been playing with it and wanted to see if I could get a simple project with Micronaut, a Kotlin/Java intermix for the (main) source code, and Groovy/Spock tests. Turns out it's not that difficult. I just had to fiddle with it a little bit to get it right. So to save you time, here it is:

My requirements were:

  • Source code in Kotlin and Java with circular dependency (just for fun)
  • Test code exclusively in Groovy
  • Maven based
  • Micronaut as the base framework wiring everything together

mn create-app --features http-server --lang kotlin --build maven hello-world-example provided a great starting point. Then it was a matter of adding gmavenplus-plugin and maven-surefire-plugin.

Here's the source code. If you would like to know more about how the plugins work with each other, then read below. If not, feel free to dig around in the code.

  1. To make Kotlin, Java, and Micronaut work together, you need kotlin-maven-plugin and maven-compiler-plugin as shown below.
  2. Also, you need gmavenplus-plugin to compile groovy code.
  3. Finally, you need maven-surefire-plugin to execute test(s).

pom.xml (kotlin-maven-plugin section responsible for kotlin, java and micronaut mix)

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

    <configuration>
        <compilerPlugins>
            <plugin>all-open</plugin>
        </compilerPlugins>
        <pluginOptions>
            <option>all-open:annotation=io.micronaut.aop.Around</option>
        </pluginOptions>
    </configuration>

    <executions>
        <execution>                                                     (1)
            <id>kapt</id>
            <goals>
                <goal>kapt</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>src/main/kotlin</sourceDir>              (2)
                    <sourceDir>src/main/java</sourceDir>                (3)
                </sourceDirs>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>io.micronaut</groupId>
                        <artifactId>micronaut-inject-java</artifactId>
                        <version>${micronaut.version}</version>
                    </annotationProcessorPath>
                    <annotationProcessorPath>
                        <groupId>io.micronaut</groupId>
                        <artifactId>micronaut-validation</artifactId>
                        <version>${micronaut.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </execution>

        <execution>                                                     (4)
            <id>compile</id>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>src/main/kotlin</sourceDir>
                </sourceDirs>
            </configuration>
        </execution>

        <execution>                                                     (5)
            <id>test-kapt</id>
            <goals>
                <goal>test-kapt</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>src/test/kotlin</sourceDir>
                </sourceDirs>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>io.micronaut</groupId>
                        <artifactId>micronaut-inject-java</artifactId>
                        <version>${micronaut.version}</version>
                    </annotationProcessorPath>
                    <annotationProcessorPath>
                        <groupId>io.micronaut</groupId>
                        <artifactId>micronaut-validation</artifactId>
                        <version>${micronaut.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </execution>

        <execution>
            <id>test-compile</id>
            <goals>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>src/test/kotlin</sourceDir>
                    <sourceDir>target/generated-sources/kapt/test</sourceDir>
                </sourceDirs>
            </configuration>
        </execution>
    </executions>

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

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <proc>none</proc>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
    <executions>
        <execution>
            <id>default-compile</id>                                    (6)
            <phase>none</phase>
        </execution>
        <execution>
            <id>default-testCompile</id>                                (7)
            <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>


  1. The Kapt compiler plugin provides support for annotation processors. micronaut does it's work at compile time. Hence, the kapt plugin is necessary.
  2. Kotlin source location
  3. Java source location
  4. For compile goal provide kotlin source location
  5. test-kapt is only necessary when writing tests in kotlin.

pom.xml (gmavenplus-plugin section responsible for groovy test code compilation)

            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.6.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>addTestSources</goal>             (1)
                            <goal>compileTests</goal>               (2)
                        </goals>
                    </execution>
                </executions>
            </plugin>
  1. Add test sources
  2. Compile tests.

pom.xml (maven-surefire-plugin responsible for executing unit tests)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
    <configuration>
        <useFile>false</useFile>
        <includes>
            <include>**/*Spec.*</include>                       (1)
            <include>**/*Test.*</include>                       (2)
        </includes>
    </configuration>
</plugin>
  1. Execute any class that ends with Spec or
  2. Execute any class that ends with Test
Testing Kotlin (programming language) Java (programming language) Groovy (programming language)

Published at DZone with permission of Ravi Isnab. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Testcontainers With Kotlin and Spring Data R2DBC
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Kotlin Code Style: Best Practices for Former Java Developers
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook