targetProperty = resolveExternalProperty(mavenProject, newPropertyName);
+ if (targetProperty != null) {
+ return targetProperty;
+ }
+ }
+ return Pair.of(propertyName, propertyValue);
+ }
+ }
+
}
diff --git a/plugin/src/main/java/org/wildfly/channelplugin/manipulation/PomManipulator.java b/plugin/src/main/java/org/wildfly/channelplugin/manipulation/PomManipulator.java
index af6fe8f..431ece9 100644
--- a/plugin/src/main/java/org/wildfly/channelplugin/manipulation/PomManipulator.java
+++ b/plugin/src/main/java/org/wildfly/channelplugin/manipulation/PomManipulator.java
@@ -95,6 +95,10 @@ public void injectRepository(String id, String url) throws XMLStreamException {
}
}
+ public void injectProperty(String key, String version) throws XMLStreamException {
+ injectProperty(eventReader, key, version);
+ }
+
/**
* Writes the updated POM file.
*/
@@ -116,7 +120,7 @@ private void assertOpen() {
}
/**
- * This method attempts to inject new depenendency into at the end of the dependencyManagement section.
+ * This method attempts to inject new dependency into at the end of the dependencyManagement section.
*
* The dependencyManagement section must be already present in the POM.
*/
@@ -124,7 +128,7 @@ static void injectManagedDependency(ModifiedPomXMLEventReader eventReader, Artif
Collection exclusions) throws XMLStreamException {
eventReader.rewind();
- Stack stack = new Stack();
+ Stack stack = new Stack<>();
String path = "";
while (eventReader.hasNext()) {
@@ -148,6 +152,38 @@ static void injectManagedDependency(ModifiedPomXMLEventReader eventReader, Artif
}
}
+ /**
+ * This method attempts to inject new property at the end of the properties section.
+ *
+ * The properties section must be already present in the POM.
+ */
+ static void injectProperty(ModifiedPomXMLEventReader eventReader, String key, String version) throws XMLStreamException {
+ eventReader.rewind();
+
+ Stack stack = new Stack<>();
+ String path = "";
+
+ while (eventReader.hasNext()) {
+ XMLEvent event = eventReader.nextEvent();
+ if (event.isStartElement()) {
+ stack.push(path);
+ path = path + "/" + event.asStartElement().getName().getLocalPart();
+ } else if (event.isEndElement()) {
+ if (event.asEndElement().getName().getLocalPart().equals("properties")
+ && path.equals("/project/properties")) {
+ eventReader.mark(0);
+ eventReader.replaceMark(0, String.format(" <%s>%s%s>\n", key, version, key)
+ + " "
+ );
+ eventReader.clearMark(0);
+ break;
+ }
+
+ path = stack.pop();
+ }
+ }
+ }
+
private static String composeDependencyElementString(ArtifactRef artifact, Collection exclusions) {
StringBuilder sb = new StringBuilder();
sb.append(" \n");
diff --git a/plugin/src/test/java/org/wildfly/channelplugin/UpgradeComponentsMojoTestCase.java b/plugin/src/test/java/org/wildfly/channelplugin/UpgradeComponentsMojoTestCase.java
index d95368e..9304b98 100644
--- a/plugin/src/test/java/org/wildfly/channelplugin/UpgradeComponentsMojoTestCase.java
+++ b/plugin/src/test/java/org/wildfly/channelplugin/UpgradeComponentsMojoTestCase.java
@@ -3,6 +3,7 @@
import java.util.Properties;
import org.apache.maven.model.Model;
+import org.apache.maven.project.MavenProject;
import org.commonjava.maven.ext.common.model.Project;
import org.junit.jupiter.api.Test;
@@ -12,33 +13,72 @@ public class UpgradeComponentsMojoTestCase {
@Test
public void testFollowProperties() throws Exception {
- Properties properties = new Properties();
- properties.put("version.a", "1.0");
- properties.put("version.b", "${version.c}");
- properties.put("version.c", "${version.d}");
- properties.put("version.d", "2.0");
-
- Model model = new Model();
+ final Model model = new Model();
model.setVersion("version");
- model.setProperties(properties);
- Project project = new Project(model);
+ model.setProperties(sampleProperties());
+ final Project project = new Project(model);
assertThat(UpgradeComponentsMojo.followProperties(project, "version.a")).satisfies(pair -> {
assertThat(pair).isNotNull();
+ assertThat(pair.getLeft()).isSameAs(project);
assertThat(pair.getRight()).isEqualTo("version.a");
});
assertThat(UpgradeComponentsMojo.followProperties(project, "version.b")).satisfies(pair -> {
assertThat(pair).isNotNull();
+ assertThat(pair.getLeft()).isSameAs(project);
assertThat(pair.getRight()).isEqualTo("version.d");
});
assertThat(UpgradeComponentsMojo.followProperties(project, "version.c")).satisfies(pair -> {
assertThat(pair).isNotNull();
+ assertThat(pair.getLeft()).isSameAs(project);
assertThat(pair.getRight()).isEqualTo("version.d");
});
assertThat(UpgradeComponentsMojo.followProperties(project, "version.d")).satisfies(pair -> {
assertThat(pair).isNotNull();
+ assertThat(pair.getLeft()).isSameAs(project);
assertThat(pair.getRight()).isEqualTo("version.d");
});
}
+ @Test
+ public void testResolveExternalProperty() {
+ final Model parentModel = new Model();
+ parentModel.setVersion("version");
+ parentModel.setProperties(sampleProperties());
+ final MavenProject parentProject = new MavenProject(parentModel);
+
+ final MavenProject project = new MavenProject();
+ project.setParent(parentProject);
+
+ assertThat(UpgradeComponentsMojo.resolveExternalProperty(project, "version.a")).satisfies(pair -> {
+ assertThat(pair).isNotNull();
+ assertThat(pair.getLeft()).isEqualTo("version.a");
+ assertThat(pair.getRight()).isEqualTo("1.0");
+ });
+ assertThat(UpgradeComponentsMojo.resolveExternalProperty(project, "version.b")).satisfies(pair -> {
+ assertThat(pair).isNotNull();
+ assertThat(pair.getLeft()).isEqualTo("version.d");
+ assertThat(pair.getRight()).isEqualTo("2.0");
+ });
+ assertThat(UpgradeComponentsMojo.resolveExternalProperty(project, "version.c")).satisfies(pair -> {
+ assertThat(pair).isNotNull();
+ assertThat(pair.getLeft()).isEqualTo("version.d");
+ assertThat(pair.getRight()).isEqualTo("2.0");
+ });
+ assertThat(UpgradeComponentsMojo.resolveExternalProperty(project, "version.d")).satisfies(pair -> {
+ assertThat(pair).isNotNull();
+ assertThat(pair.getLeft()).isEqualTo("version.d");
+ assertThat(pair.getRight()).isEqualTo("2.0");
+ });
+ }
+
+ private Properties sampleProperties() {
+ Properties properties = new Properties();
+ properties.put("version.a", "1.0");
+ properties.put("version.b", "${version.c}");
+ properties.put("version.c", "${version.d}");
+ properties.put("version.d", "2.0");
+ return properties;
+ }
+
}
diff --git a/plugin/src/test/java/org/wildfly/channelplugin/manipulation/PomManipulatorTestCase.java b/plugin/src/test/java/org/wildfly/channelplugin/manipulation/PomManipulatorTestCase.java
index 3a8b308..0e510f8 100644
--- a/plugin/src/test/java/org/wildfly/channelplugin/manipulation/PomManipulatorTestCase.java
+++ b/plugin/src/test/java/org/wildfly/channelplugin/manipulation/PomManipulatorTestCase.java
@@ -12,6 +12,7 @@
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
+import org.apache.maven.model.Model;
import org.assertj.core.api.Assertions;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
@@ -33,6 +34,7 @@ public class PomManipulatorTestCase {
@BeforeEach
public void before() throws XMLStreamException, URISyntaxException, IOException {
URL pomUrl = getClass().getResource("pom.xml");
+ Assertions.assertThat(pomUrl).isNotNull();
content = PomHelper.readXmlFile(new File(pomUrl.toURI()));
XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
@@ -41,8 +43,7 @@ public void before() throws XMLStreamException, URISyntaxException, IOException
}
@Test
- public void testInsertManagedDependency()
- throws IOException, XMLStreamException, ManipulationException {
+ public void testInsertManagedDependency() throws IOException, XMLStreamException, ManipulationException {
ArtifactRef dep = new SimpleArtifactRef("org.aesh", "aesh", "2.4.0", "jar", null);
DependencyModel model = readDependencyModel();
@@ -59,13 +60,28 @@ public void testInsertManagedDependency()
});
}
- private DependencyModel readDependencyModel() throws IOException, ManipulationException {
+ @Test
+ public void testInsertProperty() throws IOException, XMLStreamException, ManipulationException {
+ Model model = readModel();
+ Assertions.assertThat(model.getProperties().contains("prop")).isFalse();
+
+ PomManipulator.injectProperty(eventReader, "prop", "value");
+
+ model = readModel();
+ Assertions.assertThat(model.getProperties().getProperty("prop")).isEqualTo("value");
+ }
+
+ private Model readModel() throws IOException, ManipulationException {
Path pomFile = Files.createTempFile("pom", "xml");
Files.write(pomFile, content.toString().getBytes());
PomIO pomIO = new PomIO();
List projects = pomIO.parseProject(pomFile.toFile());
Assertions.assertThat(projects.size()).isEqualTo(1);
- return new DependencyModel(projects.get(0).getModel());
+ return projects.get(0).getModel();
+ }
+
+ private DependencyModel readDependencyModel() throws IOException, ManipulationException {
+ return new DependencyModel(readModel());
}
}