zoukankan      html  css  js  c++  java
  • 【转】Spring boot 打成jar包问题总结

    http://www.cnblogs.com/xingzc/p/5972488.html

    1、Unable to find a single main class from the following candidates

    1.1、问题描述

    maven build时出现以下错误提示日志:

    [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage (default) on project information: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.hhly.InformationApplication, com.hhly.test.Application] -> [Help 1]

    1.2、日志分析

    Unable to find a single main class from the following candidates [com.hhly.InformationApplication, com.hhly.test.Application]
    // 不能从下面的候选类中找到单一的main类

    1.3、解决办法

    查看着两个类,发现两个类中确实两个类中均有一个main方法,去掉一个多余的main方法,保留唯一的main方法。

    2、jar中没有主清单属性

    2.1、问题描述

    生产对应的jar包之后,通过一下命令运行spring boot程序,

    java -jar information-0.0.1-SNAPSHOT.jar
    • 1
    • 1

    2.2、问题分析

    查找资料发现为最后生成的jar包中的META-INF/MANIFEST.MF文件,没有设置主函数信息。猜想是pom.xml设置的问题,比对网上的设置,发现多数配置都是如下:

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <maimClass>com.hhly.InformationApplication</maimClass>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
    
                </plugin>
            </plugins>
        </build>

    比对自己的设置发现:自己在标签外面还包了一个 pluginManagement标签。

    2.3、解决办法

    去掉pluginManagement标签。

    3、Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required

    3.1、问题描述

    首先通过maven clean,然后再执行maven build,在执行main函数时会出现下面错误,详细日志如下:

    2016-09-09 18:29:43.419  WARN 37076 --- [ost-startStop-1] o.s.b.f.s.DefaultListableBeanFactory : Bean creation exception on non-lazy FactoryBean type 
    check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in file [D:
    eon-workspaceinformation
    	argetclassescomhhlydaoUserMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 
    'sqlSessionFactory' or 'sqlSessionTemplate' are required

    3.2、问题分析

    同样的代码,在通过Alt + F5更新项目,然后maven build生成jar包,最后执行的main的时候也就不会报错。

    3.3、解决办法

    调整打包顺序如下: 
    1、Alt + F5 
    2、maven build

    spring-boot-maven-plugin 插件的作用

       POM 文件中添加了“org.springframework.boot:spring-boot-maven-plugin”插件。在添加了该插件之后,当运行“mvn package”进行打包时,会打包成一个可以直接运行的 JAR 文件,使用“Java -jar”命令就可以直接运行。这在很大程度上简化了应用的部署,只需要安装了 JRE 就可以运行。

    可以在POM中,指定生成 的是Jar还是War。

    <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">
    <!-- ... -->
    <packaging>jar</packaging>
    <!-- ... -->
    </project>

    你还可以指定要执行的类,如果不指定的话,Spring会找有这个【public static void main(String[] args)】方法的类,当做可执行的类。

    如果你想指定的话,可以用下面两个方法:

    1,如果你的POM是继承spring-boot-starter-parent的话,只需要下面的指定就行。

    <properties>
        <!-- The main class to start by executing java -jar -->
        <start-class>com.mycorp.starter.HelloWorldApplication</start-class>
    </properties>
    2,如果你的POM不是继承spring-boot-starter-parent的话,需要下面的指定。
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>1.3.5.RELEASE</version>
          <configuration>
            <mainClass>${start-class}</mainClass>
            <layout>ZIP</layout>
          </configuration>
          <executions>
            <execution>
              <goals>
                <goal>repackage</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

    from:

    http://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html
    http://stackoverflow.com/questions/23217002/how-do-i-tell-spring-boot-which-main-class-to-use-for-the-executable-jar
    http://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html
    http://udn.yyuap.com/doc/Spring-Boot-Reference-Guide/III.%20Using%20Spring%20Boot/13.1.4.%20Using%20the%20Spring%20Boot%20Maven%20plugin.html
    http://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/#listing1

  • 相关阅读:
    跳出iframe
    leetcode 225. Implement Stack using Queues
    leetcode 206. Reverse Linked List
    leetcode 205. Isomorphic Strings
    leetcode 203. Remove Linked List Elements
    leetcode 198. House Robber
    leetcode 190. Reverse Bits
    leetcode leetcode 783. Minimum Distance Between BST Nodes
    leetcode 202. Happy Number
    leetcode 389. Find the Difference
  • 原文地址:https://www.cnblogs.com/exmyth/p/7118120.html
Copyright © 2011-2022 走看看