diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractTask.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractTask.java index 698572ce..f3152ab4 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractTask.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractTask.java @@ -13,6 +13,7 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.settings.Settings; import org.openmrs.maven.plugins.model.Server; +import org.openmrs.maven.plugins.utility.ConfigurationInstaller; import org.openmrs.maven.plugins.utility.DefaultJira; import org.openmrs.maven.plugins.utility.DistroHelper; import org.openmrs.maven.plugins.git.DefaultGitHelper; @@ -134,6 +135,11 @@ public abstract class AbstractTask extends AbstractMojo { */ DockerHelper dockerHelper; + /** + * Installs configurations + */ + ConfigurationInstaller configurationInstaller; + public AbstractTask() { } @@ -156,6 +162,7 @@ public AbstractTask(AbstractTask other) { this.testMode = other.testMode; this.openMRSPath = other.openMRSPath; this.stats = other.stats; + this.configurationInstaller = other.configurationInstaller; initTask(); } @@ -184,6 +191,9 @@ public void initTask() { if (dockerHelper == null) { dockerHelper = new DockerHelper(mavenProject, mavenSession, pluginManager, wizard); } + if(configurationInstaller == null) { + configurationInstaller = new ConfigurationInstaller(wizard, moduleInstaller); + } if (StringUtils.isNotBlank(openMRSPath)) { Server.setServersPath(openMRSPath); } diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java index 0c7970cd..3dad0ed9 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java @@ -169,7 +169,7 @@ public Setup(AbstractTask other) { * file will be created after the database is initialized. Setup should proceed to install * modules based on the OpenMRS WAR file for the given platform version. * As of this writing, this function can return null only in platform mode. - * + openmrs-sdk [org.openmrs.maven.plugins:openmrs-sdk-maven-plugin:5.0.0-SNAPSHOT:setup]* * @param server An initialized Server instance * @return distro properties instantiated by DistroHelper * @throws MojoExecutionException @@ -205,6 +205,8 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper); Artifact artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId(), "zip"); + distroProperties = distroHelper.downloadDistroProperties(server.getServerDirectory(), server, "zip"); + distroProperties.addProperties(PropertiesUtils.getConfigurationProperty(artifact)); Properties frontendProperties; if (server.getVersion().equals(versionsHelper.getLatestSnapshotVersion(artifact))) { frontendProperties = PropertiesUtils.getFrontendPropertiesFromSpaConfigUrl( @@ -220,12 +222,6 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu if(appShellVersion != null) { frontendProperties.setProperty("spa.core", appShellVersion); } - Properties allProperties = new Properties(); - allProperties.putAll(backendProperties); - allProperties.putAll(spaModuleProperty); - allProperties.putAll(frontendProperties); - allProperties.putAll(configurationProperties); - distroProperties = new DistroProperties(allProperties); platformMode = false; break; @@ -286,7 +282,7 @@ public void setup(Server server, DistroProperties distroProperties) throws MojoE distroHelper.savePropertiesToServer(distroProperties, server); setServerVersionsFromDistroProperties(server, distroProperties); moduleInstaller.installModulesForDistro(server, distroProperties, distroHelper); - setConfigFolder(server, distroProperties); + configurationInstaller.setConfigurationFolder(server, distroProperties); if (spaInstaller != null) { spaInstaller.installFromDistroProperties(server.getServerDirectory(), distroProperties, ignorePeerDependencies); } @@ -352,57 +348,6 @@ private void downloadOWAs(File targetDirectory, DistroProperties distroPropertie } } - /** - * Sets the configuration folder for the specified server using the provided distro properties. - * - * @param server The server for which to set the configuration folder. - * @param distroProperties The distro properties containing the configuration information. - */ - private void setConfigFolder(Server server, DistroProperties distroProperties) throws MojoExecutionException { - if(distroProperties.getConfigArtifacts().isEmpty()) { - return; - } - File configDir = new File(server.getServerDirectory(), SDKConstants.OPENMRS_SERVER_CONFIGURATION); - configDir.mkdir(); - downloadConfigs(distroProperties, configDir); - File referenceApplicationFile = new File(configDir, "referenceapplication-distro.owa"); - if (!referenceApplicationFile.exists()) { - return; - } - try { - ZipFile zipFile = new ZipFile(referenceApplicationFile); - zipFile.extractAll(configDir.getPath()); - for (File file : Objects.requireNonNull(configDir.listFiles())) { - if (file.getName().equals("openmrs_config")) { - FileUtils.copyDirectory(file, configDir); - } - FileUtils.deleteQuietly(file); - } - FileUtils.deleteQuietly(referenceApplicationFile); - } - catch (ZipException | IOException e) { - throw new RuntimeException(e); - } - } - - /** - * Downloads the configuration artifact specified in the distro properties and saves them in the provided config directory. - * - * @param distroProperties The distro properties containing the configuration artifacts to download. - * @param configDir The directory where the configuration files will be saved. - * @throws MojoExecutionException If an error occurs while downloading the configuration files. - */ - private void downloadConfigs(DistroProperties distroProperties, File configDir) throws MojoExecutionException { - List configs = distroProperties.getConfigArtifacts(); - wizard.showMessage("Downloading Configs...\n"); - if (!configs.isEmpty()) { - for (Artifact config : configs) { - wizard.showMessage("Downloading Config: " + config); - owaHelper.downloadOwa(configDir, config, moduleInstaller); - } - } - } - private void wipeDatabase(Server server) throws MojoExecutionException { String uri = getUriWithoutDb(server); try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) { diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ConfigurationInstaller.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ConfigurationInstaller.java new file mode 100644 index 00000000..151df0ce --- /dev/null +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ConfigurationInstaller.java @@ -0,0 +1,80 @@ +package org.openmrs.maven.plugins.utility; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import org.apache.commons.io.FileUtils; +import org.apache.maven.plugin.MojoExecutionException; +import org.openmrs.maven.plugins.model.Artifact; +import org.openmrs.maven.plugins.model.DistroProperties; +import org.openmrs.maven.plugins.model.Server; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Objects; + +public class ConfigurationInstaller { + + private final Wizard wizard; + + private final ModuleInstaller moduleInstaller; + + public ConfigurationInstaller(Wizard wizard, ModuleInstaller moduleInstaller) { + this.wizard = wizard; + this.moduleInstaller = moduleInstaller; + } + + /** + * Sets the configuration folder for the specified server using the provided distro properties. + * + * @param server The server for which to set the configuration folder. + * @param distroProperties The distro properties containing the configuration information. + */ + public void setConfigurationFolder(Server server, DistroProperties distroProperties) throws MojoExecutionException { + if(distroProperties.getConfigArtifacts().isEmpty()) { + return; + } + File configDir = new File(server.getServerDirectory(), SDKConstants.OPENMRS_SERVER_CONFIGURATION); + configDir.mkdir(); + downloadConfigurations(distroProperties, configDir); + File distroJar = new File(server.getServerDirectory(),"openmrs-distro.jar"); + if (!distroJar.exists()) { + return; + } + try { + ZipFile zipFile = new ZipFile(distroJar); + zipFile.extractAll(configDir.getPath()); + for (File file : Objects.requireNonNull(configDir.listFiles())) { + if (file.getName().equals("openmrs_config")) { + FileUtils.copyDirectory(file, configDir); + } + file.delete(); + } + } + catch (ZipException | IOException e) { + throw new RuntimeException(e); + } + finally { + distroJar.delete(); + } + } + + /** + * Downloads the configuration artifact specified in the distro properties and saves them in the provided config directory. + * + * @param distroProperties The distro properties containing the configuration artifacts to download. + * @param configDir The directory where the configuration files will be saved. + * @throws MojoExecutionException If an error occurs while downloading the configuration files. + */ + private void downloadConfigurations(DistroProperties distroProperties, File configDir) throws MojoExecutionException { + List configs = distroProperties.getConfigArtifacts(); + wizard.showMessage("Downloading Configs...\n"); + if (!configs.isEmpty()) { + for (Artifact config : configs) { + config.setDestFileName("openmrs-distro.jar"); + wizard.showMessage("Downloading Config: " + config); + moduleInstaller.installModule(config, configDir.getPath()); + } + } + } +} diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java index cec9fa94..43c6c69d 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java @@ -230,6 +230,14 @@ public void resolvePlaceholders(Properties projectProperties) throws MojoExecuti } } + public void addProperties(Properties properties) { + this.properties.putAll(properties); + } + + public void addProperty(String name, String value) { + this.properties.setProperty(name, value); + } + private String getPlaceholderKey(String string){ int startIndex = string.indexOf("${")+2; int endIndex = string.indexOf("}", startIndex); diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java index 8c215ec7..fa7aac2c 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java @@ -718,8 +718,8 @@ public String promptForRefAppVersion(VersionsHelper versionsHelper, String custo public String promptForO3RefAppVersion(VersionsHelper versionsHelper, String customMessage) throws MojoExecutionException { - Map optionsMap = getO3VersionsOptionsMap(versionsHelper, REFAPP_OPTION_TMPL, - O3_ARTIFACT_TMPL); + Map optionsMap = getO3VersionsOptionsMap(versionsHelper + ); return promptForO3Version(optionsMap, customMessage); } @@ -771,17 +771,14 @@ private Map getDistroVersionsOptionsMap(Set versions, Ve /** * Returns a map of options based on the versions of O3 * - * @param versionsHelper The VersionsHelper object to retrieve the artifact versions from. - * @param optionTemplate The template for generating option keys in the map. - * @param artifactTemplate The template for generating artifact values in the map. + * @param versionsHelper The VersionsHelper object to retrieve the artifact versions from. * @return A LinkedHashMap containing the generated options map. */ - private Map getO3VersionsOptionsMap(VersionsHelper versionsHelper, - String optionTemplate, String artifactTemplate) { + private Map getO3VersionsOptionsMap(VersionsHelper versionsHelper) { Map optionsMap = new LinkedHashMap<>(); - Artifact artifact = new Artifact("referenceapplication-distro", "3.0.0-SNAPSHOT", "org.openmrs.distro", "zip"); + Artifact artifact = new Artifact("referenceapplication-distro", "version", "org.openmrs.distro", "zip"); for (ArtifactVersion version : versionsHelper.getAllVersions(artifact, MAX_OPTIONS_SIZE)) { - optionsMap.put(String.format(optionTemplate, version.toString()), String.format(artifactTemplate, version)); + optionsMap.put(String.format(DefaultWizard.REFAPP_OPTION_TMPL, version.toString()), String.format(DefaultWizard.O3_ARTIFACT_TMPL, version)); } return optionsMap; } diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DistroHelper.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DistroHelper.java index d8d2bbcf..6e70dd6e 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DistroHelper.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DistroHelper.java @@ -260,6 +260,29 @@ public DistroProperties downloadDistroProperties(File path, Artifact artifact) t return distroProperties; } + public DistroProperties downloadDistroProperties(File path, Artifact artifact, String distroPropertiesFileName) throws MojoExecutionException { + File file = downloadDistro(path, artifact); + DistroProperties distroProperties = null; + try (ZipFile zipFile = new ZipFile(file)) { + Enumeration entries = zipFile.entries(); + while (entries.hasMoreElements()) { + ZipEntry zipEntry = entries.nextElement(); + if (distroPropertiesFileName.equals(zipEntry.getName())) { + Properties properties = new Properties(); + properties.load(zipFile.getInputStream(zipEntry)); + distroProperties = new DistroProperties(properties); + } + } + } + catch (IOException e) { + throw new MojoExecutionException("Could not read \"" + file.getAbsolutePath() + "\" " + e.getMessage(), e); + } + finally { + file.delete(); + } + return distroProperties; + } + public DistroProperties downloadDistroProperties(File serverPath, Server server) throws MojoExecutionException { Artifact artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId(), "jar"); @@ -270,6 +293,16 @@ public DistroProperties downloadDistroProperties(File serverPath, Server server) } } + public DistroProperties downloadDistroProperties(File serverPath, Server server, String fileType) throws MojoExecutionException { + Artifact artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId(), + fileType); + if (StringUtils.isNotBlank(artifact.getArtifactId())) { + return downloadDistroProperties(serverPath, artifact, "distro.properties"); + } else { + return null; + } + } + /** * Distro can be passed in two ways: either as maven artifact identifier or path to distro file * Returns null if string is invalid as path or identifier