diff --git a/maven-scm-plugin/pom.xml b/maven-scm-plugin/pom.xml index 5fb5498..7590a99 100644 --- a/maven-scm-plugin/pom.xml +++ b/maven-scm-plugin/pom.xml @@ -60,6 +60,15 @@ + org.sonatype.plexus + plexus-sec-dispatcher + 1.3 + + provided + + + org.apache.maven maven-project 2.0 diff --git a/maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/AbstractScmMojo.java b/maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/AbstractScmMojo.java index 11e4665..056faf0 100644 --- a/maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/AbstractScmMojo.java +++ b/maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/AbstractScmMojo.java @@ -36,7 +36,15 @@ import org.apache.maven.scm.repository.ScmRepository; import org.apache.maven.scm.repository.ScmRepositoryException; import org.apache.maven.settings.Server; import org.apache.maven.settings.Settings; +import org.codehaus.plexus.PlexusConstants; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.codehaus.plexus.context.Context; +import org.codehaus.plexus.context.ContextException; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; import org.codehaus.plexus.util.StringUtils; +import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; +import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException; import java.io.File; import java.io.IOException; @@ -49,8 +57,13 @@ import java.util.Properties; * @version $Id$ */ public abstract class AbstractScmMojo - extends AbstractMojo + extends AbstractMojo implements Contextualizable { + /** + * The Maven container. Useful for lookups of services, like SecDispatcher. + */ + protected PlexusContainer container; + /** * The SCM connection URL. * @@ -164,6 +177,8 @@ public abstract class AbstractScmMojo */ private Map providerImplementations; + + /** {@inheritDoc} */ public void execute() throws MojoExecutionException @@ -197,6 +212,15 @@ public abstract class AbstractScmMojo } } + /** + * Injection site for setup of Maven container reference. + */ + public void contextualize( Context context ) + throws ContextException + { + container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); + } + protected void setConnectionType( String connectionType ) { this.connectionType = connectionType; @@ -342,7 +366,7 @@ public abstract class AbstractScmMojo * * @param repo not null */ - private void loadInfosFromSettings( ScmProviderRepositoryWithHost repo ) + private void loadInfosFromSettings( ScmProviderRepositoryWithHost repo ) throws MojoExecutionException { if ( username == null || password == null ) { @@ -366,7 +390,33 @@ public abstract class AbstractScmMojo if ( password == null ) { + //Retrieve the password from settings, possibly in encrypted form password = this.settings.getServer( host ).getPassword(); + + SecDispatcher sd = null; + + //Attempt to get the Security Dispatcher to decrypt the password. + //Must allow for this to fail gracefully as decryption only arrived in Maven 2.1 + try { + sd = (SecDispatcher)container.lookup( SecDispatcher.ROLE, "maven" ); + } + catch ( ComponentLookupException e ) + { + getLog().error( "Cannot lookup SecDispatcher for decryption. Are you using Maven 2.1 or higher?"); + } + + if ( sd != null ) + { + try { + //The decrypt method tests if the string really needs decryption + // and returns the string unmodified if no decryption is warranted. + password = sd.decrypt( password ); + } + catch ( SecDispatcherException e ) + { + throw new MojoExecutionException("Decryption error", e); + } + } } if ( privateKey == null )