简体   繁体   中英

create spring boot native image in a multi module project

i am trying to build an native image with graalvm and spring boot. my project has several modules.when i try to build native image i got this error:

Error: Please specify class (or <module>/<mainclass>) containing the main entry point method. (see --help)

and when i define mainClass path(org.example.api.Application) in properties in parent pom file i got this error: Error: Main entry point class 'org.example.api.Application' neither found on the classpath nor on the modulepath. how can i define the module that contain main class for graalvm?

In your parent pom (the one where you declare all your modules) using the syntax

<modules>
    <module>module1</module>
    <module>module2</module>
    <module>module3</module>
</modules>

use the latest Spring Boot BOM as parent

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.1</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

then override the native profile

<profiles>
    <profile>
        <id>native</id>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.graalvm.buildtools</groupId>
                        <artifactId>native-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>build-image</id>
                                <goals>
                                    <goal>compile-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>
</profiles>

At this point in your modules (where you need native builds) you can set this build configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.graalvm.buildtools</groupId>
            <artifactId>native-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

At this point you will be able to compile the project(s) using the mvn -Pnative clean package

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM