问题
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 datadirEdit
postgresql.conf
as required by appending lines, or by enabling aninclude_dir
and dropping files in it. If you only need a trivial config you might be able to skip this step; things like theport
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'sProcess
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 setupRun 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