View Javadoc
1   package de.funfried.maven.plugin.zonky;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.List;
6   import java.util.concurrent.TimeoutException;
7   
8   import org.apache.maven.plugin.AbstractMojo;
9   import org.apache.maven.plugin.MojoExecutionException;
10  import org.apache.maven.plugins.annotations.LifecyclePhase;
11  import org.apache.maven.plugins.annotations.Mojo;
12  import org.apache.maven.plugins.annotations.Parameter;
13  import org.apache.maven.project.MavenProject;
14  
15  import de.funfried.maven.plugin.zonky.utils.MavenProjectUtil;
16  import de.funfried.maven.plugin.zonky.utils.ZonkyUtil;
17  import io.zonky.test.db.postgres.embedded.EmbeddedPostgres;
18  
19  /**
20   * Goal which stops the embedded postgres database.
21   */
22  @Mojo(name = "stop", defaultPhase = LifecyclePhase.PRE_CLEAN, threadSafe = false)
23  public class StopEmbeddedPostgresMojo extends AbstractMojo {
24  	/**
25  	 * The maven project.
26  	 */
27  	@Parameter(defaultValue = "${project}", required = true, readonly = true)
28  	private MavenProject project;
29  
30  	/**
31  	 * Contains the full list of projects in the reactor.
32  	 */
33  	@Parameter(defaultValue = "${reactorProjects}", readonly = true, required = true)
34  	private List<MavenProject> reactorProjects;
35  
36  	/**
37  	 * If {@code true}, only one database instance is used for all multimodule projects, otherwise all projects get their own instance.
38  	 */
39  	@Parameter(defaultValue = "true", property = "singleInstance")
40  	private boolean singleInstance;
41  
42  	/**
43  	 * Stops the embedded postgres database.
44  	 *
45  	 * @throws MojoExecutionException if an error occurs
46  	 */
47  	@Override
48  	public void execute() throws MojoExecutionException {
49  		EmbeddedPostgres pg = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_DB_INSTANCE);
50  		if (pg != null) {
51  			String workDir = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_WORK_DIRECTORY);
52  			String dataDir = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_DATA_DIRECTORY);
53  
54  			File workDirFile = new File(workDir);
55  			File dataDirFile = new File(dataDir);
56  
57  			try {
58  				ZonkyUtil.stop(pg, workDirFile, dataDirFile);
59  
60  				stopped();
61  			} catch (IOException | InterruptedException | TimeoutException ex) {
62  				getLog().error("Failed to stop database", ex);
63  			}
64  		}
65  	}
66  
67  	private void stopped() {
68  		getLog().info("Stopped embedded postgres database at port " + MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_PORT) + " (JDBC URL: "
69  				+ MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_JDBC_URL) + ")");
70  
71  		MavenProjectUtil.removeAllProjectProperties(project, singleInstance ? reactorProjects : null);
72  	}
73  }