Skip to content

Commit

Permalink
feat: relocate git versioned pom file directly to build directory
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon authored and Bengt Brodersen committed Mar 29, 2019
1 parent 760557a commit a4dcd8c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -4,5 +4,7 @@ target/
.project
.checkstyle
.idea/
*.iml
.m2/
.*-pom.xml

Expand Up @@ -8,7 +8,7 @@


@JacksonXmlRootElement(localName = "gitVersioning")
public class GitVersioningExtensionConfiguration {
public class Configuration {

public CommitVersionDescription commit;

Expand Down
Expand Up @@ -8,13 +8,15 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.*;
import org.apache.maven.model.building.DefaultModelProcessor;
import org.apache.maven.model.building.ModelProcessor;
import org.apache.maven.session.scope.internal.SessionScope;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.logging.Logger;

import javax.inject.Inject;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -23,16 +25,16 @@
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static me.qoomon.UncheckedExceptions.unchecked;
import static me.qoomon.maven.gitversioning.GitVersioningPomReplacementMojo.GIT_VERSIONING_POM_PATH;
import static me.qoomon.maven.gitversioning.VersioningMojo.GIT_VERSIONING_POM_NAME;
import static me.qoomon.maven.gitversioning.MavenUtil.isProjectPom;
import static me.qoomon.maven.gitversioning.MavenUtil.readModel;


/**
* Replacement for {@link ModelProcessor} to adapt versions.
* Replacement for {@link org.apache.maven.model.building.ModelProcessor} to adapt versions.
*/
@Component(role = ModelProcessor.class)
public class GitVersioningModelProcessor extends DefaultModelProcessor {
@Component(role = org.apache.maven.model.building.ModelProcessor.class)
public class ModelProcessor extends DefaultModelProcessor {

private final Logger logger;

Expand All @@ -49,7 +51,7 @@ public class GitVersioningModelProcessor extends DefaultModelProcessor {


@Inject
public GitVersioningModelProcessor(final Logger logger, final SessionScope sessionScope) {
public ModelProcessor(final Logger logger, final SessionScope sessionScope) {
this.logger = logger;
this.sessionScope = sessionScope;
}
Expand Down Expand Up @@ -92,7 +94,7 @@ private Model processModel(Model projectModel, Map<String, ?> options) throws IO
return projectModel;
}

final Source pomSource = (Source) options.get(ModelProcessor.SOURCE);
final Source pomSource = (Source) options.get(org.apache.maven.model.building.ModelProcessor.SOURCE);
if (pomSource != null) {
projectModel.setPomFile(new File(pomSource.getLocation()));
}
Expand All @@ -109,7 +111,7 @@ private Model processModel(Model projectModel) {
return projectModel;
}

if (projectModel.getPomFile().getName().equals(GIT_VERSIONING_POM_PATH)) {
if (projectModel.getPomFile().getName().equals(GIT_VERSIONING_POM_NAME)) {
logger.debug("skip - git versioned pom - " + projectModel.getPomFile());
return projectModel;
}
Expand All @@ -126,7 +128,7 @@ private Model processModel(Model projectModel) {

Model virtualProjectModel = this.virtualProjectModelCache.get(projectModel.getArtifactId());
if (virtualProjectModel == null) {
logger.info(projectGav.getArtifactId() + " - set project version to" + gitVersionDetails.getVersion()
logger.info(projectGav.getArtifactId() + " - set project version to " + gitVersionDetails.getVersion()
+ " (" + gitVersionDetails.getCommitRefType() + ":" + gitVersionDetails.getCommitRefName() + ")");

virtualProjectModel = projectModel.clone();
Expand Down Expand Up @@ -181,7 +183,7 @@ private Model processModel(Model projectModel) {
private GitVersionDetails getGitVersionDetails(Model projectModel) {
File mvnDir = findMvnDir(projectModel);
File configFile = new File(mvnDir, BuildProperties.projectArtifactId() + ".xml");
GitVersioningExtensionConfiguration config = loadConfig(configFile);
Configuration config = loadConfig(configFile);

GitRepoSituation repoSituation = GitUtil.situation(projectModel.getPomFile());
String providedBranch = getOption("git.branch");
Expand Down Expand Up @@ -243,11 +245,11 @@ private File findMvnDir(Model projectModel) {
private void addBuildPlugin(Model model) {
logger.debug(model.getArtifactId() + " temporary add build plugin");

Plugin projectPlugin = GitVersioningPomReplacementMojo.asPlugin();
Plugin projectPlugin = VersioningMojo.asPlugin();

PluginExecution execution = new PluginExecution();
execution.setId(GitVersioningPomReplacementMojo.GOAL);
execution.getGoals().add(GitVersioningPomReplacementMojo.GOAL);
execution.setId(VersioningMojo.GOAL);
execution.getGoals().add(VersioningMojo.GOAL);
projectPlugin.getExecutions().add(execution);

if (model.getBuild() == null) {
Expand All @@ -256,6 +258,7 @@ private void addBuildPlugin(Model model) {
model.getBuild().getPlugins().add(projectPlugin);
}


private String getOption(final String name) {
String value = mavenSession.getUserProperties().getProperty(name);
if (value == null) {
Expand All @@ -264,11 +267,12 @@ private String getOption(final String name) {
return value;
}

private GitVersioningExtensionConfiguration loadConfig(File configFile) {

private Configuration loadConfig(File configFile) {
if (!configFile.exists()) {
return new GitVersioningExtensionConfiguration();
return new Configuration();
}
logger.debug("load config from " + configFile);
return unchecked(() -> new XmlMapper().readValue(configFile, GitVersioningExtensionConfiguration.class));
return unchecked(() -> new XmlMapper().readValue(configFile, Configuration.class));
}
}
@@ -1,6 +1,5 @@
package me.qoomon.maven.gitversioning;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -21,44 +20,42 @@
* <p>
* !!! DO NOT ADD THIS PLUGIN MANUALLY TO POM !!!
* <p>
* utilized by {@link GitVersioningModelProcessor}
* utilized by {@link ModelProcessor}
*/
@Mojo(name = GitVersioningPomReplacementMojo.GOAL,
defaultPhase = LifecyclePhase.PREPARE_PACKAGE,
instantiationStrategy = InstantiationStrategy.SINGLETON,

@Mojo(name = VersioningMojo.GOAL,
defaultPhase = LifecyclePhase.PROCESS_RESOURCES,
threadSafe = true)
public class GitVersioningPomReplacementMojo extends AbstractMojo {
public class VersioningMojo extends AbstractMojo {

static final String GOAL = "pom-surrogate";
static final String GIT_VERSIONING_POM_PATH = "git-versioning/pom.xml";
static final String GOAL = "git-versioning";
static final String GIT_VERSIONING_POM_NAME = ".git-versioned-pom.xml";

@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject currentProject;

@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession mavenSession;


@Override
public synchronized void execute() throws MojoExecutionException {
try {
getLog().info("Generating git versioned POM of project " + GAV.of(currentProject.getOriginalModel()) + "...");

getLog().debug(currentProject.getModel().getArtifactId() + "remove this plugin from model");
currentProject.getOriginalModel().getBuild().removePlugin(asPlugin());
currentProject.getOriginalModel().getBuild().removePlugin(VersioningMojo.asPlugin());

// read model from pom file because we dont want to apply any changes mady by plugins, except the version
Model pomFileModel = readModel(currentProject.getFile());
if (pomFileModel.getVersion() != null) {
pomFileModel.setVersion(currentProject.getVersion());
}

if (pomFileModel.getParent() != null && isProjectPom(currentProject.getParent().getFile())) {
pomFileModel.getParent().setVersion(currentProject.getVersion());
}

File gitVersionedPomFile = new File(currentProject.getBuild().getDirectory(), GIT_VERSIONING_POM_PATH);
// write git-versioned pom file
File gitVersionedPomFile = new File(currentProject.getBuild().getDirectory(), GIT_VERSIONING_POM_NAME);
Files.createDirectories(gitVersionedPomFile.getParentFile().toPath());
writeModel(gitVersionedPomFile, pomFileModel);
// update project pom file
currentProject.setPomFile(gitVersionedPomFile);
} catch (Exception e) {
throw new MojoExecutionException("Git Versioning Pom Replacement Mojo", e);
Expand Down
Expand Up @@ -8,7 +8,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class GitVersioningExtensionConfigurationTest {
class ConfigurationTest {

@Test
void xmlUnmarshaller_empty() throws IOException {
Expand All @@ -17,8 +17,8 @@ void xmlUnmarshaller_empty() throws IOException {
"</gitVersioning>\n";

// when
GitVersioningExtensionConfiguration config = new XmlMapper()
.readValue(configXml, GitVersioningExtensionConfiguration.class);
Configuration config = new XmlMapper()
.readValue(configXml, Configuration.class);

// then
assertAll(
Expand All @@ -39,8 +39,8 @@ void xmlUnmarshaller_commitConfigOnly() throws IOException {
"</gitVersioning>\n";

// when
GitVersioningExtensionConfiguration config = new XmlMapper()
.readValue(configXml, GitVersioningExtensionConfiguration.class);
Configuration config = new XmlMapper()
.readValue(configXml, Configuration.class);

// then
assertAll(
Expand All @@ -67,8 +67,8 @@ void xmlUnmarshaller_branchConfigsOnly() throws IOException {
"</gitVersioning>\n";

// when
GitVersioningExtensionConfiguration config = new XmlMapper()
.readValue(configXml, GitVersioningExtensionConfiguration.class);
Configuration config = new XmlMapper()
.readValue(configXml, Configuration.class);

// then
assertAll(
Expand Down Expand Up @@ -105,8 +105,8 @@ void xmlUnmarshaller_tagsConfigsOnly() throws IOException {
"</gitVersioning>\n";

// when
GitVersioningExtensionConfiguration config = new XmlMapper()
.readValue(configXml, GitVersioningExtensionConfiguration.class);
Configuration config = new XmlMapper()
.readValue(configXml, Configuration.class);

// then
assertAll(
Expand Down Expand Up @@ -154,8 +154,8 @@ void xmlUnmarshaller() throws IOException {
"</gitVersioning>\n";

// when
GitVersioningExtensionConfiguration config = new XmlMapper()
.readValue(configXml, GitVersioningExtensionConfiguration.class);
Configuration config = new XmlMapper()
.readValue(configXml, Configuration.class);

// then
assertAll(
Expand Down

0 comments on commit a4dcd8c

Please sign in to comment.