Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Added flag to switch between single instance for all multimodule pr… #9

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<groupId>de.funfried.maven.plugins</groupId>
<artifactId>zonky-maven-plugin</artifactId>
<version>0.5-SNAPSHOT</version>
<version>0.5</version>
<packaging>maven-plugin</packaging>

<developers>
Expand Down
5 changes: 4 additions & 1 deletion src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
</properties>

<body>
<release version="0.5-SNAPSHOT" date="n/a" description="n/a">
<release version="0.5" date="2024-10-08" description="Feature release">
<action dev="bahlef" type="add">
Added flag to switch between single instance for all multimodule project or an instance for each sub module (which configures the plugin)
</action>
</release>

<release version="0.4" date="2024-10-07" description="Bug fix release">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
*/
Expand Down Expand Up @@ -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.");
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ public class StopEmbeddedPostgresMojo extends AbstractMojo {
@Parameter(defaultValue = "${reactorProjects}", readonly = true, required = true)
private List<MavenProject> 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.
*
* @throws MojoExecutionException if an error occurs
*/
@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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private MavenProjectUtil() {

public static <E extends Object> E getProjectProperty(MavenProject project, List<MavenProject> 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) {
Expand All @@ -60,18 +60,22 @@ public static void putProjectProperty(MavenProject project, List<MavenProject> r
project.getProperties().put(item.getKey(), item.getValue());
}

for (MavenProject p : reactorProjects) {
for (Map.Entry<String, Object> item : properties.entrySet()) {
p.getProperties().put(item.getKey(), item.getValue());
if (reactorProjects != null) {
for (MavenProject p : reactorProjects) {
for (Map.Entry<String, Object> item : properties.entrySet()) {
p.getProperties().put(item.getKey(), item.getValue());
}
}
}
}

public static void removeProjectProperty(MavenProject project, List<MavenProject> 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);
}
}
}

Expand All @@ -80,9 +84,11 @@ public static void removeProjectProperties(MavenProject project, List<MavenProje
project.getProperties().remove(key);
}

for (MavenProject p : reactorProjects) {
for (String key : propertyKeys) {
p.getProperties().remove(key);
if (reactorProjects != null) {
for (MavenProject p : reactorProjects) {
for (String key : propertyKeys) {
p.getProperties().remove(key);
}
}
}
}
Expand Down
Loading