Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reloads project info from pom.xml file using ProjectBuilder #204

Merged
merged 1 commit into from
Nov 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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