Maven password encryption for other properties

I would like to use Maven password encryption, for example, it uses Mojo properties for nodes. I tried just pasting the encrypted password into the correct property for mojo, but he treated it as plain text. I was hoping there was an attribute that I could set in the annotations for the Mojo property, which would explain that it could be encrypted, and if so, use the systemโ€™s main password for decryption, but I donโ€™t see anything in the documentation for that.

Could anyone use Maven password encryption for anything but the server nodes? I would like to make this work for my Mojo.

+4
source share
4 answers

Not a complete answer, but hopefully a pointer in the right direction ...

maven-scm-plugin , maven-release-plugin , and tomcat6-maven-plugin allow you to read passwords from the <servers> section of the file ${user.home}/.m2/settings.xml .

Perhaps if you look at the source code for these plugins / goals, you will find a Maven kernel or a generic component that allows you to do what you want, and you can adapt it for your needs.

+3
source

Take a look at this code as an example of SqlExecMojo . If you are in the plugin, you can get a password and decrypt it. If you want to use it to filter properties in a resource plugin, we probably need to write a custom version of the resource plugin. I have a similar problem could end up with this.

+1
source

@ 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; /** * @goal echopass * * @phase process-sources */ public class MyMojo extends AbstractMojo { /** * The password * @parameter expression="${password}" */ private String password; /** * Plexus component for the SecDispatcher * @component roleHint="mng-4384" */ 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:

+1
source

Using the Colselaws solution, I was getting a null pointer exception. The SecDispatcher component has not been installed. I needed to change the annotation to this:

 //Plexus component for the SecDispatcher @Component(role = SecDispatcher.class, hint = "mng-4384") private SecDispatcher secDispatcher; 

Please note that this is outside of any comment on the document, and the component must be imported from the maven annotation plugin.

0
source

All Articles