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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Testcontainers With Kotlin and Spring Data R2DBC
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory

Trending

  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • Designing Frontend Applications for Failure, Not Just Success
  • The Permission Was Valid. The Action Was Not Authorized.
  • Moving 900+ GB of Prometheus Without a Cutover Weekend

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Updated Nov. 02, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Testcontainers With Kotlin and Spring Data R2DBC
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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