Maven启动postgres服务器 [英] maven start postgres server

查看:141
本文介绍了Maven启动postgres服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个学校项目工作,需要在maven项目中用postgresql替换hsqldb.

当前,我们通过运行来启动hsql Server

mvn exec:java -P hsqldb

据我所知,这会在pom.xml中查找hsqldb配置文件

<profile>
  <id>hsqldb</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>org.hsqldb.server.Server</mainClass>
          <arguments>
            <argument>-database.0</argument>
            <argument>db/name</argument>
            <argument>-dbname.0</argument>
            <argument>name</argument>
          </arguments>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

所以我需要使用postgres设置来使这部分相关,我已经添加了postgres作为依赖项.

<mainClass>ServerClass</mainClass>

最大的问题是我无法在postgresql jar中找到主服务器类

解决方案

PostgreSQL不是内存中可嵌入的服务器.它不能只是作为库加载并运行.

您需要一些测试工具控制代码才能实现:

  • initdb临时数据目录

  • 根据需要通过添加行或启用include_dir并在其中放置文件来编辑postgresql.conf.如果您只需要简单的配置,则可以跳过此步骤.可以通过环境变量来设置port之类的东西,并且可以通过服务器启动命令的-c标志来设置许多其他内容.

  • 生成一个随机端口号以供使用

  • 启动服务器-使用Java的Process管理运行postgres -D the_datadir一个新的服务器实例.您可以使用-c传递自定义配置值,还可以设置环境变量来控制行为.

  • 通过JDBC和CREATE DATABASE连接,然后运行测试设置

  • 运行测试

  • 通过终止启动的进程来停止服务器.

  • 删除数据目录

您将要在具有适当临时目录处理的类中进行流程管理,在不干净的退出时进行清理(杀死Pg服务器并在异常时删除datadir),等等.

如果您找不到用于测试的罐装测试工具代码,我会感到惊讶.我想知道是否应该向PgJDBC添加一些内容并将其捆绑在驱动程序中?如果您发现任何好的内容或写了任何好的内容,请通过对此答案进行评论来对我进行ping操作,我将其视为实用程序类包含在PgJDBC中.

也就是说,在已经在服务器上运行的现有PostgreSQL实例中,在新创建的测试数据库中运行测试的情况更为常见.您只需使用PostgreSQL用户名/密码/主机/端口/数据库配置测试套件(或使其连接到postgres数据库以及CREATEDROP数据库).毕竟,总是需要进行一些配置:安装或编译PostgreSQL,确保二进制文件在PATH等上.

另一个选择是使用Amazon RDS:使用AWS RDS API启动新的PostgreSQL实例并将其用于测试.除非您由于设置时间和最少的运行时间而长时间运行测试,否则这可能不切实际,但这是另一种选择,就像Heroku上的类似服务一样.

I am working on a project for school and need to replace hsqldb with postgresql in a maven project.

Currently we start the hsql Server by running

mvn exec:java -P hsqldb

To my knowlege this looks up the hsqldb profile in the pom.xml

<profile>
  <id>hsqldb</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>org.hsqldb.server.Server</mainClass>
          <arguments>
            <argument>-database.0</argument>
            <argument>db/name</argument>
            <argument>-dbname.0</argument>
            <argument>name</argument>
          </arguments>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

So I need to relace this part with a postgres setup, I already added postgres as a dependency.

<mainClass>ServerClass</mainClass>

is my biggest problem since I cant find the main server class in the postgresql jar

解决方案

PostgreSQL isn't an in-memory embeddable server. It can't just be loaded as a library and run.

You'll need some test harness control code to:

  • initdb a temporary datadir

  • Edit postgresql.conf as required by appending lines, or by enabling an include_dir and dropping files in it. If you only need a trivial config you might be able to skip this step; things like the port can be set via environment variables, and many others can be set with -c flags to the server startup command.

  • generate a random port number to use

  • Launch the server - run postgres -D the_datadir a new server instance using Java's Process management. You can pass custom config values with -c and can also set environment variables to control behaviour.

  • Connect over JDBC and CREATE DATABASE then run your test setup

  • Run your tests

  • Stop the server by killing the process you launched.

  • Delete the datadir

You'll want to do the process management in a class with appropriate temp directory handling, cleanup on unclean exit (kill the Pg server and delete the datadir on exception), etc.

I'll be surprised if you can't find canned test harness code to borrow for this. I wonder if we should add some to PgJDBC and bundle it in the driver? If you find anything good, or write anything good, please ping me by commenting on this answer and I'll consider it for inclusion as a utility class in PgJDBC.

That said, it is much more common to instead run your tests in a newly created test database in an existing PostgreSQL instance that already runs on your server. You just configure your test suite with a PostgreSQL username/password/host/port/database (or let it connect to the postgres database and CREATE and DROP the database). After all, some configuration is always going be required: installing or compiling PostgreSQL, making sure the binaries are on the PATH, etc.

Another option is to use Amazon RDS: Use the AWS RDS APIs to launch a new PostgreSQL instance and use it for your tests. This probably isn't practical unless your tests run for a long time due to the setup time and minimum runtime, but it's another option, as is the similar service at Heroku.

这篇关于Maven启动postgres服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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