/** * Get attribute values and prompt if not selected * * @param helper * @param groupId * @param artifactId * @param version * @return */ public Artifact getArtifactForSelectedParameters( AttributeHelper helper, String groupId, String artifactId, String version) throws MojoExecutionException { File pomFile = new File(System.getProperty("user.dir"), SDKConstants.OPENMRS_MODULE_POM); String moduleGroupId = null; String moduleArtifactId = null; String moduleVersion = null; if (pomFile.exists() && (new ConfigurationManager(pomFile.getPath()).isOmod()) && (artifactId == null)) { ConfigurationManager manager = new ConfigurationManager(pomFile.getPath()); if (manager.getParent() != null) { moduleGroupId = manager.getParent().getGroupId(); moduleArtifactId = manager.getParent().getArtifactId(); moduleVersion = (version != null) ? version : manager.getParent().getVersion(); } else { moduleGroupId = manager.getGroupId(); moduleArtifactId = manager.getArtifactId(); moduleVersion = (version != null) ? version : manager.getVersion(); } } else { moduleGroupId = groupId; try { moduleArtifactId = helper.promptForValueIfMissing(artifactId, "artifactId"); moduleVersion = helper.promptForValueIfMissing(version, "version"); } catch (PrompterException e) { getLog().error(e.getMessage()); } } return new Artifact(moduleArtifactId, moduleVersion, moduleGroupId); }
/** * Install module to selected server * * @param serverId * @param groupId * @param artifactId * @param version * @throws MojoExecutionException * @throws MojoFailureException */ public void installModule(String serverId, String groupId, String artifactId, String version) throws MojoExecutionException, MojoFailureException { AttributeHelper helper = new AttributeHelper(prompter); if (serverId == null) { File currentProperties = helper.getCurrentServerPath(); if (currentProperties != null) serverId = currentProperties.getName(); } File serverPath = helper.getServerPath(serverId); Artifact artifact = getArtifactForSelectedParameters(helper, groupId, artifactId, version); String originalId = artifact.getArtifactId(); artifact.setArtifactId(artifact.getArtifactId() + "-omod"); File modules = new File(serverPath, SDKConstants.OPENMRS_SERVER_MODULES); modules.mkdirs(); Element[] artifactItems = new Element[1]; artifactItems[0] = artifact.toElement(modules.getPath()); File[] listOfModules = modules.listFiles(); boolean versionUpdated = false; boolean removed = false; for (File itemModule : listOfModules) { String[] parts = itemModule.getName().split("-"); String oldV = StringUtils.join(Arrays.copyOfRange(parts, 1, parts.length), "-"); if (originalId.equals(parts[0])) { try { Version oldVersion = new Version(oldV.substring(0, oldV.lastIndexOf('.'))); Version newVersion = new Version(artifact.getVersion()); if (oldVersion.higher(newVersion)) { throw new MojoExecutionException( String.format(TEMPLATE_DOWNGRADE, oldVersion.toString(), newVersion.toString())); } else if (oldVersion.lower(newVersion)) { boolean agree = helper.dialogYesNo(String.format(TEMPLATE_UPDATE, artifact.getVersion())); if (!agree) { return; } } versionUpdated = true; removed = itemModule.delete(); break; } catch (PrompterException e) { throw new MojoExecutionException(e.getMessage()); } } } executeMojo( plugin( groupId(SDKConstants.PLUGIN_DEPENDENCIES_GROUP_ID), artifactId(SDKConstants.PLUGIN_DEPENDENCIES_ARTIFACT_ID), version(SDKConstants.PLUGIN_DEPENDENCIES_VERSION)), goal("copy"), configuration(element(name("artifactItems"), artifactItems)), executionEnvironment(mavenProject, mavenSession, pluginManager)); PropertyManager properties = new PropertyManager(new File(serverPath, SDKConstants.OPENMRS_SERVER_PROPERTIES).getPath()); String[] params = {artifact.getGroupId(), originalId, artifact.getVersion()}; String module = StringUtils.join(params, "/"); properties.addToValueList(SDKConstants.PROPERTY_USER_MODULES, module); properties.apply(); if (versionUpdated) { if (removed) getLog().info(String.format(DEFAULT_UPDATE_MESSAGE, originalId, artifact.getVersion())); } else getLog().info(String.format(DEFAULT_OK_MESSAGE, originalId)); }