com.perforce.maven.mojo.P4Mojo Maven / Gradle / Ivy
The newest version!
package com.perforce.maven.mojo;
import java.io.File;
import java.util.List;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmResult;
import org.apache.maven.scm.command.status.StatusScmResult;
import org.apache.maven.scm.log.DefaultLog;
import org.apache.maven.scm.log.ScmLogDispatcher;
import org.apache.maven.scm.log.ScmLogger;
import org.apache.maven.scm.manager.ScmManager;
import org.apache.maven.scm.provider.ScmProvider;
import org.apache.maven.scm.provider.ScmProviderRepository;
import org.apache.maven.scm.repository.ScmRepository;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import com.perforce.p4java.core.IChangelist;
/**
* Super class for Perforce Mojos.
*
* @requiresProject
*/
public abstract class P4Mojo extends AbstractMojo {
/**
* @parameter expression="${project.scm.developerConnection}"
* @readonly
*/
protected String urlScm;
/**
* The username that is used when connecting to the SCM system.
*
* @parameter expression="${username}"
*/
protected String username;
/**
* The password that is used when connecting to the SCM system.
*
* @parameter expression="${password}"
*/
protected String password;
/**
* The includes that is used to include files with that pattern.
*
* @parameter expression="${includes}"
*/
protected String includes;
/**
* The excludes that is used to include files with that pattern.
*
* @parameter expression="${excludes}"
*/
protected String excludes;
/**
* Local directory to be used to issue SCM actions
*
* @parameter expression="${basedir}"
*/
protected File scmDirectory;
/**
* @component
*/
protected ScmManager scmManager;
/**
* The maven project.
*
* @parameter expression="${project}"
* @readonly
*/
protected MavenProject project;
/**
* Contains the full list of projects in the reactor.
*
* @parameter expression="${reactorProjects}"
* @readonly
* @since 1.0-beta-3
*/
protected List reactorProjects;
/**
* @parameter expression="${session}"
* @readonly
* @required
*/
protected MavenSession session;
protected ScmLogDispatcher logger;
public void execute() throws MojoExecutionException, MojoFailureException {
// Get the username and password from the maven-scm-plugin configuration
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
Xpp3Dom configuration = getScmPluginConfiguration();
if (configuration != null) {
if (StringUtils.isEmpty(username)) {
if (configuration.getChild("username") != null) {
username = configuration.getChild("username")
.getValue();
}
}
if (StringUtils.isEmpty(password)) {
if (configuration.getChild("password") != null) {
password = configuration.getChild("password")
.getValue();
}
}
}
}
// Get includes and excludes file patterns
if (StringUtils.isEmpty(includes) || StringUtils.isEmpty(excludes)) {
Xpp3Dom configuration = getScmPluginConfiguration();
if (configuration != null) {
if (StringUtils.isEmpty(includes)) {
if (configuration.getChild("includes") != null) {
includes = configuration.getChild("includes")
.getValue();
}
}
if (StringUtils.isEmpty(excludes)) {
if (configuration.getChild("excludes") != null) {
excludes = configuration.getChild("excludes")
.getValue();
}
}
}
}
executeP4Command();
}
public abstract ScmResult executeP4Command() throws MojoExecutionException;
public List getStatus() throws ScmException {
ScmRepository repository = getScmRepository();
if (repository == null) {
return null;
}
ScmProvider scmProvider = scmManager
.getProviderByRepository(repository);
StatusScmResult result = scmProvider.status(repository, new ScmFileSet(
scmDirectory));
checkResult(result);
return result.getChangedFiles();
}
/**
* @return
* @todo normally this would be handled in AbstractScmProvider
*/
protected ScmLogger getLogger() {
if (logger == null) {
logger = new ScmLogDispatcher();
ScmLogger log = new DefaultLog();
logger.addListener(log);
}
return logger;
}
protected ScmRepository getScmRepository() throws ScmException {
ScmRepository repository = null;
if (StringUtils.isEmpty(urlScm)) {
getLog().error("You need to define a developerConnectionUrl parameter.");
return null;
}
try {
repository = scmManager.makeScmRepository(urlScm);
ScmProviderRepository scmRepo = repository.getProviderRepository();
if (!StringUtils.isEmpty(username)) {
scmRepo.setUser(username);
}
if (!StringUtils.isEmpty(password)) {
scmRepo.setPassword(password);
}
} catch(Exception e) {
getLog().error("Problem connecting to the repository with the specified developerConnectionUrl parameter.", e);
}
return repository;
}
protected void checkResult(ScmResult result) throws ScmException {
if (!result.isSuccess()) {
getLog().error("Provider message: " + result.getProviderMessage());
getLog().error("Command output: " + result.getCommandOutput());
throw new ScmException("Error!");
}
}
protected Xpp3Dom getScmPluginConfiguration() {
Xpp3Dom configuration = null;
List plugins = project.getBuildPlugins();
for (Plugin plugin : plugins) {
if (plugin.getGroupId().equals("org.apache.maven.plugins")) {
if (plugin.getArtifactId().equals("maven-scm-plugin")) {
configuration = (Xpp3Dom) plugin.getConfiguration();
}
}
}
return configuration;
}
/**
* Parse the changelist string to a changelist number. Convert the "default"
* changelist string to the default changelist number. If it is negative
* return unknown changelist. Otherwise, return the converted changelist
* number.
*
* @param changelist
* the changelist
* @return the int
*/
public static int parseChangelist(String changelist) {
if (StringUtils.isNotEmpty(changelist)) {
if (changelist.trim().equalsIgnoreCase("default")) {
return IChangelist.DEFAULT;
}
try {
int changelistId = Integer.parseInt(changelist);
if (changelistId < 0) {
return IChangelist.UNKNOWN;
}
return changelistId;
} catch (NumberFormatException e) {
// Suppress error
}
}
return IChangelist.UNKNOWN;
}
public void setScmManager(ScmManager scmManager) {
this.scmManager = scmManager;
}
public void setUrlScm(String urlScm) {
this.urlScm = urlScm;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setIncludes(String includes) {
this.includes = includes;
}
public void setExcludes(String excludes) {
this.excludes = excludes;
}
/**
* Checks if is empty.
*
* @param value
* the value
* @return true, if is empty
*/
public static boolean isEmpty(String value) {
if (value == null || value.trim().length() == 0) {
return true;
}
return false;
}
}