如何部署webapp并创建其资源 [英] how to deploy a webapp and create its resources
问题描述
- 创建jdbc连接池(/ WEB-INF / glassfish- resources.xml - >
jdbc-connection-pool
本身不工作...可以从asadmin发送) - 创建jdbc资源池(/WEB-INF/glassfish-resources.xml - >
jdbc-resource
同上) - 创建auth-realm没有自动,现在使用asadmin)
- 创建模式(peristence.xml - >
属性javax.persistence.schema-generation.create-database-schemas
,但是是假的) - 创建表(persistence.xml - >create-tables,不完美,但至少起作用)
现在我在做:
- 上传myapp.war glassfish-resources.xml在/ tmp / install
-
asadmin add-resources ...
-
asadmin create-auth-realm ...
-
asadmin deploy ...
-
asadmin disable mya pp ...
-
nano /.../glassfish/applications/myapp/WEB-INF/classes/META-INF/ persistence.xml
- 注释几行,
ctrl + o
,enter
,ctrl + x
,enter
-
asadmin enable myapp ...
-
rm -Rf / tmp / install
没有其他建议我打算:
- 上传myapp.war,deploy.shon / tmp / install
-
chmod + x deploy.sh
-
./ deploy.sh
,脚本将照顾所有内容。
,但我想仅通过glassfish http控制台上传只有战争文件,并获得相同的结果。
是否有在 contextInitialized
之前调用的类或脚本的方法
您将如何部署这个东西?
为了完整,这里有一些额外的信息:
/myapp/WEB-INF/classes/META-INF/persistence.xml
;?xml version =1.0encoding =UTF-8?>
< persistence version =2.0xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-实例xsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd\">
< persistence-unit name =myapptransaction-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>
<属性>
< property name =javax.persistence.jdbc.drivervalue =com.mysql.jdbc.Driver/>
< property name =javax.persistence.jdbc.urlvalue =jdbc:mysql:// localhost:3306 / myapp/>
< property name =javax.persistence.jdbc.uservalue =root/>
< property name =javax.persistence.jdbc.passwordvalue =password/>
< property name =javax.persistence.schema-generation.database.actionvalue =create/>
< property name =javax.persistence.schema-generation.create-database-schemasvalue =false/>
< property name =javax.persistence.schema-generation.scripts.actionvalue =drop-and-create/>
< property name =javax.persistence.schema-generation.scripts.create-targetvalue =C:/tmp/myapp_create.ddl/>
< property name =javax.persistence.schema-generation.scripts.drop-targetvalue =C:/tmp/myapp_drop.ddl/>
< property name =eclipselink.deploy-on-startupvalue =true/>
< property name =eclipselink.target-databasevalue =MySQL/>
<! - < property name =eclipselink.ddl-generationvalue =create-tables/> - >
<! - < property name =eclipselink.ddl-generation.output-modevalue =database/> - >
<! - < property name =eclipselink.create-ddl-jdbc-file-namevalue =myapp.ddl/> - >
<! - < property name =eclipselink.logging.levelvalue =FINE/> - >
<! - < property name =eclipselink.logging.level.sqlvalue =FINE/> - >
<! - < property name =eclipselink.logging.parametersvalue =true/> - >
<! - < property name =eclipselink.logging.loggervalue =org.eclipse.persistence.logging.DefaultSessionLog/> - >
< / properties>
< / persistence-unit>
< / persistence>
/myapp/WEB-INF/glassfish-resources.xml
<?xml version =1.0encoding =UTF-8?>
< DOCTYPE资源PUBLIC - // GlassFish.org//DTD GlassFish应用服务器3.1资源定义// ENhttp://glassfish.org/dtds/glassfish-resources_1_5.dtd\">
< resources>
< jdbc-connection-pool allow-non-component-callers =falseassociate-with-thread =falseconnection-creation-retry-attempts =0connection-creation-retry-interval- in-seconds =10connection-leak-reclaim =falseconnection-leak-timeout-in-seconds =0connection-validation-method =auto-commitdatasource-classname =com.mysql.jdbc .jdbc2.optional.MysqlDataSourcefail-all-connections =falseidle-timeout-in-seconds =300is-connection-validation-required =falseis-isolation-level-guarantee =truelazy -connection-association =falselazy-connection-queistment =falsematch-connections =falsemax-connection-usage-count =0max-pool-size =32max-wait-time- in-millis =60000name =jdbc / myapp_poolnon-transactional-connections =falsepool-resize-quantity =2res-type =javax.sql.DataSourcestatement-timeout-in-seconds = - 1steady-pool-size =8validate-atmost-once-period-in-seconds =0wrap-jdbc-objects =false>
< property name =serverNamevalue =localhost/>
< property name =portNumbervalue =3306/>
< property name =databaseNamevalue =myapp/>
< property name =Uservalue =root/>
< property name =Passwordvalue =password/>
< property name =URLvalue =jdbc:mysql:// localhost:3306 / myapp/>
< property name =driverClassvalue =com.mysql.jdbc.Driver/>
< / jdbc-connection-pool>
< jdbc-resource enabled =truejndi-name =jdbc / myappobject-type =userpool-name =jdbc / myapp_pool/>
< / resources>
而glassfish了解/myapp/.../persistence.xml(有时也执行 CREATE SCHEMA myapp
,有时不是,显然是随机的 - 但是很好),
我绝对不能使glassfish读取/ myapp / WEB-INF / GlassFish的-resources.xml中。它忽略了这个文件。
更新
glassfish读取文件,但以j $ / 打破其他引用。意识到这个事情我用前缀重写引用,现在它正常工作。
最后,我注意到,如果 glassfish-resources.xml
在 META-INF
(而不是 WEB-INF
)glassfish读取文件,它也存在于http应用程序myapp>描述符中的
最后我找到一个解决方案:
-
数据库创建/升级:在
ServletContextListener.contextInitialized
我使用编译时生成的ddl脚本来创建数据库(如果不存在),或者使用liquibase升级数据库(如果存在)。没有更多的persistence.xml用于数据库生成。 -
认证领域部署:我不部署或创建任何容器特定领域。在
ServletContextListener.contextInitialized
中,我注册了一个自定义的 JASPIC 实现,它本身就是一个JAAS登录模块包装器。感谢@ArjanTijms的这个aricle 和这个答案
before deploy "myapp.war" on glassfish 4 i have to
- create jdbc connection pool (/WEB-INF/glassfish-resources.xml ->
jdbc-connection-pool
does not work by itself... fine from asadmin) - create jdbc resource for pool (/WEB-INF/glassfish-resources.xml ->
jdbc-resource
same as above) - create auth-realm (nothing auto, using asadmin for now)
- create schema (peristence.xml ->
property javax.persistence.schema-generation.create-database-schemas
, but is bogus) - create tables (persistence.xml -> `create-tables', not perfect but at least it works)
now i'm doing:
- upload "myapp.war", "glassfish-resources.xml" on /tmp/install
asadmin add-resources ...
asadmin create-auth-realm ...
asadmin deploy ...
asadmin disable myapp ...
nano /.../glassfish/applications/myapp/WEB-INF/classes/META-INF/persistence.xml
- comment a few lines,
ctrl+o
,enter
,ctrl+x
,enter
asadmin enable myapp ...
rm -Rf /tmp/install
and without other suggestion i'm planning to:
- upload "myapp.war", "deploy.sh" on /tmp/install
chmod +x deploy.sh
./deploy.sh
and the script will take care of everything. but i'd like to upload only a war file via glassfish http console and obtain the same result.
is there a way to have a class or script called before contextInitialized
?
how would you deploy this thing?
for completeness here are some additional info:
/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>
while glassfish understands /myapp/.../persistence.xml (sometimes executing also CREATE SCHEMA myapp
, sometimes not, apparently random - but it's fine),
i definitely can't make glassfish read /myapp/WEB-INF/glassfish-resources.xml. it ignores this file.
UPDATE
glassfish reads the file but prefixes jndi names with java:app/
breaking other references. being aware of this thing i rewrite references with prefix and now it is working fine.
lastly, i noted that if glassfish-resources.xml
is inside META-INF
(instead of WEB-INF
) glassfish reads the file and it is also present in http ui under "applications > myapp > descriptors"
finally i found a solution:
database create/upgrade: in
ServletContextListener.contextInitialized
i use compile-time-generated ddl script to create database if not exists, or use liquibase to upgrade database if exists. no more persistence.xml usage for database generation.authentication realm deploy: i don't deploy or create any container-specific realm. in
ServletContextListener.contextInitialized
i register a custom JASPIC implementation, which is itself a JAAS login module wrapper. thanks to @ArjanTijms for this aricle and this answer
这篇关于如何部署webapp并创建其资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!