I am trying to create an integration test, which requires a running PostgreSQL server. Is it possible to start the server in maven build and stop it when tests are completed (in a separate process, I think)? Assuming the PostgreSQL server is not installed on the machine.
-
Can you start and stop the server, say, from command line? If yes, then just use the maven-antrun-plugin in pre/post-integration-test phases. – lexicore Jun 15 '11 at 22:34
-
As I mentioned in the question - the PostgreSQL Server is **not** installed on the machine. – yegor256 Jun 15 '11 at 22:42
-
I never mentioned starting/stopping the server on the _local_ machine. – lexicore Jun 15 '11 at 22:51
-
In my case it's not installed anywhere. I need to install, start, test, stop, uninstall - everything inside Maven build cycle. – yegor256 Jun 15 '11 at 23:03
6 Answers
You are trying to push maven far beyond the intended envelope, so you'll be in for a fair amount of hurt before it will work.
Luckily postgresql can be downloaded as a zip archive.
As already mentioned above maven can use ant tasks to extend its reach. Ant has a large set of tasks to unzip files, and run commands. The sequence would be as follows :
- unzip postgresql-xxx.zip in a well known directory --> INSTALL_DIR
- create a data directory --> DATA_DIR
- /bin/init-db -D
- /bin/postgres -D
- /bin/create_db -EUNICODE test
This should give you a running server with a test database.
Further issues : create a user, security (you likely want to connect via TCP/IP but this is disabled by default if I recall correct, this requires editing a config file before starting the database)
...
Good Luck.

- 34,983
- 11
- 83
- 114
-
thanks, makes sense. Postregsql is available in Maven Central as a ZIP archive? – yegor256 Jun 16 '11 at 18:18
-
It is now, but not officially supported: com.github.adrianboimvaser:postgresql-dist:9.2.4 – adrianboimvaser Aug 31 '13 at 23:14
I started writing a plugin for this purpose:
https://github.com/adrianboimvaser/postgresql-maven-plugin
It's in a very early stage and lacks documentation, but mostly works. I already released version 0.1 to Maven Central.
I'm also releasing PostgreSQL binary distributions for all platforms as maven artifacts. You can find the usage pattern in the plugin's integration tests.
Cheers!

- 2,651
- 1
- 22
- 30
Just to bring some fresh perspective into this matter:
You could also start the postgresql database as docker instance. The plugin ecosystem for docker seems to be still in flux, so you might need to decide yourself which fits. Here are a few links to speed up your search:

- 7,968
- 6
- 48
- 70
I think the usual scenario is to have a running integration test db, and not to shut it down/ restart it between builds.
But if you really want to you could set up your continuous integration server to start/ stop the db.

- 37,247
- 13
- 80
- 152
-
Yes, I really want. How exactly can I do it from Maven? Are there any plugins for PostreSQL? I need to install it, start, run tests, stop, uninstall. – yegor256 Jun 15 '11 at 23:02
You sound like you are trying to build a full continuous integration environment. You should probably look into using a full CI tool such as Cruise Control or Bamboo.
How I've done it before is to set up a dedicated CI db that is accessible from the CI server, and then have a series of bash/python/whatever scripts run as a After Successful Build step which can then run whatever extra integration tasks you like. Pair that with something like liquibase and you could wipe out the CI db and make sure it is up to the latest schema every build.

- 111
- 1
- 9