在Faadin项目(Java Servlet Web应用程序)中将Flyway迁移放在哪里? [英] Where to put Flyway migrations in a Vaadin project (Java Servlet web app)?

查看:92
本文介绍了在Faadin项目(Java Servlet Web应用程序)中将Flyway迁移放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里将其 Flyway 迁移文件放在多模块Maven原型?

Where does one put their Flyway migration files in a Vaadin 7 project created with the multi-module Maven archetype?

我将通过Flyway(不是命令行)中的Java API激活迁移.

I will activate the migrations through the Java API in Flyway (not the command-line).

Vaadin的解决方案可以在运行在任何基于Java Servlet 的Web应用程序项目中工作网络容器,例如码头 ?

Might the solution for Vaadin work in any Java Servlet based web app project running in a web container such as Tomcat or Jetty?

推荐答案

更新

截至2018年6月, Vaadin 8名为vaadin-archetype-application-multimodule的原型不再提供Web PagesMETA-INFWEB-INF的现有文件夹,如以下 Previously 部分中所示. WEB-INFclasses文件夹是在构建时动态创建的.

Update

As of 2018-06, the Vaadin 8 archetype named vaadin-archetype-application-multimodule no longer provides an existing folders for Web Pages, META-INF, and WEB-INF as seen below in the Previously section. The WEB-INF and classes folders are created dynamically at build-time.

查看您的foobar-ui模块的文件夹.找到src> main> resources文件夹层次结构.添加嵌套文件夹db& migration遵循Flyway约定.

Look to your foobar-ui module’s folder. Locate the src > main > resources folder hierarchy. Add the nested folders db & migration following Flyway conventions.

如果我们分解生成的WAR文件,我们会看到在构建过程中创建了一个WEB-INF文件夹,其中包含一个嵌套的classes文件夹,在其中找到我们的db> migration文件夹,其中包含第一个添加的文件夹. SQL脚本文件.

If we explode the resulting WAR file, we see that during the build process a WEB-INF folder was created, with a nested classes folder, where we find our db > migration folder containing our first added SQL script file.

Vaadin Ltd.提供的多模块Maven原型的早期版本使用的布局与上面所看到的不同.

Prior versions of the multi-module Maven archetype provided by Vaadin Ltd. used a different arrangement than that seen above.

默认情况下,Flyway会在此确切的文件夹路径中找到您的.sql迁移文件:

Flyway by default will find your .sql migration files if located in this exact folder path:

/WEB-INF/classes/db/migration/

在项目的-ui模块中,找到WEB-INF文件夹. Java Servlet 规范将该特殊文件夹定义为存储类文件,.jar文件,以及其他应提供给Servlet的资源,但始终要与外界隔离,永远不能作为Web应用程序的公共部分使用.

In your project, in the -ui module, locate the WEB-INF folder. The Java Servlet spec defines this special folder as a place to store class files, .jar files, and other resources that should be made available to your servlets but always shield from outside, never available as a public part of the web app.

WEB-INF文件夹将包含classes& lib文件夹.您可能尚未在 IDE 的项目视图中看到这两个文件夹.这两个特殊文件夹是在编译和生成过程中创建的.第一个文件包含Web应用程序的单个.class文件,而lib则包含.jar文件.同样,所有这些都由Servlet规范定义.

That WEB-INF folder will contain classes & lib folder. You may not yet see these two folders in your IDE’s project view. These two special folders are created during the compiling and build process. The first houses individual .class files for your web app, while lib houses .jar files. Again, all this is defined by the Servlet spec.

技巧:(如果未显示)在IDE中手动创建classes文件夹.在编译/生成过程中创建时,您放置在内部的内容将自动与目的地为classes文件夹的其他内容合并.

The Trick: Manually create the classes folder in your IDE if not shown. What you place inside will be automatically merged with the other content destined for the classes folder when created during the compiling/build process.

默认情况下,Flyway会在类路径中查找完全命名为/db/migration/的层次结构.因此,请在添加到WEB-INFclasses文件夹中,使用这些名称创建文件夹.现在,当Flyway在运行时在 classpath 上查找时,Flyway将在/WEB-INF/classes中查找并找到/db/migration/.

Flyway by default looks on the classpath for a hierarchy named exactly /db/migration/. So create folders by these names inside that classes folder you added to WEB-INF. Now when Flyway looks on the classpath at runtime, Flyway will look in /WEB-INF/classes and find /db/migration/.

因此在您的IDE中:

  1. 在项目的WEB-INF中创建一个名为classes的文件夹.
  2. 创建一个名为db的嵌套文件夹.
  3. 创建一个名为migration的嵌套文件夹.
  4. 将.sql文件放在该migration文件夹中.
  1. Create a folder named classes in your project’s WEB-INF.
  2. Create a nested folder named db.
  3. Create a nested folder named migration.
  4. Place your .sql files within that migration folder.

运行Flyway迁移.这是在我的实现[ServletContextListener][3]的类的contextInitialized方法中使用 H2数据库的示例代码.

Run your Flyway migration. Here is example code using the H2 Database called in the contextInitialized method of my class implementing [ServletContextListener][3].

Flyway flyway = new Flyway ();
flyway.setDataSource ( "jdbc:h2:~/test" , "scott" , "tiger" );
flyway.migrate ();

注意

我是Flyway的新手,请带一点盐来回答这个问题.但这对我来说使用 Flyway 4, NetBeans 8.1, Apache Tomcat 8.0.27,Mac OS X El Capitan.

Caveat

Take this Answer with a grain of salt as I am new to Flyway. But this is working for me using Flyway 4, Vaadin 7.6.4, Java 1.8 Update 77, NetBeans 8.1, Apache Tomcat 8.0.27, Mac OS X El Capitan.

这篇关于在Faadin项目(Java Servlet Web应用程序)中将Flyway迁移放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆