Search in sources :

Example 1 with ModelParseException

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;
    }
}
Also used : ModelParseException(org.apache.maven.model.io.ModelParseException) PluginExecution(org.apache.maven.model.PluginExecution) java.util(java.util) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) VersionFormatDescription(com.qoomon.maven.extension.gitversioning.config.model.VersionFormatDescription) Component(org.codehaus.plexus.component.annotations.Component) Build(org.apache.maven.model.Build) Inject(javax.inject.Inject) Matcher(java.util.regex.Matcher) ModelProcessor(org.apache.maven.model.building.ModelProcessor) ModelUtil(com.qoomon.maven.ModelUtil) Source(org.apache.maven.building.Source) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) DefaultModelProcessor(org.apache.maven.model.building.DefaultModelProcessor) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) MavenSession(org.apache.maven.execution.MavenSession) Constants(org.eclipse.jgit.lib.Constants) IOException(java.io.IOException) Reader(java.io.Reader) SessionScopeUtil(com.qoomon.maven.extension.gitversioning.SessionScopeUtil) Collectors(java.util.stream.Collectors) File(java.io.File) ObjectId(org.eclipse.jgit.lib.ObjectId) GAV(com.qoomon.maven.GAV) Plugin(org.apache.maven.model.Plugin) SessionScope(org.apache.maven.session.scope.internal.SessionScope) Stream(java.util.stream.Stream) BuildProperties(com.qoomon.maven.BuildProperties) VersioningConfigurationProvider(com.qoomon.maven.extension.gitversioning.config.VersioningConfigurationProvider) Logger(org.codehaus.plexus.logging.Logger) Pattern(java.util.regex.Pattern) VersioningConfiguration(com.qoomon.maven.extension.gitversioning.config.VersioningConfiguration) Repository(org.eclipse.jgit.lib.Repository) Model(org.apache.maven.model.Model) InputStream(java.io.InputStream) Repository(org.eclipse.jgit.lib.Repository) ModelParseException(org.apache.maven.model.io.ModelParseException) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) VersionFormatDescription(com.qoomon.maven.extension.gitversioning.config.model.VersionFormatDescription) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Aggregations

BuildProperties (com.qoomon.maven.BuildProperties)1 GAV (com.qoomon.maven.GAV)1 ModelUtil (com.qoomon.maven.ModelUtil)1 SessionScopeUtil (com.qoomon.maven.extension.gitversioning.SessionScopeUtil)1 VersioningConfiguration (com.qoomon.maven.extension.gitversioning.config.VersioningConfiguration)1 VersioningConfigurationProvider (com.qoomon.maven.extension.gitversioning.config.VersioningConfigurationProvider)1 VersionFormatDescription (com.qoomon.maven.extension.gitversioning.config.model.VersionFormatDescription)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 java.util (java.util)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Inject (javax.inject.Inject)1 StrSubstitutor (org.apache.commons.lang3.text.StrSubstitutor)1 DefaultArtifactVersion (org.apache.maven.artifact.versioning.DefaultArtifactVersion)1 Source (org.apache.maven.building.Source)1