在Glassfish 4上部署“ myapp.war”之前,我必须
jdbc-connection-pool
jdbc-resource
property javax.persistence.schema-generation.create-database-schemas
现在我在做:
asadmin add-resources ...
asadmin create-auth-realm ...
asadmin deploy ...
asadmin disable myapp ...
nano /.../glassfish/applications/myapp/WEB-INF/classes/META-INF/persistence.xml
ctrl+o
enter
ctrl+x
asadmin enable myapp ...
rm -Rf /tmp/install
在没有其他建议的情况下,我打算:
chmod +x deploy.sh
./deploy.sh
该脚本将处理所有事情。但我 只想 通过glassfish http控制台上传战争文件,并获得相同的结果。
有没有一种方法可以 在之前 调用类或脚本contextInitialized?
contextInitialized
您将如何部署这个东西?
为了完整性,这里有一些其他信息:
/myapp/WEB-INF/classes/META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="myapp" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <jta-data-source>jdbc/myapp</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <shared-cache-mode>NONE</shared-cache-mode> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myapp"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="password"/> <property name="javax.persistence.schema-generation.database.action" value="create"/> <property name="javax.persistence.schema-generation.create-database-schemas" value="false"/> <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/> <property name="javax.persistence.schema-generation.scripts.create-target" value="C:/tmp/myapp_create.ddl"/> <property name="javax.persistence.schema-generation.scripts.drop-target" value="C:/tmp/myapp_drop.ddl"/> <property name="eclipselink.deploy-on-startup" value="true"/> <property name="eclipselink.target-database" value="MySQL"/> <!-- <property name="eclipselink.ddl-generation" value="create-tables"/> --> <!-- <property name="eclipselink.ddl-generation.output-mode" value="database"/> --> <!-- <property name="eclipselink.create-ddl-jdbc-file-name" value="myapp.ddl"/> --> <!-- <property name="eclipselink.logging.level" value="FINE" /> --> <!-- <property name="eclipselink.logging.level.sql" value="FINE"/> --> <!-- <property name="eclipselink.logging.parameters" value="true"/> --> <!-- <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/> --> </properties> </persistence-unit> </persistence>
/myapp/WEB-INF/glassfish-resources.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"> <resources> <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="jdbc/myapp_pool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false"> <property name="serverName" value="localhost"/> <property name="portNumber" value="3306"/> <property name="databaseName" value="myapp"/> <property name="User" value="root"/> <property name="Password" value="password"/> <property name="URL" value="jdbc:mysql://localhost:3306/myapp"/> <property name="driverClass" value="com.mysql.jdbc.Driver"/> </jdbc-connection-pool> <jdbc-resource enabled="true" jndi-name="jdbc/myapp" object-type="user" pool-name="jdbc/myapp_pool"/> </resources>
虽然glassfish可以理解/ myapp /…/ persistence.xml(有时也执行CREATE SCHEMA myapp,有时不是,显然是随机的,但是很好),
CREATE SCHEMA myapp
我绝对无法使glassfish读取/myapp/WEB-INF/glassfish-resources.xml。它会忽略此文件。
UPDATE glassfish读取文件,但在jndi名称前加上java:app/其他引用前缀。意识到这一点,我用前缀重写了引用,现在它可以正常工作。最后,我注意到,如果glassfish- resources.xml位于META-INF(而不是WEB-INF)glassfish中,它将读取该文件,并且该文件也位于http ui中的“应用程序> myapp>描述符”下
java:app/
glassfish- resources.xml
META-INF
WEB-INF
终于我找到了解决方案:
数据库创建/升级 :在ServletContextListener.contextInitialized我中,使用编译时生成的ddl脚本创建数据库(如果不存在),或者使用liquibase升级数据库(如果存在)。不再需要persistence.xml来生成数据库。
ServletContextListener.contextInitialized
身份验证领域部署 :我不部署或创建任何特定于容器的领域。在ServletContextListener.contextInitialized我注册了一个自定义 JASPIC 实现,它本身就是一个JAAS登录模块包装器。感谢@ArjanTijms的这篇文章和这个答案