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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Testcontainers With Kotlin and Spring Data R2DBC
  • Selenium Grid Tutorial: Essential Tips and How to Set It Up
  • Automating Cucumber Data Table to Java Object Mapping in Your Cucumber Tests
  • Why You Should Migrate Microservices From Java to Kotlin: Experience and Insights

Trending

  • Article Moderation: Your Questions, Answered
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • How to Perform Custom Error Handling With ANTLR
  • How to Ensure Cross-Time Zone Data Integrity and Consistency in Global Data Pipelines
  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.2K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Testcontainers With Kotlin and Spring Data R2DBC
  • Selenium Grid Tutorial: Essential Tips and How to Set It Up
  • Automating Cucumber Data Table to Java Object Mapping in Your Cucumber Tests
  • Why You Should Migrate Microservices From Java to Kotlin: Experience and Insights

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!