For SB 3.1.1 with the following dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

And having in the src/main/resources location the schema.sql, data.sql and application.properties files, where for this latest mentioned is declared as follows:

spring.datasource.url=jdbc:mysql://192.168.1.222:3307/....
spring.datasource.username=root
spring.datasource.password=secret

spring.profiles.active=cache,exception-non

If is executed the mvn clean compile and mvn spring-boot:run commands, the app works in peace.

Observation

The .sql files work as expected - they are totally valid about their own syntax.

Until here no reason to create this post.

Situation

If these scripts files are renamed as schema-mysql.sql and data-mysql.sql respectively and both moved to the com/manuel/jordan/mysql/v2 package/directory located in the src/main/resources location - and having now the application-mysql.properties file in the same src/main/resources location .

For a better understanding see the following structure:

src/main/resources
 com
  manuel
   jordan
    mysql
     v2
      data-mysql.sql
      schema-mysql.sql
application.properties
application-mysql.properties

Therefore the application-mysql.properties file being empty then is edited to:

spring.datasource.url=jdbc:mysql://192.168.1.222:3307/....
spring.datasource.username=root
spring.datasource.password=secret

spring.sql.init.mode=always
spring.sql.init.platform=mysql
spring.sql.init.schema-locations=classpath:/com/manuel/jordan/mysql/v2/
spring.sql.init.data-locations=classpath:/com/manuel/jordan/mysql/v2/

And the application.properties file is updated to:

spring.profiles.active=mysql,cache,exception-non

And executing the following commands:

  • mvn clean compile
  • mvn spring-boot:run

The app throws this error:

Unsatisfied dependency expressed through constructor parameter 0: 
 Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource 
 [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: 
  Failed to execute SQL script statement #1 of class path resource [com/manuel/jordan/mysql/v2/]: data-mysql.sql
...
Caused by: org.springframework.beans.factory.BeanCreationException: 
 Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource 
  [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: 
   Failed to execute SQL script statement #1 of class path resource [com/manuel/jordan/mysql/v2/]: data-mysql.sql
...
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: 
 Failed to execute SQL script statement #1 of class path resource [com/manuel/jordan/mysql/v2/]: data-mysql.sql
...
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; 
 check the manual that corresponds to your MySQL server version for the right syntax to use near 'data-mysql.sql' at line 1
````
Yes, I know the correct configuration is with (_now the scripts files names are included_): 

```properties
spring.sql.init.schema-locations=classpath:/com/manuel/jordan/mysql/v2/schema-mysql.sql
spring.sql.init.data-locations=classpath:/com/manuel/jordan/mysql/v2/data-mysql.sql

But the error stack trace is tricky due the following points (here the problems and reasons of this post):

  • The error is about the data-mysql.sql file and not about the schema-mysql.sql .

Is expected the latter be executed first than the former right? Therefore can be assumed by error that the schema-mysql.sql file was used and not the data-mysql.sql file. * The most important, the error indicates an error of SQL Syntax and it is not correct. Remember the files were only renamed and moved to other location.

Therefore should be better indicate that does not exist the v2.sql scripts files ... it because was used the classpath:/com/manuel/jordan/mysql/v2 declaration and it ends with v2

Thanks for your understanding

Comment From: wilkinsona

Much of what you've described doesn't seem to be directly related to the problem. For example, the various profiles seem to be adding complexity unnecessarily. I've tried to reproduce the behaviour that you have described but I haven't been able to do so. It fails due to the schema scripts not being present:

Caused by: java.lang.IllegalStateException: No schema scripts found at location 'classpath:/com/manuel/jordan/mysql/v2/'
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.getScripts(AbstractScriptDatabaseInitializer.java:129) ~[main/:na]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.applyScripts(AbstractScriptDatabaseInitializer.java:106) ~[main/:na]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.applySchemaScripts(AbstractScriptDatabaseInitializer.java:98) ~[main/:na]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.initializeDatabase(AbstractScriptDatabaseInitializer.java:76) ~[main/:na]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.afterPropertiesSet(AbstractScriptDatabaseInitializer.java:66) ~[main/:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.10.jar:6.0.10]
    ... 97 common frames omitted

If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

Comment From: manueljordan

Thanks for the reply, let me send a .zip file this weekend. I did create the project from the scratch 3 times in different days. And the same error "format" appears. Thanks for your understanding.

Comment From: manueljordan

@wilkinsona let me close this issue until I create the app as was requested, it to re-open it again ... Can we be agreed?

Comment From: manueljordan

Hello @wilkinsona

Done, I created and uploaded here a minimal project to reproduce the current situation. Remember it happens for SB 3.1.1. It was expanded to H2 too

When the application.properties file has spring.profiles.active=mysql then the application-mysql.properties file is used as follows (update the IP and Password):

spring.datasource.url=jdbc:mysql://192.168.1.X:3306/ensamble_presentation_v2?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=<password>

spring.sql.init.mode=always
#spring.sql.init.platform=mysql

#
#Invalid
#
# Because the script files are not defined
spring.sql.init.schema-locations=classpath:/com/manuel/jordan/mysql/v2/
spring.sql.init.data-locations=classpath:/com/manuel/jordan/mysql/v2/

#
#Valid
#
# Because the script files are not defined
#spring.sql.init.schema-locations=classpath:/com/manuel/jordan/mysql/v2/schema-mysql.sql
#spring.sql.init.data-locations=classpath:/com/manuel/jordan/mysql/v2/data-mysql.sql

Therefore happens arises (complete error stacktrace):

Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cienciaJdbcRepository' defined in file [/home/manueljordan/whiteroom/spring/sts4/sts4-workspace-ensamble-presentation/spring-boot-36386/target/classes/com/manuel/jordan/repository/jdbc/CienciaJdbcRepository.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Failed to execute SQL script statement #1 of class path resource [com/manuel/jordan/mysql/v2/]: data-mysql.sql
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:245) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1189) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[spring-context-6.0.10.jar:6.0.10]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.10.jar:6.0.10]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.1.jar:3.1.1]
    at com.manuel.jordan.SpringBoot36386Application.main(SpringBoot36386Application.java:14) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Failed to execute SQL script statement #1 of class path resource [com/manuel/jordan/mysql/v2/]: data-mysql.sql
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:888) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-6.0.10.jar:6.0.10]
    ... 18 common frames omitted
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [com/manuel/jordan/mysql/v2/]: data-mysql.sql
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:282) ~[spring-jdbc-6.0.10.jar:6.0.10]
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:254) ~[spring-jdbc-6.0.10.jar:6.0.10]
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:54) ~[spring-jdbc-6.0.10.jar:6.0.10]
    at org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer.runScripts(DataSourceScriptDatabaseInitializer.java:87) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.runScripts(AbstractScriptDatabaseInitializer.java:146) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.applyScripts(AbstractScriptDatabaseInitializer.java:108) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.applySchemaScripts(AbstractScriptDatabaseInitializer.java:98) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.initializeDatabase(AbstractScriptDatabaseInitializer.java:76) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.afterPropertiesSet(AbstractScriptDatabaseInitializer.java:66) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.10.jar:6.0.10]
    ... 31 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'data-mysql.sql' at line 1
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:121) ~[mysql-connector-j-8.0.33.jar:8.0.33]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-j-8.0.33.jar:8.0.33]
    at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:763) ~[mysql-connector-j-8.0.33.jar:8.0.33]
    at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648) ~[mysql-connector-j-8.0.33.jar:8.0.33]
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:94) ~[HikariCP-5.0.1.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-5.0.1.jar:na]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:261) ~[spring-jdbc-6.0.10.jar:6.0.10]
    ... 41 common frames omitted

When the application.properties file has spring.profiles.active=h2 then the application-h2.properties file is used as follows:

spring.datasource.url=jdbc:h2:mem:ensamble_presentation_v2;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password

spring.sql.init.mode=embedded

#
#Invalid
#
# Because the script files are not defined
spring.sql.init.schema-locations=classpath:/com/manuel/jordan/h2/v2/
spring.sql.init.data-locations=classpath:/com/manuel/jordan/h2/v2/

#
#Valid
#
# Because the script files are defined
#spring.sql.init.schema-locations=classpath:/com/manuel/jordan/h2/v2/schema-h2.sql
#spring.sql.init.data-locations=classpath:/com/manuel/jordan/h2/v2/data-h2.sql

#spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=none

Therefore happens arises (similar error as MySQL) (complete error stacktrace):

Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cienciaJdbcRepository' defined in file [/home/manueljordan/whiteroom/spring/sts4/sts4-workspace-ensamble-presentation/spring-boot-36386/target/classes/com/manuel/jordan/repository/jdbc/CienciaJdbcRepository.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Failed to execute SQL script statement #1 of class path resource [com/manuel/jordan/h2/v2/]: data-h2.sql
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:245) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1189) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) ~[spring-context-6.0.10.jar:6.0.10]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.10.jar:6.0.10]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.1.jar:3.1.1]
    at com.manuel.jordan.SpringBoot36386Application.main(SpringBoot36386Application.java:14) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Failed to execute SQL script statement #1 of class path resource [com/manuel/jordan/h2/v2/]: data-h2.sql
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:888) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-6.0.10.jar:6.0.10]
    ... 18 common frames omitted
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [com/manuel/jordan/h2/v2/]: data-h2.sql
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:282) ~[spring-jdbc-6.0.10.jar:6.0.10]
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:254) ~[spring-jdbc-6.0.10.jar:6.0.10]
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:54) ~[spring-jdbc-6.0.10.jar:6.0.10]
    at org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer.runScripts(DataSourceScriptDatabaseInitializer.java:87) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.runScripts(AbstractScriptDatabaseInitializer.java:146) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.applyScripts(AbstractScriptDatabaseInitializer.java:108) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.applySchemaScripts(AbstractScriptDatabaseInitializer.java:98) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.initializeDatabase(AbstractScriptDatabaseInitializer.java:76) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.afterPropertiesSet(AbstractScriptDatabaseInitializer.java:66) ~[spring-boot-3.1.1.jar:3.1.1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816) ~[spring-beans-6.0.10.jar:6.0.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.10.jar:6.0.10]
    ... 31 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "[*]data-h2.sql"; expected "DELETE, DROP, DECLARE, DEALLOCATE"; SQL statement:
data-h2.sql [42001-214]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:502) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:477) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.message.DbException.getSyntaxError(DbException.java:261) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.command.Parser.getSyntaxError(Parser.java:900) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.command.Parser.parsePrepared(Parser.java:866) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.command.Parser.parse(Parser.java:689) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.command.Parser.parse(Parser.java:666) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.command.Parser.prepareCommand(Parser.java:569) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.engine.SessionLocal.prepareLocal(SessionLocal.java:631) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.engine.SessionLocal.prepareCommand(SessionLocal.java:554) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1116) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:237) ~[h2-2.1.214.jar:2.1.214]
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:223) ~[h2-2.1.214.jar:2.1.214]
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:94) ~[HikariCP-5.0.1.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-5.0.1.jar:na]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:261) ~[spring-jdbc-6.0.10.jar:6.0.10]
    ... 41 common frames omitted

The app is ready to fail for both profiles.

The environment for the spring-boot-36386.tar.gz project is:

Spring Boot 3.1.1
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:    22.04
Codename:   jammy
java -version
openjdk version "17.0.6" 2023-01-17
OpenJDK Runtime Environment Temurin-17.0.6+10 (build 17.0.6+10)
OpenJDK 64-Bit Server VM Temurin-17.0.6+10 (build 17.0.6+10, mixed mode, sharing)

javac -version
javac 17.0.6

Comment From: wilkinsona

Thanks for the sample. I can see what's happening now.

The problem is that /com/manuel/jordan/h2/v2/ exists as a directory on the classpath. Somewhat surprisingly, this allows it to be read. classLoader.getResourceAsStream("/com/manuel/jordan/h2/v2/) succeeds and returns an InputStream whose contents list the files in the directory. You can reproduce this behavior without involving Spring Boot at all:

String content = FileCopyUtils.copyToString(new InputStreamReader(
        new ClassPathResource("/com/manuel/jordan/h2/v2").getInputStream()));
System.out.println(content);

The above produces the following output:

data-h2.sql
schema-h2.sql

It looks like we can use isReadable() to prevent the directory listing from being used. Framework goes to great lengths to make that work with resources that are inside a jar and that are available directly on the file system.

Comment From: manueljordan

Thanks for the confirmation and valuable explanation. Just being curious. Does have sense add another secondary control to check if the property's value ends with .sql?

Comment From: wilkinsona

Does have sense add another secondary control to check if the property's value ends with .sql?

No, I don't think so. We have no way of knowing what file extensions people may have chosen to use. Restricting it to .sql may break people's apps and we don't really have a reason to justify adding such a restriction.

Comment From: manueljordan

No, I don't think so. We have no way of knowing what file extensions people may have chosen to use. Restricting it to .sql may break people's apps and we don't really have a reason to justify adding such a restriction.

But: Do the spring.sql.init.schema-locations and spring.sql.init.data-locations properties work using a .sql file right?

I don't remember an example through the Web using a different file extension than .sql - if is possible (a surprise to me) could be added in the current Reference Documentation?

Comment From: wilkinsona

There's nothing in the docs that says that .sql is a required suffix and I don't think we need to say anything. As with many similar things in Boot, the properties work with anything that can be resolved to a Resource.

Comment From: manueljordan

As with many similar things in Boot, the properties work with anything that can be resolved to a Resource

Pls, just to have complete the idea. what other file extensions apart of .sql for this properties could be used?

Comment From: wilkinsona

You can use anything you want.