java – 如何部署webapp并创建其资源

之前在glassfish 4上部署“myapp.war”之前我必须这样做

>创建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’,不完美,但至少它可以工作)

现在我在做:

>在/ tmp / install上传“myapp.war”,“glassfish-resources.xml”
> asadmin add-resources …
> asadmin create-auth-realm …
> asadmin部署…
> asadmin禁用myapp …
> nano /…/glassfish/applications/myapp/WEB-INF/classes/Meta-INF/persistence.xml
>注释几行,ctrl o,输入,ctrl x,输入
> asadmin启用myapp …
> rm -Rf / tmp / install

没有其他建议我打算:

>在/ tmp / install上传“myapp.war”,“deploy.sh”
> chmod x deploy.sh
> ./deploy.sh

并且脚本将处理所有事情.
但是我想通过glassfish http console上传一个war文件并获得相同的结果.

有没有办法在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,有时候不是,显然是随机的 – 但它很好),

我绝对不能把glassfish读成/myapp/WEB-INF/glassfish-resources.xml.它忽略了这个文件.

UPDATE
glassfish读取文件,但使用java:app /破坏其他引用的jndi名称前缀.知道这件事我用前缀重写引用,现在它工作正常.
最后,我注意到如果glassfish-resources.xml在Meta-INF(而不是WEB-INF)中,则glassfish会读取该文件,并且它也存在于http:“applications> myapp> descriptors”下的http ui中

解决方法

最后我找到了解决方案:

>数据库创建/升级:在ServletContextListener.contextInitialized中,我使用编译时生成的ddl脚本创建数据库(如果不存在),或使用liquibase升级数据库(如果存在).不再有用于数据库生成的persistence.xml用法.
>身份验证领域部署:我不部署或创建任何特定于容器的领域.在ServletContextListener.contextInitialized中,我注册了一个自定义JASPIC实现,它本身就是一个JAAS登录模块包装器.感谢@ArjanTijms为this ariclethis answer

相关文章

ArrayList简介:ArrayList 的底层是数组队列,相当于动态数组。与 Java 中的数组相比,它的容量能动态增...
一、进程与线程 进程:是代码在数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位。 线程...
本文为博客园作者所写:&#160;一寸HUI,个人博客地址:https://www.cnblogs.com/zsql/ 简单的一个类...
#############java面向对象详解#############1、面向对象基本概念2、类与对象3、类和对象的定义格式4、...
一、什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错。在java中,阻止当前方法或作用域...
Collection接口 Collection接口 Collection接口 Collection是最基本的集合接口,一个Collection代表一组...