
ch.sourcepond.maven.release.ReleaseInvoker Maven / Gradle / Ivy
/*Copyright (C) 2016 Roland Hauser,
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package ch.sourcepond.maven.release;
import static java.lang.String.format;
import java.io.File;
import java.io.IOException;
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.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;
import ch.sourcepond.maven.release.providers.RootProject;
import ch.sourcepond.maven.release.reactor.Reactor;
import ch.sourcepond.maven.release.reactor.ReleasableModule;
/**
* @author Roland Hauser [email protected]
*
*/
class ReleaseInvoker {
static final String DEPLOY = "deploy";
static final String SKIP_TESTS = "-DskipTests=true";
static final String LOCAL_REPO = "-Dmaven.repo.local=%s";
private final Log log;
private final RootProject project;
private final InvocationRequest request;
private final Invoker invoker;
private boolean skipTests;
private boolean debugEnabled;
private File localMavenRepo;
private List goals;
private List modulesToRelease;
private List releaseProfiles;
public ReleaseInvoker(final Log log, final RootProject project) {
this(log, project, new DefaultInvocationRequest(), new DefaultInvoker());
}
public ReleaseInvoker(final Log log, final RootProject 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 setDebugEnabled(final boolean debugEnabled) {
this.debugEnabled = debugEnabled;
}
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);
}
final void setLocalMavenRepo(final File localMavenRepo) {
this.localMavenRepo = localMavenRepo;
}
public final void runMavenBuild(final Reactor reactor) throws MojoExecutionException {
request.setInteractive(false);
request.setShowErrors(true);
request.setDebug(debugEnabled || log.isDebugEnabled());
final List goals = getGoals();
if (skipTests) {
goals.add(SKIP_TESTS);
}
if (localMavenRepo != null) {
try {
goals.add(format(LOCAL_REPO, localMavenRepo.getCanonicalFile()));
} catch (final IOException e) {
throw new MojoExecutionException("Local repository path could not be determined!", e);
}
}
request.setGoals(getGoals());
final List profiles = profilesToActivate();
request.setProfiles(profiles);
final List changedModules = new ArrayList();
final List modulesToRelease = getModulesToRelease();
for (final ReleasableModule releasableModule : reactor) {
final String modulePath = releasableModule.getRelativePathToModule();
final boolean userExplicitlyWantsThisToBeReleased = modulesToRelease.contains(modulePath);
final boolean userImplicitlyWantsThisToBeReleased = modulesToRelease.isEmpty();
if (userExplicitlyWantsThisToBeReleased
|| (userImplicitlyWantsThisToBeReleased && releasableModule.getVersion().hasChanged())) {
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 and modules %s", goals, profilesInfo, changedModules));
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) {
for (final String releaseProfile : getReleaseProfilesOrNull()) {
profiles.add(releaseProfile);
}
}
profiles.addAll(project.getActiveProfileIds());
return profiles;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy