build: move command gateway to optional profile
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package com.lingniu.ingest.historyapp;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MavenModuleProfileTest {
|
||||
|
||||
@Test
|
||||
void commandGatewayIsOutsideDefaultProductionReactor() throws Exception {
|
||||
Document pom = rootPom();
|
||||
|
||||
assertThat(defaultModules(pom))
|
||||
.doesNotContain("modules/apps/command-gateway")
|
||||
.doesNotContain("modules/protocols/protocol-jt1078");
|
||||
|
||||
assertThat(profileModules(pom, "optional-command-gateway"))
|
||||
.containsExactly(
|
||||
"modules/protocols/protocol-jt1078",
|
||||
"modules/apps/command-gateway");
|
||||
}
|
||||
|
||||
private static Document rootPom() throws Exception {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
return factory.newDocumentBuilder().parse(Files.newInputStream(repositoryRoot().resolve("pom.xml")));
|
||||
}
|
||||
|
||||
private static List<String> defaultModules(Document pom) {
|
||||
Element project = pom.getDocumentElement();
|
||||
return directModules(firstDirectChild(project, "modules"));
|
||||
}
|
||||
|
||||
private static List<String> profileModules(Document pom, String profileId) {
|
||||
Element profiles = firstDirectChild(pom.getDocumentElement(), "profiles");
|
||||
if (profiles == null) {
|
||||
return List.of();
|
||||
}
|
||||
NodeList children = profiles.getChildNodes();
|
||||
for (int i = 0; i < children.getLength(); i++) {
|
||||
if (!(children.item(i) instanceof Element profile) || !"profile".equals(profile.getTagName())) {
|
||||
continue;
|
||||
}
|
||||
Element id = firstDirectChild(profile, "id");
|
||||
if (id != null && profileId.equals(id.getTextContent().trim())) {
|
||||
return directModules(firstDirectChild(profile, "modules"));
|
||||
}
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
private static List<String> directModules(Element modules) {
|
||||
if (modules == null) {
|
||||
return List.of();
|
||||
}
|
||||
List<String> out = new ArrayList<>();
|
||||
NodeList children = modules.getChildNodes();
|
||||
for (int i = 0; i < children.getLength(); i++) {
|
||||
if (children.item(i) instanceof Element child && "module".equals(child.getTagName())) {
|
||||
out.add(child.getTextContent().trim());
|
||||
}
|
||||
}
|
||||
return List.copyOf(out);
|
||||
}
|
||||
|
||||
private static Element firstDirectChild(Element parent, String tagName) {
|
||||
NodeList children = parent.getChildNodes();
|
||||
for (int i = 0; i < children.getLength(); i++) {
|
||||
if (children.item(i) instanceof Element child && tagName.equals(child.getTagName())) {
|
||||
return child;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Path repositoryRoot() {
|
||||
Path current = Path.of(System.getProperty("user.dir")).toAbsolutePath();
|
||||
while (current != null) {
|
||||
if (Files.exists(current.resolve("pom.xml")) && Files.exists(current.resolve("modules"))) {
|
||||
return current;
|
||||
}
|
||||
current = current.getParent();
|
||||
}
|
||||
throw new IllegalStateException("repository root not found");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user