Spring Boot Maven Plugin package exceptions and three workarounds: unable to find main class

Source: Internet
Author: User

The "Background" Spring-boot project, packaged as an executable jar, has two classes with the Main method in the project and all use @SpringBootApplication Note (or another scenario: you have two main methods and your class does not use @springbootapplication annotations), pom.xml as follows

<plugin>    <groupId>Org.springframework.boot</groupId>    <Artifactid>Spring-boot-maven-plugin</Artifactid>    <version>1.5.3.RELEASE</version>    <executions>        <Execution>            <Goals>                <goal>Repackage</goal>            </Goals>        </Execution>    </executions></plugin>

Problem

    • Execute the MVN clean package with the following error (say something unrelated, use install the same.) Because the Spring-boot:repackage target (goal) is bound to the package build phase (phases), and the package phase is executed before the install phase, the stage before the build phase is specified. See more: Introduction to the Build Lifecycle)

  [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.3.release:repackage (default) on project Webapps-api-bid:execution default of Goal Org.springframework.boot:spring-boot-maven-plugin:1.5.3.release: Repackage failed:unable to find a single main class from the following candidates [Com.xx.api.main.ApiBidMain, Com.xx.web Apps.api.main.WebappsApiBidMain]

    • Execute MVN Clean Package spring-boot:repackage, error below, as above log detail

  [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.3.release:repackage (default) on project Webapps-api-bid:execution default of Goal Org.springframework.boot:spring-boot-maven-plugin:1.5.3.release: Repackage Failed:unable to find main class

Solution

Note: Refer to the website description , do not specify <mainClass> or inherit the Spring-boot-starter-parent and the <start-class> property is not configured, will automatically find the signature is public static void main(String[] args)method of ... So the plugin is crazy, two girls and who together ...

    • [Recommended] General solution :<configuration> configuration MainClass, specify the program entry.
<plugin>    <groupId>Org.springframework.boot</groupId>    <Artifactid>Spring-boot-maven-plugin</Artifactid>    <version>1.5.3.RELEASE</version>    <Configuration>        <MainClass>Com.xx.webapps.api.main.WebappsApiBidMain</MainClass>    </Configuration>    <executions>        <Execution>            <Goals>                <goal>Repackage</goal>            </Goals>        </Execution>    </executions></plugin>

Spring Boot Maven plugin provides several goals (goal), <executions> tab <goal>repackage </goal> corresponding spring-boot:repackage this goal.

  • repackage : Create a jar or war file that is auto-executable. It can replace the regular artifact or can is attached to the build lifecyle with a separate classifier . /li>
  • run : Run your Spring Boot application with several options to pass parameters to it.< /li>
  • start and stop : Integrate your Spring Boot application to T He integration-test phase So, the application starts before it.

The plugin rewrites your manifest, and in particular it manages the Main-class and start-class entries, So if the defaults don't work for you has to configure those there (not in the jar plugin). The main-class in the manifest are actually controlled by the layout property of the boot plugin

The plugin rewrites the manifest file (MANIFEST.MF, the manifest file inside the jar), which manages the main class (Main-class) and the Start class (Start-class) portal. The main-class in the manifest file is controlled by layout

The start-class here is the <mainClass>of our configuration, and Main-class is controlled by the layout property, don't be confused by the name (isn't it weird?). Look at the solution and see why it's so weird .... To get an intuitive feel for an image, use the following XML configuration for the packaged manifest file (MANIFEST. MF):

  

The layout property does not need to be configured by default, and the plugin automatically infers it. The main-class in the different layout property manifest file will also be different accordingly. For example, layout is not configured or configured as a jar corresponding to the Main-class is jarlauncher,layout configured for the war Main-class is Warlauncher.

    • [restricted conditions] workaround two : If your pom inherits from Spring-boot-starter-parent (Note this premise), you can also go directly to the < Properties> configuration <start-class> (in fact, here the Start-class directly corresponds to the start-class in the manifest file):
< Properties >    < Start-class >com.xx.webapps.api.main.WebappsApiBidMain</start-class>
</ Properties >

    • Solution Three: when packaging the time to comment out the other @springbootapplication ... Or you have two main methods and neither use @SpringBootApplication annotations, comment out a Main method ... This is the third solution. 233333

"Say it casually."

Talk about spring-boot:repackage this goal. Spring Boot Maven Plugin This plugin contains a series of targets (goal), we <EXECUTIONS> the <goal>repackage</GOAL> For the goal of Spring-boot:repackage, look at the official introduction:

  Spring-boot:repackage repackages your Jar/war to be executable.

Repackages existing JAR and WAR archives So, they can be executed from the command line using Java-jar. With Layout=none can also is used simply to package a JAR with nested dependencies (and no main class, so not exe cutable).

Simply put, the shipment is repackaged with an executable jar/war that can be executed at the command line using-jar. If the specified layout is none then there is no main class that just hits a normal jar (not executable), and generally does not do so.

In general, this goal will hit a new Jar/war and add the Jar/war that Maven defaults to. Original suffix, which you can see in the target directory:

  

Reference

1.https://docs.spring.io/spring-boot/docs/current-snapshot/reference/htmlsingle/#build-tool-plugins-maven-packaging

2.https://docs.spring.io/spring-boot/docs/2.0.0.build-snapshot/maven-plugin//repackage-mojo.html

3.https://stackoverflow.com/questions/23217002/how-do-i-tell-spring-boot-which-main-class-to-use-for-the-executable-jar

Spring Boot Maven Plugin package exceptions and three workarounds: unable to find main class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.