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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  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.

Ravi Hasija user avatar by
Ravi Hasija
·
Nov. 02, 18 · Tutorial
Like (2)
Save
Tweet
Share
8.53K 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 Hasija, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Mind Map Reuse in Software Groups
  • Utilize OpenAI API to Extract Information From PDF Files
  • Ultimate Guide to FaceIO
  • PHP vs React

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: