From cbcd28957170fa28f02244acbaa144f9aed168fa Mon Sep 17 00:00:00 2001 From: fbahle Date: Tue, 8 Oct 2024 10:30:23 +0200 Subject: [PATCH] - Added flag to switch between single instance for all multimodule projects or a database per submodule - Version changed to finale version (0.5-SNAPSHOT -> 0.5) --- pom.xml | 2 +- src/changes/changes.xml | 5 +++- .../zonky/StartEmbeddedPostgresMojo.java | 18 +++++++++----- .../zonky/StopEmbeddedPostgresMojo.java | 18 +++++++++----- .../plugin/zonky/utils/MavenProjectUtil.java | 24 ++++++++++++------- 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index 7688921..e682624 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ de.funfried.maven.plugins zonky-maven-plugin - 0.5-SNAPSHOT + 0.5 maven-plugin diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e428cd9..32a40f7 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -9,7 +9,10 @@ - + + + Added flag to switch between single instance for all multimodule project or an instance for each sub module (which configures the plugin) + diff --git a/src/main/java/de/funfried/maven/plugin/zonky/StartEmbeddedPostgresMojo.java b/src/main/java/de/funfried/maven/plugin/zonky/StartEmbeddedPostgresMojo.java index 698440f..75cc3d5 100644 --- a/src/main/java/de/funfried/maven/plugin/zonky/StartEmbeddedPostgresMojo.java +++ b/src/main/java/de/funfried/maven/plugin/zonky/StartEmbeddedPostgresMojo.java @@ -42,6 +42,12 @@ public class StartEmbeddedPostgresMojo extends AbstractMojo { @Parameter(defaultValue = "true", property = "createDatabase") private boolean createDatabase; + /** + * If {@code true}, only one database instance is used for all multimodule projects, otherwise all projects get their own instance. + */ + @Parameter(defaultValue = "true", property = "singleInstance") + private boolean singleInstance; + /** * The database name to write your data. Should not be postgres! */ @@ -91,7 +97,7 @@ public class StartEmbeddedPostgresMojo extends AbstractMojo { */ @Override public void execute() throws MojoExecutionException { - EmbeddedPostgres pg = MavenProjectUtil.getProjectProperty(project, reactorProjects, MavenProjectUtil.PROP_DB_INSTANCE); + EmbeddedPostgres pg = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_DB_INSTANCE); if (pg != null) { if (AlreadyStartedPolicy.fail.equals(onAlreadyStarted)) { throw new MojoExecutionException("Embedded database already started."); @@ -111,8 +117,8 @@ public void execute() throws MojoExecutionException { throw new MojoExecutionException("Failed to reset embedded database", ex); } - String workDir = MavenProjectUtil.getProjectProperty(project, reactorProjects, MavenProjectUtil.PROP_WORK_DIRECTORY); - String dataDir = MavenProjectUtil.getProjectProperty(project, reactorProjects, MavenProjectUtil.PROP_DATA_DIRECTORY); + String workDir = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_WORK_DIRECTORY); + String dataDir = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_DATA_DIRECTORY); File workDirFile = new File(workDir); File dataDirFile = new File(dataDir); @@ -122,8 +128,8 @@ public void execute() throws MojoExecutionException { getLog().info("Embedded postgres database reinitialized"); } else if (AlreadyStartedPolicy.restart.equals(onAlreadyStarted)) { try { - String workDir = MavenProjectUtil.getProjectProperty(project, reactorProjects, MavenProjectUtil.PROP_WORK_DIRECTORY); - String dataDir = MavenProjectUtil.getProjectProperty(project, reactorProjects, MavenProjectUtil.PROP_DATA_DIRECTORY); + String workDir = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_WORK_DIRECTORY); + String dataDir = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_DATA_DIRECTORY); File workDirFile = new File(workDir); File dataDirFile = new File(dataDir); @@ -185,6 +191,6 @@ private void started(EmbeddedPostgres pg, File workingDirectory, File dataDirect properties.put(MavenProjectUtil.PROP_DATA_DIRECTORY, dataDirectory.getAbsolutePath()); properties.put(MavenProjectUtil.PROP_DB_INSTANCE, pg); - MavenProjectUtil.putProjectProperty(project, reactorProjects, properties); + MavenProjectUtil.putProjectProperty(project, singleInstance ? reactorProjects : null, properties); } } diff --git a/src/main/java/de/funfried/maven/plugin/zonky/StopEmbeddedPostgresMojo.java b/src/main/java/de/funfried/maven/plugin/zonky/StopEmbeddedPostgresMojo.java index 87fcc4b..b7a92c5 100644 --- a/src/main/java/de/funfried/maven/plugin/zonky/StopEmbeddedPostgresMojo.java +++ b/src/main/java/de/funfried/maven/plugin/zonky/StopEmbeddedPostgresMojo.java @@ -33,6 +33,12 @@ public class StopEmbeddedPostgresMojo extends AbstractMojo { @Parameter(defaultValue = "${reactorProjects}", readonly = true, required = true) private List reactorProjects; + /** + * If {@code true}, only one database instance is used for all multimodule projects, otherwise all projects get their own instance. + */ + @Parameter(defaultValue = "true", property = "singleInstance") + private boolean singleInstance; + /** * Stops the embedded postgres database. * @@ -40,10 +46,10 @@ public class StopEmbeddedPostgresMojo extends AbstractMojo { */ @Override public void execute() throws MojoExecutionException { - EmbeddedPostgres pg = MavenProjectUtil.getProjectProperty(project, reactorProjects, MavenProjectUtil.PROP_DB_INSTANCE); + EmbeddedPostgres pg = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_DB_INSTANCE); if (pg != null) { - String workDir = MavenProjectUtil.getProjectProperty(project, reactorProjects, MavenProjectUtil.PROP_WORK_DIRECTORY); - String dataDir = MavenProjectUtil.getProjectProperty(project, reactorProjects, MavenProjectUtil.PROP_DATA_DIRECTORY); + String workDir = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_WORK_DIRECTORY); + String dataDir = MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_DATA_DIRECTORY); File workDirFile = new File(workDir); File dataDirFile = new File(dataDir); @@ -59,9 +65,9 @@ public void execute() throws MojoExecutionException { } private void stopped() { - getLog().info("Stopped embedded postgres database at port " + MavenProjectUtil.getProjectProperty(project, reactorProjects, MavenProjectUtil.PROP_PORT) + " (JDBC URL: " - + MavenProjectUtil.getProjectProperty(project, reactorProjects, MavenProjectUtil.PROP_JDBC_URL) + ")"); + getLog().info("Stopped embedded postgres database at port " + MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_PORT) + " (JDBC URL: " + + MavenProjectUtil.getProjectProperty(project, singleInstance ? reactorProjects : null, MavenProjectUtil.PROP_JDBC_URL) + ")"); - MavenProjectUtil.removeAllProjectProperties(project, reactorProjects); + MavenProjectUtil.removeAllProjectProperties(project, singleInstance ? reactorProjects : null); } } diff --git a/src/main/java/de/funfried/maven/plugin/zonky/utils/MavenProjectUtil.java b/src/main/java/de/funfried/maven/plugin/zonky/utils/MavenProjectUtil.java index 4789802..d7e5190 100644 --- a/src/main/java/de/funfried/maven/plugin/zonky/utils/MavenProjectUtil.java +++ b/src/main/java/de/funfried/maven/plugin/zonky/utils/MavenProjectUtil.java @@ -37,7 +37,7 @@ private MavenProjectUtil() { public static E getProjectProperty(MavenProject project, List reactorProjects, String key) { E value = getProjectProperty(project, key); - if (value == null) { + if (value == null && reactorProjects != null) { for (MavenProject p : reactorProjects) { E v = getProjectProperty(p, key); if (v != null) { @@ -60,9 +60,11 @@ public static void putProjectProperty(MavenProject project, List r project.getProperties().put(item.getKey(), item.getValue()); } - for (MavenProject p : reactorProjects) { - for (Map.Entry item : properties.entrySet()) { - p.getProperties().put(item.getKey(), item.getValue()); + if (reactorProjects != null) { + for (MavenProject p : reactorProjects) { + for (Map.Entry item : properties.entrySet()) { + p.getProperties().put(item.getKey(), item.getValue()); + } } } } @@ -70,8 +72,10 @@ public static void putProjectProperty(MavenProject project, List r public static void removeProjectProperty(MavenProject project, List reactorProjects, String propertyKey) { project.getProperties().remove(propertyKey); - for (MavenProject p : reactorProjects) { - p.getProperties().remove(propertyKey); + if (reactorProjects != null) { + for (MavenProject p : reactorProjects) { + p.getProperties().remove(propertyKey); + } } } @@ -80,9 +84,11 @@ public static void removeProjectProperties(MavenProject project, List