Home > Mobile >  How to create executable jar file for Micronaut using maven?
How to create executable jar file for Micronaut using maven?

Time:10-12

I'm trying to build and run a simple executable jar file for the Micronaut framework using maven.

What I do:

  1. Run mvn clean package
  2. Try to run the application java -jar target/simple_server-0.1.jar
  3. Get the next exception:
Error: Could not find or load main class com.github.volodya_lombrozo.Application
Caused by: java.lang.ClassNotFoundException: com.github.volodya_lombrozo.Application

The main question here is how to build and run the Micronaut application using maven properly? Maybe I missed somehting?

Here is my pom.xml file

<?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>
    <groupId>com.github.volodya_lombrozo</groupId>
    <artifactId>simple_server</artifactId>
    <version>0.1</version>
    <packaging>${packaging}</packaging>
    <parent>
        <groupId>io.micronaut</groupId>
        <artifactId>micronaut-parent</artifactId>
        <version>3.1.0</version>
    </parent>
    <properties>
        <packaging>jar</packaging>
        <jdk.version>11</jdk.version>
        <release.version>11</release.version>
        <micronaut.version>3.1.0</micronaut.version>
        <exec.mainClass>com.github.volodya_lombrozo.Application</exec.mainClass>
        <micronaut.runtime>netty</micronaut.runtime>
    </properties>
    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-inject</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-validation</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-http-client</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-http-server-netty</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-runtime</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>io.micronaut.build</groupId>
                <artifactId>micronaut-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths combine.children="append">
                        <path>
                            <groupId>io.micronaut</groupId>
                            <artifactId>micronaut-http-validation</artifactId>
                            <version>${micronaut.version}</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <arg>
                            -Amicronaut.processing.group=com.github.volodya_lombrozo
                        </arg>
                        <arg>-Amicronaut.processing.module=simple_server</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

And the main class:

package com.github.volodya_lombrozo.simple_server;

import io.micronaut.runtime.Micronaut;

public class Application {

    public static void main(String[] args) {
        Micronaut.run(Application.class, args);
    }
}

CodePudding user response:

Add Application (main class) to your maven plugin config. Like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <annotationProcessorPaths combine.children="append">
            <path>
                <groupId>io.micronaut</groupId>
                <artifactId>micronaut-http-validation</artifactId>
                <version>${micronaut.version}</version>
            </path>
        </annotationProcessorPaths>
        <compilerArgs>
            <arg>
                -Amicronaut.processing.group=com.github.volodya_lombrozo
            </arg>
            <arg>-Amicronaut.processing.module=simple_server</arg>
        </compilerArgs>
        
        <mainClass>com.github.volodya_lombrozo.simple_server.Application</mainClass> <!-- HERE ----->

    </configuration>
</plugin>
  • Related