You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven构建JAR添加models文件夹时遇DataSource配置错误求助

问题解决:Spring Boot打包JAR后资源缺失与数据源配置错误

问题背景

项目依赖两个本地JAR包(jdf.jar、VnCoreNLP-1.2.jar),运行时要求根目录存在models资源文件夹:

  • IntelliJ IDEA中直接运行正常
  • 打包成JAR后用java -jar执行时,依赖库无法工作,检查发现JAR内未包含models文件夹
  • 添加资源打包配置后,触发以下数据源错误:

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

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class

项目结构:

📦 project
├─ models
├─ src
├─ jdf.jar
└─ VnCoreNLP-1.2.jar

原依赖配置:

<dependency>
   <groupId>jdf</groupId>
   <artifactId>jdf</artifactId>
   <version>1.0.0</version>
   <scope>system</scope>
   <systemPath>${project.basedir}/jdf.jar</systemPath>
</dependency>

<dependency>
   <groupId>vncorenlp</groupId>
   <artifactId>vncorenlp</artifactId>
   <version>1.2.0</version>
   <scope>system</scope>
   <systemPath>${project.basedir}/VnCoreNLP-1.2.jar</systemPath>
</dependency>

原打包配置尝试:

<build>
   <resources>
      <resource>
          <directory>models</directory>
      </resource>
   </resources>
</build>

解决方案步骤

1. 修复数据源错误

报错原因是Spring Boot自动启用了数据源自动配置,但项目未配置数据库连接信息。根据项目需求二选一:

  • 无需数据库:禁用自动配置
    在启动类上添加注解,跳过数据源相关自动配置:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
  • 需要数据库:补充配置
    在src/main/resources/application.properties中添加数据库连接信息:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

2. 确保models文件夹被打包进JAR

原配置覆盖了Maven默认资源目录,导致src/main/resources下的配置文件也无法打包。修改配置同时保留默认资源目录和models:

<build>
    <resources>
        <!-- 保留默认资源目录 -->
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <!-- 添加models目录作为资源,打包后位于JAR根目录 -->
        <resource>
            <directory>models</directory>
        </resource>
    </resources>
</build>

3. 处理system scope依赖的打包问题

system scope的依赖默认不会被打包到最终JAR中,需使用maven-shade-plugin将本地JAR合并到成品JAR:

<build>
    <!-- 上述resources配置 -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <!-- 指定Spring Boot启动类 -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>你的启动类全路径(如com.example.YourApplication)</mainClass>
                            </transformer>
                        </transformers>
                        <!-- 包含本地system依赖 -->
                        <includeDependencies>
                            <includeDependency>
                                <groupId>jdf</groupId>
                                <artifactId>jdf</artifactId>
                            </includeDependency>
                            <includeDependency>
                                <groupId>vncorenlp</groupId>
                                <artifactId>vncorenlp</artifactId>
                            </includeDependency>
                        </includeDependencies>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

4. 验证打包结果

执行mvn clean package后,打开生成的JAR包检查:

  • models文件夹是否存在于JAR根目录
  • 本地JAR的类是否被合并到成品JAR中
  • application.properties等配置文件是否存在

内容的提问来源于stack exchange,提问作者Văn Thuận Nguyễn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 18:05:15