use of com.qoomon.maven.GAV in project maven-git-versioning-extension by qoomon.
the class VersioningPomReplacementMojo method execute.
@Override
public synchronized void execute() throws MojoExecutionException, MojoFailureException {
try {
MavenProject currentProject = mavenSession.getCurrentProject();
GAV gav = GAV.of(currentProject);
logger.debug(gav + "remove plugin");
currentProject.getOriginalModel().getBuild().removePlugin(asPlugin());
temporaryOverridePomFileFromModel(currentProject);
} catch (Exception e) {
throw new MojoExecutionException("Git Versioning Pom Replacement Mojo", e);
}
}
use of com.qoomon.maven.GAV in project maven-git-versioning-extension by qoomon.
the class VersioningModelProcessor method addBuildPlugin.
private void addBuildPlugin(Model model) {
GAV projectGav = GAV.of(model);
logger.debug(projectGav + " temporary add build plugin");
if (model.getBuild() == null) {
model.setBuild(new Build());
}
Plugin projectPlugin = VersioningPomReplacementMojo.asPlugin();
PluginExecution execution = new PluginExecution();
execution.setId(VersioningPomReplacementMojo.GOAL);
execution.getGoals().add(VersioningPomReplacementMojo.GOAL);
projectPlugin.getExecutions().add(execution);
model.getBuild().getPlugins().add(projectPlugin);
}
use of com.qoomon.maven.GAV in project maven-git-versioning-extension by qoomon.
the class VersioningModelProcessor method provisionModel.
private Model provisionModel(Model model, Map<String, ?> options) throws IOException {
try {
if (!initialized) {
initialize();
initialized = true;
}
if (disabled) {
return model;
}
// ---------------- provisioning ----------------
Source pomSource = (Source) options.get(ModelProcessor.SOURCE);
File pomFile = new File(pomSource != null ? pomSource.getLocation() : "");
if (!isProjectPom(pomFile)) {
// skip unrelated models
logger.debug("skip unrelated model - source" + pomFile);
return model;
}
GAV projectGav = GAV.of(model);
// deduce version
ProjectVersion projectVersion = deduceProjectVersion(projectGav, pomFile.getParentFile());
// add properties
if (projectVersion.getBranch() != null) {
model.addProperty("project.branch", projectVersion.getBranch());
}
if (projectVersion.getTag() != null) {
model.addProperty("project.tag", projectVersion.getTag());
}
model.addProperty("project.commit", projectVersion.getCommit());
// update parent version
if (model.getParent() != null) {
File parentPomFile = new File(pomFile.getParentFile(), model.getParent().getRelativePath());
GAV parentGav = GAV.of(model.getParent());
if (parentPomFile.exists() && isProjectPom(parentPomFile)) {
// check if parent pom file match project parent
Model parentModel = ModelUtil.readModel(parentPomFile);
GAV parentProjectGav = GAV.of(parentModel);
if (parentProjectGav.equals(parentGav)) {
ProjectVersion parentProjectVersion = deduceProjectVersion(parentGav, parentPomFile.getParentFile());
logger.debug(projectGav + " adjust project parent version to " + parentProjectVersion);
model.getParent().setVersion(parentProjectVersion.getVersion());
}
}
}
// update project version
if (model.getParent() == null || !model.getParent().getVersion().equals(projectVersion.getVersion())) {
logger.debug(projectGav + " adjust project version to " + projectVersion);
model.setVersion(projectVersion.getVersion());
}
// add plugin
// has to be removed from model by plugin itself
addBuildPlugin(model);
return model;
} catch (Exception e) {
throw new IOException("Branch Versioning Model Processor", e);
}
}
use of com.qoomon.maven.GAV 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