Skip to content

Commit

Permalink
Merge pull request #204 from cidus/master
Browse files Browse the repository at this point in the history
reloads project info from pom.xml file using ProjectBuilder
  • Loading branch information
aleksandr-m authored Nov 3, 2019
2 parents f82f021 + dbcf699 commit 3486430
Showing 1 changed file with 19 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.amashchenko.maven.plugin.gitflow;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -25,13 +24,13 @@
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingResult;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.components.interactivity.Prompter;
import org.codehaus.plexus.util.StringUtils;
Expand Down Expand Up @@ -166,6 +165,10 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
/** Maven session. */
@Parameter(defaultValue = "${session}", readonly = true)
protected MavenSession mavenSession;

@Component
protected ProjectBuilder projectBuilder;

/** Default prompter. */
@Component
protected Prompter prompter;
Expand Down Expand Up @@ -226,38 +229,27 @@ protected void validateConfiguration(String... params)
* @throws MojoFailureException
*/
protected String getCurrentProjectVersion() throws MojoFailureException {
final Model model = readModel(mavenSession.getCurrentProject());
if (model.getVersion() == null) {
final MavenProject reloadedProject = reloadProject(mavenSession.getCurrentProject());
if (reloadedProject.getVersion() == null) {
throw new MojoFailureException(
"Cannot get current project version. This plugin should be executed from the parent project.");
}
return model.getVersion();
return reloadedProject.getVersion();
}

/**
* Reads model from Maven project pom.xml.
* Reloads project info from file
*
* @param project
* Maven project
* @return Maven model
* @return
* @throws MojoFailureException
*/
private Model readModel(MavenProject project) throws MojoFailureException {
private MavenProject reloadProject(MavenProject project) throws MojoFailureException {
try {
// read pom.xml
Model model;
FileReader fileReader = new FileReader(project.getFile().getAbsoluteFile());
MavenXpp3Reader mavenReader = new MavenXpp3Reader();
try {
model = mavenReader.read(fileReader);
} finally {
if (fileReader != null) {
fileReader.close();
}
}
return model;
ProjectBuildingResult result = projectBuilder.build(project.getFile(), mavenSession.getProjectBuildingRequest());
return result.getProject();
} catch (Exception e) {
throw new MojoFailureException("", e);
throw new MojoFailureException("Error re-loading project info", e);
}
}

Expand Down Expand Up @@ -295,15 +287,15 @@ protected void checkSnapshotDependencies() throws MojoFailureException {

List<MavenProject> projects = mavenSession.getProjects();
for (MavenProject project : projects) {
final Model model = readModel(project);
final MavenProject reloadedProject = reloadProject(project);

builtArtifacts.add(model.getGroupId() + ":" + model.getArtifactId() + ":" + model.getVersion());
builtArtifacts.add(reloadedProject.getGroupId() + ":" + reloadedProject.getArtifactId() + ":" + reloadedProject.getVersion());

List<Dependency> dependencies = model.getDependencies();
List<Dependency> dependencies = reloadedProject.getDependencies();
for (Dependency d : dependencies) {
String id = d.getGroupId() + ":" + d.getArtifactId() + ":" + d.getVersion();
if (!builtArtifacts.contains(id) && ArtifactUtils.isSnapshot(d.getVersion())) {
snapshots.add(model + " -> " + d);
snapshots.add(reloadedProject + " -> " + d);
}
}
}
Expand Down

0 comments on commit 3486430

Please sign in to comment.