如何使用 Maven 为 Micronaut 创建可执行 jar 文件?

How to create executable jar file for Micronaut using maven?

我正在尝试使用 maven.

Micronaut 框架构建并 运行 一个简单的可执行 jar 文件

我的工作:

  1. 运行 mvn clean package
  2. 尝试运行申请java -jar target/simple_server-0.1.jar
  3. 获取下一个异常:
Error: Could not find or load main class com.github.volodya_lombrozo.Application
Caused by: java.lang.ClassNotFoundException: com.github.volodya_lombrozo.Application

这里的主要问题是如何正确使用 maven 构建和 运行 Micronaut 应用程序?也许我错过了什么?

这是我的 pom.xml 文件

<?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>

主要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);
    }
}

Application (main class) 添加到您的 Maven 插件配置中。 像这样:

<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>