idea将springboot打包成jar或者war

时间:2022-11-11 09:37:13

1、首先在pom.xml中添加下面配置

    <groupId>com.melo</groupId>
    <artifactId>focus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!--打包成jar包,如果要打包成war包,修改成war-->
    <packaging>jar</packaging>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    <!--打包的文件名,在这里配置-->
        <finalName>focusDemo</finalName>
</build>

2、打包操作

idea将springboot打包成jar或者war

 

这样就打包成功了,打包的路径在D:\springboot-shiro-demo1\focus-master\focus\target\focusDemo.jar

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ focus ---
[INFO] Building jar: D:\springboot-shiro-demo1\focus-master\focus\target\focusDemo.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) @ focus ---
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ focus ---
[INFO] Installing D:\springboot-shiro-demo1\focus-master\focus\target\focusDemo.jar to C:\Users\yangwj\.m2\repository\com\melo\focus\0.0.1-SNAPSHOT\focus-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\springboot-shiro-demo1\focus-master\focus\pom.xml to C:\Users\yangwj\.m2\repository\com\melo\focus\0.0.1-SNAPSHOT\focus-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.548 s
[INFO] Finished at: 2018-12-11T11:04:18+08:00
[INFO] Final Memory: 27M/306M
[INFO] ------------------------------------------------------------------------

3、测试运行,点击idea的terminal,进入包目录D:\springboot-shiro-demo1\focus-master\focus\target,运行下面命令

java -jar focus.jar

这样就可以在本机访问了。

 

注意:如果部署在服务器上,jdk版本一定要一直,不然会报错哦,例如:

ubuntu@VM-0-14-ubuntu:~$ java -jar focus.jar 
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/melo/focus/FocusApplication : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

该错误就是jdk是1.7的版本,而开发机是jdk1.8版本,所以需要在服务器上安装jdk1.8才有效。

 

---------------------------------------------------------------------------------------------------------------------------------

补充一个:使用外部配置文件启动springboot项目

java -jar focus.jar --Dspring.config.location=application.yml  #application.xml为文件名