IDEA最全的正确打包姿势—以及常常出现的运行错误

文章适合新手了解IDEA怎么正确打包。文章在编写过程中难免有疏漏和错误,欢迎大佬指出文章的不足之处;更多内容请点进👉 Lino_White 👈查看。
未来的世界充满着各式各样的数据,我们该怎么好好利用起来呢?开启正文吧~~~

IDEA分三种打包方式,接下来我们将逐一使用截图来介绍,哪一种都可以使用哟!

第一种:集所有依赖包在项目中打包成jar,一个jar

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
点击Apply,再点击OK
在这里插入图片描述
选择刚才生成的打包名,build——
在这里插入图片描述
生成后的项目会在你项目的/out/artifacts/打包名/项目名.jar

第二种:打包成所有依赖包+项目包,多个jar

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
点击Apply,再点击OK
在这里插入图片描述
选择刚才生成的打包名,build——
在这里插入图片描述
生成后的项目会在你项目的/out/artifacts/打包名/项目名.jar

第三种(小编推荐):maven打成一个jar包,一个jar

在这里插入图片描述
先点击clean,会先将你之前编译的class全部清除,移除缓存;再点击package打包
在这里插入图片描述
运行成功后在项目的target文件夹下就会出现一个jar包,这个拿到java上运行即可。
在这里插入图片描述

不要着急,收藏一波,看看运行常常出现的bug!!!

1、Failed to configure a DataSource: ‘url’ attribute is …

Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method ‘dataSource‘ threw except...

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
 
Reason: Failed to determine a suitable driver class
 
 
Action:
 
Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

  • 首先,在你的配置文件当中,先确定自己的项目有没有使用到HikariDataSource,如果没有移除即可;
  • 如果有,那么就看看你的配置文件中spring:datasource有没有配置少或者错误;
  • 都没问题,那么就看看pom.xml文件是不是配置正确了。
  • pom.xml文件必须扫包的时候配置有扫描项目下的**/* .yml、** /* .xml、**/* .properties的相关配置,如下:
 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.2.RELEASE</version>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>

        </resources>
    </build>

2、错误: 找不到或无法加载主类 com.xxx.cn.AppApplication
如果你选择的是第一种,那么你应该就会遇到这种错误,这时候只需要将jar打开,将

在这里插入图片描述
3、Exception in thread “main” java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext

Exception in thread "main" java.lang.ClassCastException: 
org.apache.logging.slf4j.SLF4JLoggerContext cannot be
 cast to org.apache.logging.log4j.core.LoggerContext

Exception in thread "main" java.lang.IllegalArgumentException: 
LoggerFactory is not a Logback LoggerContext but Logback is on the 
classpath. Either remove Logback or the competing implementation 
(class org.slf4j.impl.Log4jLoggerFactory loaded from 
file:/D:/cyy/software/maven/repository/org/slf4j/slf4j-
log4j12/1.7.25/slf4j-log4j12-1.7.25.jar). If you are using WebLogic 
you will need to add 'org.slf4j' to prefer-application-packages in 
WEB-INF/weblogic.xml: org.slf4j.impl.Log4jLoggerFactory

spring自带的slf4j包跟其他自带的包冲突所产生,去除冲突的包,在pom.xml文件中添加以下相关配置即可:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--去除打印日志-->
            <exclusions>
            <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

如果提示没有log4j包的话,再单独导包进去,如以下配置:

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值