Skip to content

Commit f4aad32

Browse files
authored
O3-3439: Add exclusions to distro.properties (#290)
1 parent 1f96919 commit f4aad32

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.openmrs.maven.plugins;
2+
3+
import org.apache.maven.plugin.MojoExecutionException;
4+
import org.junit.After;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.openmrs.maven.plugins.model.DistroProperties;
8+
import org.openmrs.maven.plugins.utility.DistroHelper;
9+
10+
import java.io.File;
11+
12+
import static org.junit.Assert.assertFalse;
13+
import static org.junit.Assert.assertNotNull;
14+
import static org.junit.Assert.assertTrue;
15+
16+
public class AddExclusionIntegrationTest extends AbstractSdkIntegrationTest {
17+
18+
private String distroFile;
19+
20+
private DistroProperties originalProperties;
21+
22+
@Before
23+
public void setUp() {
24+
distroFile = testDirectory + File.separator + "openmrs-distro.properties";
25+
originalProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
26+
}
27+
28+
@Test
29+
public void shouldAddExclusion() throws Exception {
30+
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
31+
assertNotNull(distroProperties);
32+
assertFalse(distroProperties.getExclusions().contains("omod.uicommons"));
33+
34+
addTaskParam("distro", distroFile);
35+
addTaskParam("property", "omod.uicommons");
36+
executeTask("exclusion");
37+
38+
assertSuccess();
39+
distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
40+
assertTrue(distroProperties.getExclusions().contains("omod.uicommons"));
41+
}
42+
43+
44+
@After
45+
public void reset() throws MojoExecutionException {
46+
originalProperties.saveTo(new File(distroFile).getParentFile());
47+
}
48+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.openmrs.maven.plugins;
2+
3+
import org.apache.commons.lang.StringUtils;
4+
import org.apache.maven.plugin.MojoExecutionException;
5+
import org.apache.maven.plugin.MojoFailureException;
6+
import org.apache.maven.plugins.annotations.Mojo;
7+
import org.apache.maven.plugins.annotations.Parameter;
8+
import org.openmrs.maven.plugins.model.Artifact;
9+
import org.openmrs.maven.plugins.model.DistroProperties;
10+
11+
import java.io.File;
12+
import java.util.List;
13+
import java.util.stream.Collectors;
14+
15+
@Mojo(name = "exclusion", requiresProject = false)
16+
public class AddExclusion extends AbstractTask {
17+
18+
/**
19+
* Path to the openmrs-distro.properties file to modify
20+
*/
21+
@Parameter(property = "distro")
22+
private String distro;
23+
24+
/**
25+
* Name of the property you want to exclude
26+
*/
27+
@Parameter(property = "property")
28+
private String property;
29+
30+
31+
@Override
32+
public void executeTask() throws MojoExecutionException, MojoFailureException {
33+
if (distro == null) {
34+
File userDir = new File(System.getProperty("user.dir"));
35+
File distroFile = new File(userDir, DistroProperties.DISTRO_FILE_NAME);
36+
if (distroFile.exists()) {
37+
distro = distroFile.getAbsolutePath();
38+
} else {
39+
throw new MojoFailureException("Please specify a distro file");
40+
}
41+
}
42+
43+
DistroProperties originalDistroProperties = null;
44+
45+
if (StringUtils.isNotBlank(distro)) {
46+
originalDistroProperties = distroHelper.resolveDistroPropertiesForStringSpecifier(distro, versionsHelper);
47+
48+
}
49+
50+
if (originalDistroProperties == null) {
51+
throw new MojoFailureException("Invalid distro file");
52+
}
53+
54+
Artifact distroArtifact = originalDistroProperties.getParentArtifact();
55+
if (StringUtils.isNotBlank(distroArtifact.getArtifactId()) && StringUtils.isNotBlank(distroArtifact.getGroupId()) && StringUtils.isNotBlank(distroArtifact.getVersion())) {
56+
DistroProperties distroProperties = distroHelper.resolveParentArtifact(distroArtifact, new File(distro).getParentFile(), originalDistroProperties, null);
57+
if (StringUtils.isBlank(property)) {
58+
List<String> currentExclusions = distroProperties.getExclusions();
59+
List<String> options = distroProperties.getPropertyNames().stream().filter(prop -> !currentExclusions.contains(prop)).collect(Collectors.toList());
60+
property = wizard.promptForMissingValueWithOptions("Enter the property you want to exclude",
61+
null, null, options);
62+
} else {
63+
if (!distroProperties.getPropertyNames().contains(property)) {
64+
wizard.showWarning("The property is not included in the parent distro");
65+
}
66+
}
67+
68+
} else {
69+
wizard.showWarning("This distro properties file does not contain a valid parent distro");
70+
if (StringUtils.isBlank(property)) {
71+
property = wizard.promptForValueIfMissing(null, "property to exclude");
72+
if (StringUtils.isBlank(property)) {
73+
throw new MojoFailureException("Invalid property name");
74+
}
75+
}
76+
}
77+
originalDistroProperties.addExclusion(property);
78+
wizard.showMessage(property + " is added to exclusions");
79+
originalDistroProperties.saveTo(new File(distro).getParentFile());
80+
}
81+
82+
}

sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,16 @@ public void removeProperty(String property) throws MojoExecutionException {
282282
properties.remove(property);
283283
}
284284

285+
public void addExclusion(String exclusion) {
286+
String exclusions = getParam("exclusions");
287+
if (exclusions == null){
288+
properties.setProperty("exclusions", exclusion);
289+
return;
290+
}
291+
292+
properties.setProperty("exclusions", exclusions + "," + exclusion);
293+
}
294+
285295
private String getPlaceholderKey(String string){
286296
int startIndex = string.indexOf("${")+2;
287297
int endIndex = string.indexOf("}", startIndex);

sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ public void showError(String textToShow) {
325325
writer.println("\n[ERROR]" + textToShow);
326326
}
327327

328+
@Override
329+
public void showWarning(String message) {
330+
writer.println("\n[WARNING]" + message);
331+
}
332+
328333
/**
329334
* Prompt for a value if it not set, and default value is NOT set
330335
*

sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ String promptForMissingValueWithOptions(String message, String value, String par
5151

5252
void showError(String message);
5353

54+
void showWarning(String message);
55+
5456
String promptForValueIfMissingWithDefault(String message, String value, String parameterName, String defValue)
5557
throws MojoExecutionException;
5658

0 commit comments

Comments
 (0)