Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
Expand Down Expand Up @@ -404,22 +403,26 @@ public static class Profile {
}

public @Nullable String getGroupId() {
return groupId == null && parent != null ? parent.getGroupId() : groupId;
if (!StringUtils.isBlank(groupId)) {
return groupId;
}
if (parent != null && !StringUtils.isBlank(parent.getGroupId())) {
return parent.getGroupId();
}
return null;
}

public @Nullable String getVersion() {
if (version == null) {
if (currentVersion == null) {
if (parent == null) {
return null;
} else {
return parent.getVersion();
}
} else {
return currentVersion;
}
if (!StringUtils.isBlank(version)) {
return version;
}
return version;
if (!StringUtils.isBlank(currentVersion)) {
return currentVersion;
}
if (parent != null && !StringUtils.isBlank(parent.getVersion())) {
return parent.getVersion();
}
return null;
}


Expand All @@ -428,15 +431,26 @@ public Pom toPom(@Nullable Path inputPath, @Nullable MavenRepository repo) {
getParent().getGroupId(), getParent().getArtifactId(),
getParent().getVersion()), getParent().getRelativePath());

String resolvedGroupId = getGroupId();
String resolvedVersion = getVersion();
if (resolvedGroupId == null || resolvedVersion == null) {
throw new MavenParsingException(
"POM is missing a required coordinate:" +
(resolvedGroupId == null ? " groupId" : "") +
(resolvedVersion == null ? " version" : "") +
" for " + new GroupArtifactVersion(resolvedGroupId, artifactId, resolvedVersion) +
(repo == null ? "" : " (from " + repo.getUri() + ")"));
}

Pom.PomBuilder builder = Pom.builder()
.sourcePath(inputPath)
.repository(repo)
.parent(parent)
.gav(new ResolvedGroupArtifactVersion(
repo == null ? null : repo.getUri(),
Objects.requireNonNull(getGroupId()),
resolvedGroupId,
artifactId,
Objects.requireNonNull(getVersion()),
resolvedVersion,
null))
.name(name)
.obsoletePomVersion(pomVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,4 +610,108 @@ void deserializePluginConfiguration() throws Exception {
assertThat(plugin.getConfigurationList("grandparent.parent.child.stringList", String.class)).hasSize(4)
.contains("f", "r", "e", "d");
}

@Test
void missingGroupIdThrowsParsingException() {
RawPom pom = RawPom.parse(
//language=xml
new ByteArrayInputStream("""
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
""".getBytes()),
null
);

assertThatThrownBy(() -> pom.toPom(null, null))
.isInstanceOf(MavenParsingException.class)
.hasMessageContaining("missing a required coordinate")
.hasMessageContaining("groupId")
.hasMessageContaining("my-app");
}

@Test
void missingVersionThrowsParsingException() {
RawPom pom = RawPom.parse(
//language=xml
new ByteArrayInputStream("""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
</project>
""".getBytes()),
null
);

assertThatThrownBy(() -> pom.toPom(null, null))
.isInstanceOf(MavenParsingException.class)
.hasMessageContaining("missing a required coordinate")
.hasMessageContaining("version");
}

@Test
void emptyGroupIdElementThrowsParsingException() {
RawPom pom = RawPom.parse(
//language=xml
new ByteArrayInputStream("""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId/>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
""".getBytes()),
null
);

assertThatThrownBy(() -> pom.toPom(null, null))
.isInstanceOf(MavenParsingException.class)
.hasMessageContaining("groupId");
}

@Test
void whitespaceVersionElementThrowsParsingException() {
RawPom pom = RawPom.parse(
//language=xml
new ByteArrayInputStream("""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version> </version>
</project>
""".getBytes()),
null
);

assertThatThrownBy(() -> pom.toPom(null, null))
.isInstanceOf(MavenParsingException.class)
.hasMessageContaining("version");
}

@Test
void groupIdInheritedFromParentStillResolves() {
RawPom pom = RawPom.parse(
//language=xml
new ByteArrayInputStream("""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-parent</artifactId>
<version>1</version>
</parent>
<artifactId>my-app</artifactId>
</project>
""".getBytes()),
null
);

Pom resolved = pom.toPom(null, null);
assertThat(resolved.getGroupId()).isEqualTo("com.mycompany.app");
assertThat(resolved.getVersion()).isEqualTo("1");
}
}