maven start postgres server [duplicate]

北城以北 提交于 2019-12-08 07:33:07

问题


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


回答1:


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.




回答2:


I believe you need this:

<mainClass>org.postgresql.Driver</mainClass>


来源:https://stackoverflow.com/questions/24035743/maven-start-postgres-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!