build: scope optional module versions to profiles

This commit is contained in:
lingniu
2026-07-01 09:42:15 +08:00
parent 072fa00ea9
commit ba1ec9a012
2 changed files with 128 additions and 33 deletions

View File

@@ -93,6 +93,35 @@ class MavenModuleProfileTest {
"modules/apps/xinda-push-app");
}
@Test
void optionalModuleVersionsAreScopedToTheirProfiles() throws Exception {
Document pom = rootPom();
assertThat(rootDependencyManagementArtifacts(pom))
.doesNotContain(
"protocol-jt1078",
"protocol-jsatl12",
"vehicle-state-service",
"raw-archive-store",
"event-file-store",
"command-gateway",
"inbound-xinda-push",
"xinda-push-app");
assertThat(profileDependencyManagementArtifacts(pom, "optional-command-gateway"))
.containsExactly("protocol-jt1078", "command-gateway");
assertThat(profileDependencyManagementArtifacts(pom, "optional-attachments"))
.containsExactly("protocol-jsatl12");
assertThat(profileDependencyManagementArtifacts(pom, "optional-latest-state"))
.containsExactly("vehicle-state-service");
assertThat(profileDependencyManagementArtifacts(pom, "optional-raw-archive-store"))
.containsExactly("raw-archive-store");
assertThat(profileDependencyManagementArtifacts(pom, "optional-event-file-store"))
.containsExactly("event-file-store");
assertThat(profileDependencyManagementArtifacts(pom, "legacy-xinda"))
.containsExactly("inbound-xinda-push", "xinda-push-app");
}
@Test
void appsDirectoryContainsOnlyRealMavenModules() throws Exception {
try (Stream<Path> apps = Files.list(repositoryRoot().resolve("modules/apps"))) {
@@ -232,9 +261,29 @@ class MavenModuleProfileTest {
}
private static List<String> profileModules(Document pom, String profileId) {
Element profile = profile(pom, profileId);
if (profile == null) {
return List.of();
}
return directModules(firstDirectChild(profile, "modules"));
}
private static List<String> rootDependencyManagementArtifacts(Document pom) {
return dependencyManagementArtifacts(pom.getDocumentElement());
}
private static List<String> profileDependencyManagementArtifacts(Document pom, String profileId) {
Element profile = profile(pom, profileId);
if (profile == null) {
return List.of();
}
return dependencyManagementArtifacts(profile);
}
private static Element profile(Document pom, String profileId) {
Element profiles = firstDirectChild(pom.getDocumentElement(), "profiles");
if (profiles == null) {
return List.of();
return null;
}
NodeList children = profiles.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
@@ -243,10 +292,10 @@ class MavenModuleProfileTest {
}
Element id = firstDirectChild(profile, "id");
if (id != null && profileId.equals(id.getTextContent().trim())) {
return directModules(firstDirectChild(profile, "modules"));
return profile;
}
}
return List.of();
return null;
}
private static List<String> directModules(Element modules) {
@@ -263,6 +312,32 @@ class MavenModuleProfileTest {
return List.copyOf(out);
}
private static List<String> dependencyManagementArtifacts(Element owner) {
Element dependencyManagement = firstDirectChild(owner, "dependencyManagement");
if (dependencyManagement == null) {
return List.of();
}
Element dependencies = firstDirectChild(dependencyManagement, "dependencies");
if (dependencies == null) {
return List.of();
}
List<String> artifacts = new ArrayList<>();
NodeList children = dependencies.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
if (!(children.item(i) instanceof Element dependency) || !"dependency".equals(dependency.getTagName())) {
continue;
}
Element groupId = firstDirectChild(dependency, "groupId");
Element artifactId = firstDirectChild(dependency, "artifactId");
if (groupId != null
&& artifactId != null
&& "com.lingniu.ingest".equals(groupId.getTextContent().trim())) {
artifacts.add(artifactId.getTextContent().trim());
}
}
return List.copyOf(artifacts);
}
private static boolean dependencyOptional(Document pom, String groupId, String artifactId) {
Element dependencies = firstDirectChild(pom.getDocumentElement(), "dependencies");
if (dependencies == null) {