jboss maven_使用Maven配置JBoss / Wildfly数据源

jboss maven

大多数Java EE应用程序在其业务逻辑中使用数据库访问,因此开发人员经常面临在应用程序服务器中配置驱动程序和数据库连接属性的需求。 在本文中,我们将使用Maven为JBoss / Wildfly和Postgre数据库自动化该任务。 这项工作是根据我从以前的魔兽拍卖批量应用世界岗位

Maven配置

首先,将以下内容添加到我们的pom.xml

Wildfly Maven插件


   
   
    
    
    
     
     org.wildfly.plugins
    
    
    
    
    
     
     wildfly-maven-plugin
    
    
    
    
    
     
     1.0.2.Final
    
    
    
    
    
        
     
     
            
      
      
       
       false
      
      
            
      
       
      
      
        
     
     
    
    
    
    
    
    
        
     
     
            
      
      
       
       org.postgresql
      
      
            
      
      
       
       postgresql
      
      
            
      
      
       
       9.3-1102-jdbc41
      
      
        
     
     
    
    
    

   
   

我们将使用Wildfly Maven插件在应用程序服务器中使用命令执行脚本。 请注意,我们还向Postgre驱动程序添加了依赖项。 这是供Maven下载的依赖项,因为我们稍后将需要它来将其添加到服务器中。 还有一个${cli.file}属性将被分配给一个配置文件。 这是为了指示我们要执行哪个脚本。

让我们还将以下内容添加到pom.xml

Maven资源插件


   
   
    
    
    
     
     org.apache.maven.plugins
    
    
    
    
    
     
     maven-resources-plugin
    
    
    
    
    
     
     2.6
    
    
    
    
    
        
     
     
            
      
      
       
       copy-resources
      
      
            
      
      
       
       process-resources
      
      
            
      
      
                
       
       
         copy-resources 
       
            
      
      
            
      
      
                
       
       
         ${basedir}/target/scripts 
       
                
       
        
         
         
           src/main/resources/scripts 
          
         
           true 
          
         
       
                
       
        
        
          ${basedir}/src/main/resources/configuration.properties 
         
       
            
      
      
        
     
     
    
    
    

   
   

使用Resources Maven插件,我们将过滤src/main/resources/scripts包含的脚本文件,并用${basedir}/src/main/resources/configuration.properties文件中包含的属性替换它们。

最后,使用我们要运行的脚本将一些Maven配置文件添加到pom.xml

Maven个人资料


   
   
    
    
    
        
     
     
      
      install-driver
     
     
        
     
     
            
      
      
       
       wildfly-install-postgre-driver.cli
      
      
        
     
     
    
    
    

    
    
    
        
     
     
      
      remove-driver
     
     
        
     
     
            
      
      
       
       wildfly-remove-postgre-driver.cli
      
      
        
     
     
    
    
    

    
    
    
        
     
     
      
      install-wow-auctions
     
     
        
     
     
            
      
      
       
       wow-auctions-install.cli
      
      
        
     
     
    
    
    

    
    
    
        
     
     
      
      remove-wow-auctions
     
     
        
     
     
            
      
      
       
       wow-auctions-remove.cli
      
      
        
     
     
    
    
    

   
   

Wildfly脚本文件

添加驱动

带有用于添加驱动程序的命令的脚本:

wildfly-install-postgre-driver.cli
# Connect to Wildfly instance
connect

# Create Oracle JDBC Driver Module
# If the module already exists, Wildfly will output a message saying that the module already exists and the script exits.
module add \
    --name=org.postgre \
    --resources=${settings.localRepository}/org/postgresql/postgresql/9.3-1102-jdbc41/postgresql-9.3-1102-jdbc41.jar \
    --dependencies=javax.api,javax.transaction.api

# Add Driver Properties
/subsystem=datasources/jdbc-driver=postgre: \
    add( \
        driver-name="postgre", \
        driver-module-name="org.postgre")

数据库驱动程序作为模块添加到Wildfly 。 在这种情况下,驱动程序可广泛用于服务器中部署的所有应用程序。 使用${settings.localRepository}我们指向下载到本地Maven存储库的数据库驱动程序jar。 还记得我们添加到Wildfly Maven插件中的依赖吗? 当您运行插件时,将下载驱动程序并将其添加到服务器。 现在,要运行我们执行的脚本(您需要运行应用程序服务器):

mvn process-resources wildfly:execute-commands -P "install-driver"

需要process-resources生命周期来替换脚本文件中的属性。 在我的情况下, ${settings.localRepository}/Users/radcortez/.m3/repository/代替。 检查target/scripts文件夹。 运行命令后,您应该在Maven日志中看到以下输出:

{"outcome" => "success"}

在服务器上:

INFO  [org.jboss.as.connector.subsystems.datasources] (management-handler-thread - 4) JBAS010404: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 9.3)
INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) JBAS010417: Started Driver service with driver-name = postgre
wildfly-删除-postgre-driver.cli
# Connect to Wildfly instance
connect

if (outcome == success) of /subsystem=datasources/jdbc-driver=postgre:read-attribute(name=driver-name)

    # Remove Driver
    /subsystem=datasources/jdbc-driver=postgre:remove

end-if

# Remove Oracle JDBC Driver Module
module remove --name=org.postgre

该脚本用于从应用程序服务器中删除驱动程序。 执行mvn wildfly:execute-commands -P "remove-driver" 。 如果您之前已经执行过命令,则不需要process-resources ,除非您更改了脚本。

添加数据源

wow-auctions-install.cli

带有用于添加数据源的命令的脚本:

wow-auctions-install.cli

# Connect to Wildfly instance
connect

# Create Datasource
/subsystem=datasources/data-source=WowAuctionsDS: \
    add( \
    	jndi-name="${datasource.jndi}", \
    	driver-name=postgre, \
    	connection-url="${datasource.connection}", \
    	user-name="${datasource.user}", \
    	password="${datasource.password}")

/subsystem=ee/service=default-bindings:write-attribute(name="datasource", value="${datasource.jndi}")

我们还需要一个文件来定义属性:

configuration.properties
datasource.jndi=java:/datasources/WowAuctionsDS
datasource.connection=jdbc:postgresql://localhost:5432/wowauctions
datasource.user=wowauctions
datasource.password=wowauctions

默认的Java EE 7数据源

Java EE 7指定容器应提供默认的数据源。 与其在应用程序中定义JNDI名称java:/datasources/WowAuctionsDS将新创建的数据源指向具有/subsystem=ee/service=default-bindings:write-attribute(name="datasource", value="${datasource.jndi}") 。 这样,我们无需在应用程序中进行任何更改。 使用mvn wildfly:execute-commands -P "install-wow-auctions"执行脚本。 您应该获得以下Maven输出:

org.jboss.as.cli.impl.CommandContextImpl printLine
INFO: {"outcome" => "success"}
{"outcome" => "success"}
org.jboss.as.cli.impl.CommandContextImpl printLine
INFO: {"outcome" => "success"}
{"outcome" => "success"}

在服务器上:

INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source
wow-auctions-remove.cli
# Connect to Wildfly instance
connect

# Remove Datasources
/subsystem=datasources/data-source=WowAuctionsDS:remove

/subsystem=ee/service=default-bindings:write-attribute(name="datasource", value="java:jboss/datasources/ExampleDS")

这是用于删除数据源并还原Java EE 7默认数据源的脚本。 通过执行mvn wildfly:execute-commands -P "remove-wow-auctions"运行它

结论

这篇文章演示了如何自动向Wildfly实例添加/删除驱动程序以及如何添加/删除数据源。 如果要在数据库之间切换,或者要从头开始配置服务器,这将很有用。 考虑一下CI环境。 这些脚本也可以轻松调整为其他驱动程序。

请享用!

翻译自: https://www.javacodegeeks.com/2014/10/configure-jboss-wildfly-datasource-with-maven.html

jboss maven

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值