SQL (DDL) 脚本的建议位置是什么?
What's the recommended location for SQL, DDL, ... scripts in the Maven standard directory structure?
I bet almost every web project uses a DB and some kind of SQL scripts that need to be stored somewhere, so what would probably be the "best" place where to keep these files?
Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为这没有最佳实践。在我过去的项目中,我创建了一个单独的目录来存储此类 SQL 脚本。
例如
src/main/db
。默认情况下,它不会打包到最终的 JAR 中(这是大多数情况下的首选方式),但将其打包在程序集中很方便。您甚至可以通过添加相应的资源声明或使用 maven build-helper 插件将它们打包到主工件 JAR 中。
但是,一切都取决于您对此脚本的使用情况。我的简单“经验法则”是,只有当它们确实是要由应用程序加载的资源时,我才会考虑将它们放入
resources/
中。I think there is no best practice for this. In my past project, I created a separate directory for storing such SQL script.
For example
src/main/db
.It won't be packaged to final JAR by default (which are the preferred way in most case), yet it is convenient enough to let it packaged in assembly. You can even package them in the main artifact JAR, by adding corresponding resource declaration or using maven build-helper plugin.
However, all depends on your usage on this script. My simple "rule of thumb" is that, I would consider putting them in
resources/
only when they are really resources to be loaded by the application.我认为这完全取决于这些脚本的处理时间和方式:
src/main/
中的某个位置,例如src/main/sql
或src/main/db
。虽然我不会这样做,但我可以看到编译时的任务使用它们来修改数据库。我可以看到这里使用 liquibase 脚本,然后通过 Maven 任务执行。I think it depends entirely on when and how these scripts are processed:
src/main/
, likesrc/main/sql
orsrc/main/db
. While I wouldn't do this, I could see these being used by a task at compile to modify your DB. I could see liquibase scripts being used here and then executed via a maven task.src/main/resources
seems reasonable so that your runtime processes can consume them change your DB as you see fit - say as part of your hot-fix processing on deployment, or as part of your normal DB on-the-fly versioning efforts. Again, maybe you ship liquibase with your app, and then do in-place DB changes that way...src/scripts/sql
orsrc/scripts/db
. This places them as "source" files within the purview of maven, but in a place that is designed to used in a more ad-hoc manner.src/main/resources
是一个好地方,但请记住它被打包到您的最终 jar 中,因此这取决于您是否想在生产代码中显示它。如果没有,您可以通过将 maven-jar-plugin 配置摘录添加到适当的
pom.xml
来过滤掉它:src/main/resources
is a good place, but remember it gets packed into your final jar, so it depends if you want to reveal this in your production code or not.If not, you can filter out this by adding maven-jar-plugin configuration excerpt to appropriate
pom.xml
:我将使用 src/main/resources 来实现此目的。也许在那里创建一个子文件夹。
I would use
src/main/resources
for this purpose. Maybe creating a subfolder there.我将我的应用程序划分为多个 Eclipse 项目,每个架构层一个。这包括数据库。本质上,我专门为该项目创建了一种新的“数据库”打包类型。我创建了一个 src/main/sql 文件夹,在该文件夹下放置数据库模式(例如 src/main/sql/security),而不是 Java 包。我想如果您使用目录,那么首先是目录,然后是目录下面的模式。在每个模式文件夹中,我为每种类型的数据库对象(表、视图等)放置一个文件夹,以便最终得到 src/main/sql/security/tables,然后放置所有表定义那里有该模式的文件。我有兴趣阅读有关这样做的任何意见。
I divided my application into multiple Eclipse projects, one for each architectural layer. This includes the database. In essence, I created a new packaging type of "database" just for that project. I created a src/main/sql folder, and under that, instead of Java packages, I put database schemas (eg src/main/sql/security). I suppose if you use catalogs, it would be first the catalogs then under them the schemas. In each of the schema folders, I put a folder for each type of database object (table, view, etc) so that I ended up with, for example, src/main/sql/security/tables and then put all the table definition files for that schema in there. I'm interesting in reading any opinions on doing it this way.
这在很大程度上取决于您的情况,但首先最好将您的应用程序与底层数据库结构分开。因此,我建议您将所有与数据库相关的内容移动到单独的 Maven 项目中。完成此操作后,数据库脚本在 /src/main/scripts 中有一个很好的位置。
It very much depends on your mileage but first of all it's a good idea to separate your application from the underlying database structure. Therefore i'll recommend that you move all database related stuff to a separate maven project. Having done that, database scripts have a nice slot in /src/main/scripts.