use of org.apache.maven.model.io.ModelParseException in project maven-git-versioning-extension by qoomon.
the class VersioningModelProcessor method deduceProjectVersion.
private ProjectVersion deduceProjectVersion(GAV gav, File gitDir) throws IOException {
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder().findGitDir(gitDir);
logger.debug(gav + "git directory " + repositoryBuilder.getGitDir());
try (Repository repository = repositoryBuilder.build()) {
final String headCommit = getHeadCommit(repository);
String projectBranchName = null;
String projectTagName = null;
VersionFormatDescription projectVersionFormatDescription = null;
Map<String, String> projectVersionDataMap = buildCommonVersionDataMap(headCommit, gav);
if (!configuration.getTagVersionDescriptions().isEmpty()) {
final List<String> headTags = getHeadTags(repository);
if (!headTags.isEmpty()) {
for (VersionFormatDescription versionFormatDescription : configuration.getTagVersionDescriptions()) {
String tagName = headTags.stream().sequential().filter(tag -> tag.matches(versionFormatDescription.pattern)).sorted((versionLeft, versionRight) -> {
DefaultArtifactVersion tagVersionLeft = new DefaultArtifactVersion(removePrefix(versionLeft, versionFormatDescription.prefix));
DefaultArtifactVersion tagVersionRight = new DefaultArtifactVersion(removePrefix(versionRight, versionFormatDescription.prefix));
// -1 revert sorting, latest version first
return tagVersionLeft.compareTo(tagVersionRight) * -1;
}).findFirst().orElse(null);
if (tagName != null) {
projectTagName = tagName;
projectVersionFormatDescription = versionFormatDescription;
projectVersionDataMap.put("tag", removePrefix(projectTagName, projectVersionFormatDescription.prefix));
projectVersionDataMap.putAll(getRegexGroupValueMap(projectVersionFormatDescription.pattern, projectTagName));
break;
}
}
}
}
if (projectTagName == null) {
final String branchName = getHeadBranch(repository).orElseThrow(() -> new ModelParseException(gitDir + ": No Branch Name provided in Detached HEAD state. See documentation.", 0, 0));
projectBranchName = branchName;
projectVersionFormatDescription = configuration.getBranchVersionDescriptions().stream().filter(versionFormatDescription -> branchName.matches(versionFormatDescription.pattern)).findFirst().orElseThrow(() -> new ModelParseException(gitDir + ": No version format for branch '" + branchName + "' found.", 0, 0));
projectVersionDataMap.put("branch", removePrefix(projectBranchName, projectVersionFormatDescription.prefix));
projectVersionDataMap.putAll(getRegexGroupValueMap(projectVersionFormatDescription.pattern, projectBranchName));
}
String version = StrSubstitutor.replace(projectVersionFormatDescription.versionFormat, projectVersionDataMap);
ProjectVersion projectVersion = new ProjectVersion(escapeVersion(version), headCommit, projectBranchName, projectTagName);
logger.info(gav.getArtifactId() + ":" + gav.getVersion() + (projectVersion.getTag() != null ? " - tag: " + projectVersion.getTag() : "") + (projectVersion.getTag() != null ? " - branch: " + projectVersion.getBranch() : "") + " -> version: " + projectVersion.getVersion());
return projectVersion;
}
}
Aggregations