Home > Java > javaTutorial > How to use maven package to specify mainClass in Springboot

How to use maven package to specify mainClass in Springboot

WBOY
Release: 2023-05-23 19:55:11
forward
1973 people have browsed it

Use maven packaging to specify mainClass

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.xxx.XxxApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
Copy after login

SpringBoot Maven packaging errors and reasons

1.org.springframework.boot:spring-boot-maven-plugin:2.2.1.RELEASE:repackage failed: Unable to find main class

2. Unable to find symbol

Unable to find main class Cause of the problem

If you use SpringBoot packaging plug-in as follows

<build>
    <plugins>
         <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
     </plugins>
</build>
Copy after login
  • If this packaging plug-in is used, then we must have a class annotated with @SpringBootApplication, otherwise repackage failed: Unable to find main class will be reported when packaging.

  • If our project packaging is just an ordinary toolkit, then there is no need to add any packaging plug-ins. Maven uses the default method to package it for us without configuration (you can learn about the maven default method configuration online Check, there are many online).

  • If our project is built in a multi-level build (multi-module) mode, it is just an ordinary module when packaging, but the repackage failed: Unable to find main class error is still reported. This Then we check whether the module's parent project has added the SpringBoot packaging plug-in, because the packaging plug-in will also be inherited. Therefore, it is recommended not to directly add the SpringBoot packaging plug-in to the parent project for convenience. Instead, the Module needs to be packaged as a SpringBoot project and then the SpringBoot packaging plug-in is added.

  • Regarding maven's default packaging method (as shown below), the package is packaged in jar mode, so there is no need to configure pom.xml, unless we just package it as pom, we can configure< ;packaging>pom, otherwise there is no need to configure it. Of course, the top level of multi-module must be the pom packaging method.

How to use maven package to specify mainClass in Springboot

A project has multiple main.class, which causes maven to not know which one to use as the main entrance when packaging. Here we need to set

<build>
 <plugins>
   <plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     <configuration>
      	<mainClass>com.demo.springboot.DemoSbApplication</mainClass>
     </configuration>
   </plugin>
 </plugins>
</build>
Copy after login

The reason why the symbol cannot be found

The reason is usually that when we package the project, we package the project into a jar package and reference other modules.

Other modules are not packaged using jar. For springboot, it is set <packaging>pom</packaging>. This kind of class must not be found, so we only need Just set the packaging method of that module to <packaging>jar</packaging>. Note: This may cause Unable to find main class problem.

The above is the detailed content of How to use maven package to specify mainClass in Springboot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template