@ user944849 started me in the right direction and here is the solution.
If you are using Maven 2, you need to add the following dependency for your mojo:
<dependency> <groupId>org.sonatype.plexus</groupId> <artifactId>plexus-sec-dispatcher</artifactId> <version>1.4</version> <scope>compile</scope> </dependency>
And put the following in src/main/resources/META-INF/plexus/components.xml :
<?xml version="1.0" encoding="utf-8" ?> <component-set> <components> <component> <role>org.sonatype.plexus.components.sec.dispatcher.SecDispatcher</role> <role-hint>mng-4384</role-hint> <implementation>org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher</implementation> <requirements> <requirement> <role>org.sonatype.plexus.components.cipher.PlexusCipher</role> <role-hint>mng-4384</role-hint> <field-name>_cipher</field-name> </requirement> </requirements> <configuration> <_configuration-file>~/.m2/settings-security.xml</_configuration-file> </configuration> </component> <component> <role>org.sonatype.plexus.components.cipher.PlexusCipher</role> <role-hint>mng-4384</role-hint> <implementation>org.sonatype.plexus.components.cipher.DefaultPlexusCipher</implementation> </component> </components> </component-set>
Then in your Mojo, enter the password as a regular property and SecDispatcher as a component with the same roleHint . The decrypt method on String will return the string itself if it is not an encrypted Maven string.
import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException; public class MyMojo extends AbstractMojo { private String password; private SecDispatcher secDispatcher; private String decrypt(String input) { try { return secDispatcher.decrypt(input); } catch (SecDispatcherException sde) { getLog().warn(sde.getMessage()); return input; } } public void execute() throws MojoExecutionException { String s = decrypt(password); getLog().info("The password is " + s); } }
The string can be in a property in settings.xml , in the profile, or you can even pass the encrypted string as a system property on the command line.
Literature:
source share