Spring boot 打成jar包问题总结

摘要:
如果要指定,可以使用以下两种方法:1.如果POM继承了spring-boot-starter父级,则只需指定以下内容。
Spring boot 打成jar包问题总结

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类

Spring boot 打成jar包问题总结第1张

1.3、解决办法

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

2、jar中没有主清单属性

2.1、问题描述

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

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

jar中没有主清单属性

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

免责声明:文章转载自《Spring boot 打成jar包问题总结》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS开发之Xcode常用调试技巧总结Zabbix邮件报警下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Webstorm快捷键

网店快捷键1.搜索/替换,包括全局搜索和文件搜索。...

Delete from join 用法

delete(别名)fromtblA(别名)leftjointblb(别名)on。。。...

vue+jspdf+html2canvas导出PDF文件

没有废话。首先,查看最终打印结果。我说最后打印的pdf文件看起来像这样。pdf文件的分页是通过设置jspdf实现的,但我暂时无法对文件内容进行分页。因为我们首先将需要打印的元素转换为画布,然后将画布转换为图像,然后将图像转换为pdf文件。...

ZFS文件系统及Freenas介绍

作为OpenSolaris开源计划的一部分,ZFS于2005年11月发布。它被Sun称为终极文件系统,已经积极开发了10年。ZFS的最大优点之一是,当将其他磁盘添加到池中时,现有文件系统可以自动增长。ZFS使用快照来跟踪文件系统中的更改。5.数据完整性验证和自动修复当新数据写入ZFS时,将创建数据的校验和,从而允许文件系统分叉到新数据集中。...

windows下如何查看和修改MySQL的端口号

更改为要设置的新端口号。4.在桌面上找到“我的电脑”,右键单击并选择“管理”5.找到“服务和应用程序”并双击6.继续双击“服务”7.在列表中找到“MySQL”,右击并选择“重新启动”8.等待重新启动完成,然后按照我们刚才用来查看MySQL端口号的方法查看更改是否成功。如图所示,3396是我更改的端口号。...

微信公众号平台开发(三):几大微信接口的调用

但是,有一些高级接口。您的微信公众号必须具有一定的权限,例如通过微信认证,才能调用自定义菜单、微信支付和其他高级功能。不过,微信公众号的测试号系统可以应用这些高级接口。菜单类型很多,但xml类型不同。详情可在微信公众号平台查看相应文件。...