@ user944849 は私を正しい方向に導きました。これが解決策です。
Maven 2 を使用している場合は、mojo に次の依存関係を追加する必要があります。
<dependency>
<groupId>org.sonatype.plexus</groupId>
<artifactId>plexus-sec-dispatcher</artifactId>
<version>1.4</version>
<scope>compile</scope>
</dependency>
そして、以下を入れます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>
次に、Mojo でパスワードを通常のプロパティとして取得SecDispatcher
し、同じroleHint
. のdecrypt
メソッドString
は、Maven で暗号化された文字列でない場合、文字列自体を返します。
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);
}
}
文字列は、 のプロパティsettings.xml
やプロファイルに含めることができます。また、暗号化された文字列をコマンドラインでシステム プロパティとして渡すこともできます。
参考文献: