I have written an OSS plugin to start/stop Derby during a Maven build process.
The plugin works fine in plain old single modules. However, if I have an aggregator of several modules and more than one of them has database-related tests, I seem to be hitting some weird problem.
I am invoking the plugin’s start and stop goals (respectively during the process-resources and test phases), as shown below:
<plugin>
<groupId>org.carlspring.maven</groupId>
<artifactId>derby-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<failIfAlreadyRunning>false</failIfAlreadyRunning>
</configuration>
<executions>
<execution>
<id>start-derby</id>
<phase>process-resources</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-derby</id>
<phase>test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
The problem consists in the fact that upon the second attempt to start the Derby (in-memory) server, Derby appears to still be running, or to have loaded with the database contents from the first module. I can tell this because the first module in the aggregator creates and populates some data in a table. My expectation is that once I’ve shutdown Derby and started it all over again from the other module, that it would be a fresh database without any existing contents.
Here is my code inside the plugin that deals with shutting down Derby:
try
{
try
{
server.ping();
}
catch (Exception e)
{
if (failIfNotRunning)
{
throw new MojoExecutionException("Failed to stop the Derby server, no server running!", e);
}
getLog().error("Derby server was already stopped.");
return;
}
server.shutdown();
while (true)
{
Thread.sleep(1000);
try
{
server.ping();
}
catch (Exception e)
{
getLog().info("Derby has stopped!");
return;
}
}
}
catch (Exception e)
{
throw new MojoExecutionException(e.getMessage(), e);
}
The full source of this rather simple plugin can be checked out or viewed in GitHub here.
I ended up doing the following:
This wasn’t the way I expected it had to be done.