com.github.danielflower.mavenplugins.release.ReleaseInvoker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multi-module-maven-release-plugin Show documentation
Show all versions of multi-module-maven-release-plugin Show documentation
A maven release plugin built for multi-maven-module git repositories allowing continuous deployment
package com.github.danielflower.mavenplugins.release;
import static java.lang.String.format;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.shared.invoker.MavenInvocationException;
/**
* @author Roland Hauser [email protected]
*
*/
class ReleaseInvoker {
static final String DEPLOY = "deploy";
static final String SKIP_TESTS = "-DskipTests=true";
private final Log log;
private final MavenProject project;
private final InvocationRequest request;
private final Invoker invoker;
private boolean skipTests;
private List goals;
private List modulesToRelease;
private List releaseProfiles;
private String arguments;
public ReleaseInvoker(final Log log, final MavenProject project) {
this(log, project, new DefaultInvocationRequest(), new DefaultInvoker());
}
public ReleaseInvoker(final Log log, final MavenProject project, final InvocationRequest request,
final Invoker invoker) {
this.log = log;
this.project = project;
this.request = request;
this.invoker = invoker;
}
private List getGoals() {
if (goals == null || goals.isEmpty()) {
goals = new ArrayList<>();
goals.add(DEPLOY);
}
return goals;
}
private List getModulesToRelease() {
return modulesToRelease == null ? Collections. emptyList() : modulesToRelease;
}
private List getReleaseProfilesOrNull() {
return releaseProfiles;
}
final void setGoals(final List goalsOrNull) {
goals = goalsOrNull;
}
final void setModulesToRelease(final List modulesToReleaseOrNull) {
modulesToRelease = modulesToReleaseOrNull;
}
final void setReleaseProfiles(final List releaseProfilesOrNull) {
releaseProfiles = releaseProfilesOrNull;
}
final void setSkipTests(final boolean skipTests) {
this.skipTests = skipTests;
}
final void setGlobalSettings(final File globalSettings) {
request.setGlobalSettingsFile(globalSettings);
}
final void setUserSettings(final File userSettings) {
request.setUserSettingsFile(userSettings);
}
public final void runMavenBuild(final Reactor reactor) throws MojoExecutionException {
request.setBatchMode(true);
request.setShowErrors(true);
request.setDebug(log.isDebugEnabled());
final List goals = getGoals();
if (arguments != null) {
goals.add(arguments);
}
if (skipTests) {
goals.add(SKIP_TESTS);
}
request.setGoals(getGoals());
final List profiles = profilesToActivate();
request.setProfiles(profiles);
request.setAlsoMake(true);
final List changedModules = new ArrayList();
final List modulesToRelease = getModulesToRelease();
for (final ReleasableModule releasableModule : reactor.getModulesInBuildOrder()) {
final String modulePath = releasableModule.getRelativePathToModule();
final boolean userExplicitlyWantsThisToBeReleased = modulesToRelease.contains(modulePath);
final boolean userImplicitlyWantsThisToBeReleased = modulesToRelease.isEmpty();
if (userExplicitlyWantsThisToBeReleased
|| (userImplicitlyWantsThisToBeReleased && releasableModule.willBeReleased())) {
changedModules.add(modulePath);
}
}
request.setProjects(changedModules);
final String profilesInfo = profiles.isEmpty() ? "no profiles activated" : "profiles " + profiles;
log.info(format("About to run mvn %s with %s", goals, profilesInfo));
try {
final InvocationResult result = invoker.execute(request);
if (result.getExitCode() != 0) {
throw new MojoExecutionException("Maven execution returned code " + result.getExitCode());
}
} catch (final MavenInvocationException e) {
throw new MojoExecutionException("Failed to build artifact", e);
}
}
private List profilesToActivate() {
final List profiles = new ArrayList();
if (getReleaseProfilesOrNull() != null) {
profiles.addAll(getReleaseProfilesOrNull());
}
for (final Object activatedProfile : project.getActiveProfiles()) {
profiles.add(((org.apache.maven.model.Profile) activatedProfile).getId());
}
return profiles;
}
public String getArguments() {
return arguments;
}
public void setArguments(String arguments) {
this.arguments = arguments;
}
}