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.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; 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.ScmProviderRepositoryWithHost; import org.apache.maven.scm.repository.ScmRepository; import org.apache.maven.settings.Server; import org.apache.maven.settings.Settings; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException; import com.perforce.p4java.core.IChangelist; /** * Super class for Perforce Mojos. * * @requiresProject */ public abstract class P4Mojo extends AbstractMojo { /** * @parameter expression="${project.scm.developerConnection}" * @readonly */ @Parameter( defaultValue = "${project.scm.developerConnection}" ) protected String urlScm; /** * The username that is used when connecting to the SCM system. * */ @Parameter( property = "username" ) protected String username; /** * The password that is used when connecting to the SCM system. * */ @Parameter( property = "password" ) protected String password; /** * The includes that is used to include files with that pattern. * */ @Parameter( property = "includes" ) protected String includes; /** * The excludes that is used to exclude files with that pattern. * */ @Parameter( property = "excludes" ) protected String excludes; /** * Local directory to be used to issue SCM actions * */ @Parameter( defaultValue = "${project.basedir}" ) protected File scmDirectory; @Component protected ScmManager scmManager; /** * The maven project. * */ @Parameter( defaultValue = "${project}", readonly = true ) protected MavenProject project; /** * When this plugin requires Maven 3.0 as minimum, this component can be removed and o.a.m.s.c.SettingsDecrypter be * used instead. */ @Component( hint = "mng-4384" ) private SecDispatcher secDispatcher; @Parameter( defaultValue = "${settings}", readonly = true ) private Settings settings; 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); ScmProviderRepositoryWithHost scmRepo = (ScmProviderRepositoryWithHost)repository.getProviderRepository(); loadInfosFromSettings(scmRepo); 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<Plugin> 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; } private String decrypt(String str, String server) { try { return secDispatcher.decrypt(str); } catch (SecDispatcherException e) { getLog().warn( "Failed to decrypt password/passphrase for server " + server + ", using auth token as is" ); return str; } } /** * Load username password from settings if user has not set them in JVM properties * * @param repo not null */ private void loadInfosFromSettings(ScmProviderRepositoryWithHost repo) { if (username == null || password == null) { String host = repo.getHost(); int port = repo.getPort(); if (port > 0) { host += ":" + port; } Server server = this.settings.getServer(host); if (server != null) { if (username == null) { username = server.getUsername(); } if (password == null) { password = decrypt( server.getPassword(), host ); } } } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#5 | 9520 | dantran | folder rename to match with its artifactId | ||
#4 | 9513 | dantran | add provider doc, deploy site:jar, support external charset config | ||
#3 | 9191 | dantran |
jobs,shelve, and unshelve should run as aggregate, there is no need to tranverse. Ensure extracting credential from settings.xml working |
||
#2 | 9190 | dantran | Migrate to java annotation, add loading credential from settings.xml if not given | ||
#1 | 9167 | dantran |
Populate //guest/dantran/p4maven/... from //guest/perforce_software/p4maven/main/.... |
||
//guest/perforce_software/p4maven/main/com.perforce.maven.mojo/src/main/java/com/perforce/maven/mojo/P4Mojo.java | |||||
#1 | 8496 | Matt Attaway | Move P4Ant and P4Maven into their proper homes | ||
//public/perforce/p4maven/main/com.perforce.maven.mojo/src/main/java/com/perforce/maven/mojo/P4Mojo.java | |||||
#1 | 8270 | Matt Attaway | Initial add of P4Maven source code |