Author: tchemit Date: 2009-10-01 22:44:34 +0200 (Thu, 01 Oct 2009) New Revision: 601 Added: trunk/src/main/java/org/nuiton/plugin/ShareServerSecretPlugin.java Modified: trunk/src/main/java/org/nuiton/plugin/CheckProjectFilesPlugin.java Log: - Evolution #69: ajout d'un goal pour exposer des secrets du serveur - le goal CheckProjectFile est un AbstractPlugin Modified: trunk/src/main/java/org/nuiton/plugin/CheckProjectFilesPlugin.java =================================================================== --- trunk/src/main/java/org/nuiton/plugin/CheckProjectFilesPlugin.java 2009-09-29 17:47:52 UTC (rev 600) +++ trunk/src/main/java/org/nuiton/plugin/CheckProjectFilesPlugin.java 2009-10-01 20:44:34 UTC (rev 601) @@ -21,9 +21,7 @@ package org.nuiton.plugin; import java.io.File; -import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; /** @@ -39,7 +37,7 @@ * @author chemit * @since 1.0.2 */ -public class CheckProjectFilesPlugin extends AbstractMojo { +public class CheckProjectFilesPlugin extends AbstractPlugin { /** * Dependance du projet. @@ -58,7 +56,31 @@ protected boolean verbose; @Override - public void execute() throws MojoExecutionException, MojoFailureException { + public MavenProject getProject() { + return project; + } + + @Override + public void setProject(MavenProject project) { + this.project = project; + } + + @Override + public boolean isVerbose() { + return verbose; + } + + @Override + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + @Override + protected boolean init() throws Exception { + return true; + } + + protected void doAction() throws Exception { checkChangelogFile(); checkReadmeFile(); checkLicenseFile(); Added: trunk/src/main/java/org/nuiton/plugin/ShareServerSecretPlugin.java =================================================================== --- trunk/src/main/java/org/nuiton/plugin/ShareServerSecretPlugin.java (rev 0) +++ trunk/src/main/java/org/nuiton/plugin/ShareServerSecretPlugin.java 2009-10-01 20:44:34 UTC (rev 601) @@ -0,0 +1,182 @@ +package org.nuiton.plugin; + +import java.util.Properties; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; +import org.apache.maven.settings.Server; +import org.apache.maven.settings.Settings; +import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; + +/** + * Obtain a server authentication and share it in the maven + * project properties. + * <p/> + * To select data to export from the server with the given {@code serverId}, + * fill the properties : + * <pre> + * usernameOut + * passwordOut + * passphraseOut + * </pre> + * + * @author chemit + * @since 1.1.0 + * + * @goal share-server-secret + * @phase initialize + * @requireProjects true + */ +public class ShareServerSecretPlugin extends AbstractPlugin { + + /** + * Dependance du projet. + * + * @parameter default-value="${project}" + * @required + * @readonly + * @since 1.1.0 + */ + protected MavenProject project; + /** + * Dependance du settings. + * + * @parameter default-value="${settings}" + * @required + * @readonly + * @since 1.1.0 + */ + protected Settings settings; + /** + * Server id to use for authentication (must be defined in your setting + * and use the maven >= 2.1.0 password encryption mecanism). + * <p/> + * + * @parameter expression="${helper.serverId}" + * @required + * @since 1.1.0 + */ + protected String serverId; + /** + * The name of the property where to export the username of the server. + * <p/> + * <b>Note:</> If not set - then no export of the username of the server. + * + * @parameter + * @since 1.1.0 + */ + private String usernameOut; + /** + * The name of the property where to export the password of the server. + * <p/> + * <b>Note:</> If not set - then no export of the password of the server. + * <p/> + * <b>Note:</> If the password is crypted (since maven 2.1.0) then decrypt it. + * + * @parameter + * @since 1.1.0 + */ + private String passwordOut; + /** + * The name of the property where to export the passphrase of the server. + * <p/> + * <b>Note:</> If not set - then no export of the passphrase of the server. + * <p/> + * <b>Note:</> If the passphrase is crypted (since maven 2.1.0) then decrypt it. + * + * @parameter + * @since 1.1.0 + */ + private String passphraseOut; + /** + * Un flag pour activer le mode verbeux. + * + * @parameter expression="${helper.verbose}" default-value="${maven.verbose}" + * @since 1.1.0 + */ + protected boolean verbose; + /** + * password decypher + * + * @component roleHint="maven-helper-plugin" + * @since 1.1.0 + */ + protected SecDispatcher sec; + private boolean exportUsername, exportPassword, exportPassphrase; + + @Override + protected boolean init() throws Exception { + if (usernameOut != null && !usernameOut.trim().isEmpty()) { + exportUsername = true; + } + if (passwordOut != null && !passwordOut.trim().isEmpty()) { + exportPassword = true; + } + if (passphraseOut != null && !passphraseOut.trim().isEmpty()) { + exportPassphrase = true; + } + if (!(exportUsername || exportPassword || exportPassphrase)) { + getLog().error("Nothing to export, you should specify what to export via 'usernameOut', 'passwordOut', 'passphraseOut' parameters."); + return false; + } + return true; + } + + @Override + protected void doAction() throws Exception { + Server server = null; + + if (serverId != null && !serverId.trim().isEmpty()) { + + server = settings.getServer(serverId); + + if (server == null) { + throw new MojoExecutionException("Could not find server with id '" + serverId + "', check your settings.xml file."); + } + } + + Properties properties = project.getModel().getProperties(); + + if (exportUsername) { + + String username = server.getUsername(); + getLog().info("export server [" + serverId + "] username in ${" + usernameOut + "}"); + properties.setProperty(usernameOut, username); + } + + if (exportPassword) { + String password = server.getPassword(); + password = sec.decrypt(server.getPassword()); + + getLog().info("export server [" + serverId + "] password in ${" + passwordOut + "}"); + properties.setProperty(passwordOut, password); + } + + if (exportPassphrase) { + String passphrase = server.getPassphrase(); + passphrase = sec.decrypt(server.getPassword()); + + getLog().info("export server [" + serverId + "] passphrase in ${" + passphraseOut + "}"); + properties.setProperty(passphraseOut, passphrase); + } + } + + @Override + public MavenProject getProject() { + return project; + } + + @Override + public void setProject(MavenProject project) { + this.project = project; + } + + @Override + public boolean isVerbose() { + return verbose; + } + + @Override + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } +} Property changes on: trunk/src/main/java/org/nuiton/plugin/ShareServerSecretPlugin.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL