From 67055a051f85c4ae292407ab9eb9213d7e1c8847 Mon Sep 17 00:00:00 2001 From: Pablo <padilo@gmail.com> Date: Sat, 16 May 2015 18:15:44 +0200 Subject: [PATCH] All Mojos now extends AbstractMojo who provides commons parameters and functionality. To allow this the parameters now are readed using annotations instead of the old comments alternative no allow inheritance. (testing is pending) --- .../embedmongo/AbstractEmbeddedMongoMojo.java | 127 +++++++++++++- .../embedmongo/ImportEmbeddedMongoMojo.java | 2 +- .../embedmongo/InitDataMongoMojo.java | 25 ++- .../embedmongo/StartEmbeddedMongoMojo.java | 160 +++--------------- .../embedmongo/StopEmbeddedMongoMojo.java | 13 +- 5 files changed, 165 insertions(+), 162 deletions(-) diff --git a/src/main/java/com/github/joelittlejohn/embedmongo/AbstractEmbeddedMongoMojo.java b/src/main/java/com/github/joelittlejohn/embedmongo/AbstractEmbeddedMongoMojo.java index 341a586..504b9cf 100644 --- a/src/main/java/com/github/joelittlejohn/embedmongo/AbstractEmbeddedMongoMojo.java +++ b/src/main/java/com/github/joelittlejohn/embedmongo/AbstractEmbeddedMongoMojo.java @@ -34,26 +34,100 @@ public abstract class AbstractEmbeddedMongoMojo extends AbstractMojo { @Parameter(property = "embedmongo.skip", defaultValue = "false") private boolean skip; + /** + * The port MongoDB should run on. + * + * @since 0.1.0 + */ @Parameter(property = "embedmongo.port", defaultValue = "27017") private int port; + /** + * Whether a random free port should be used for MongoDB instead of the one + * specified by {@code port}. If {@code randomPort} is {@code true}, the + * random port chosen will be available in the Maven project property + * {@code embedmongo.port}. + * + * @since 0.1.8 + */ @Parameter(property = "embedmongo.randomPort", defaultValue = "false") private boolean randomPort; + /** + * The version of MongoDB to run e.g. 2.1.1, 1.6 v1.8.2, V2_0_4, + * + * @since 0.1.0 + */ @Parameter(property = "embedmongo.version", defaultValue = "2.2.1") private String version; + /** + * A proxy hostname to be used when downloading MongoDB distributions. + * + * @since 0.1.1 + */ + @Parameter(property = "embedmongo.proxyHost") + private String proxyHost; + + /** + * A proxy port to be used when downloading MongoDB distributions. + * + * @since 0.1.1 + */ + @Parameter(property = "embedmongo.proxyPort", defaultValue = "80") + private int proxyPort; + + /** + * Block immediately and wait until MongoDB is explicitly stopped (eg: + * {@literal <ctrl-c>}). This option makes this goal similar in spirit to + * something like jetty:run, useful for interactive debugging. + * + * @since 0.1.2 + */ + @Parameter(property = "embedmongo.wait", defaultValue = "false") + private boolean wait; + + /** + * The proxy user to be used when downloading MongoDB + * + * @since 0.1.6 + */ + @Parameter(property = "embedmongo.proxyUser") + private String proxyUser; + + /** + * The proxy password to be used when downloading MondoDB + * + * @since 0.1.6 + */ + @Parameter(property = "embedmongo.proxyPassword") + private String proxyPassword; + + @Component protected MavenProject project; + public AbstractEmbeddedMongoMojo() { + + } + + AbstractEmbeddedMongoMojo(int port) { + this.port = port; + } + @Override public final void execute() throws MojoExecutionException, MojoFailureException { - if(!skip){ - executeEmbeddedMongo(); + if(skip) { + onSkip(); + } else { + executeStart(); } } - // FIXME: This is a copy/paste version of StartEmbeddedMongoMojo + protected void onSkip() { + // Nothing to do, this is just to allow do things if mojo is skipped + } + protected IFeatureAwareVersion getVersion() { String versionEnumName = this.version.toUpperCase().replaceAll("\\.", "_"); @@ -84,5 +158,50 @@ public abstract class AbstractEmbeddedMongoMojo extends AbstractMojo { } } - public abstract void executeEmbeddedMongo() throws MojoExecutionException, MojoFailureException; + public abstract void executeStart() throws MojoExecutionException, MojoFailureException; + + /** + * Saves port to the {@link MavenProject#getProperties()} (with the property + * name {@code embedmongo.port}) to allow others (plugins, tests, etc) to + * find the randomly allocated port. + */ + protected void savePortToProjectProperties(int port) { + project.getProperties().put("embedmongo.port", String.valueOf(port)); + } + + public boolean isSkip() { + return skip; + } + + public boolean isRandomPort() { + return randomPort; + } + + public String getProxyHost() { + return proxyHost; + } + + public int getProxyPort() { + return proxyPort; + } + + public boolean isWait() { + return wait; + } + + public String getProxyUser() { + return proxyUser; + } + + public String getProxyPassword() { + return proxyPassword; + } + + public MavenProject getProject() { + return project; + } + + public boolean hasProxyConfigured() { + return this.proxyHost != null && this.proxyHost.length() > 0; + } } diff --git a/src/main/java/com/github/joelittlejohn/embedmongo/ImportEmbeddedMongoMojo.java b/src/main/java/com/github/joelittlejohn/embedmongo/ImportEmbeddedMongoMojo.java index d363b81..37fbab7 100644 --- a/src/main/java/com/github/joelittlejohn/embedmongo/ImportEmbeddedMongoMojo.java +++ b/src/main/java/com/github/joelittlejohn/embedmongo/ImportEmbeddedMongoMojo.java @@ -47,7 +47,7 @@ public class ImportEmbeddedMongoMojo extends AbstractEmbeddedMongoMojo { private Boolean parallel; @Override - public void executeEmbeddedMongo() throws MojoExecutionException, MojoFailureException { + public void executeStart() throws MojoExecutionException, MojoFailureException { try { sendImportScript(); } catch (Exception e) { diff --git a/src/main/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojo.java b/src/main/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojo.java index e7a81a6..156d3b9 100644 --- a/src/main/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojo.java +++ b/src/main/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojo.java @@ -1,5 +1,5 @@ /** - * Copyright © 2012 Joe Littlejohn + * Copyright © 2012 Pierre-Jean Vardanéga * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,9 @@ import com.mongodb.CommandResult; import com.mongodb.DB; import com.mongodb.MongoClient; import com.mongodb.MongoException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; /** * When invoked, this goal connects to an instance of mongo and execute some instructions @@ -33,43 +36,37 @@ import com.mongodb.MongoException; * * You should use the same javascript syntax that you would use in the mongo client. * - * @goal init-data - * @phase pre-integration-test */ -public class InitDataMongoMojo extends AbstractMojo { +@Mojo(name="init-data", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST) +public class InitDataMongoMojo extends AbstractEmbeddedMongoMojo { /** * Folder that contains all scripts to execute. * @parameter * @required */ + @Parameter(property = "scriptsDirectory", required = true) private File scriptsDirectory; - /** - * Optional. Specify the port only if you want have a different one from the default value. - * Default value is 27017. - * @parameter - */ - private int port; - /** * The name of the database where data will be stored. * @parameter * @required */ + @Parameter(property = "databaseName", required = true) private String databaseName; public InitDataMongoMojo() { } InitDataMongoMojo(File scriptsDirectory, int port, String databaseName) { + super(port); this.scriptsDirectory = scriptsDirectory; - this.port = port; this.databaseName = databaseName; } @Override - public void execute() throws MojoExecutionException, MojoFailureException { + public void executeStart() throws MojoExecutionException, MojoFailureException { DB db = connectToMongoAndGetDatabase(); if (scriptsDirectory.isDirectory()) { @@ -113,7 +110,7 @@ public class InitDataMongoMojo extends AbstractMojo { MongoClient mongoClient; try { - mongoClient = new MongoClient("localhost", port == 0 ? 27017 : port); + mongoClient = new MongoClient("localhost", getPort()); } catch (UnknownHostException e) { throw new MojoExecutionException("Unable to connect to mongo instance", e); } diff --git a/src/main/java/com/github/joelittlejohn/embedmongo/StartEmbeddedMongoMojo.java b/src/main/java/com/github/joelittlejohn/embedmongo/StartEmbeddedMongoMojo.java index d181248..40df40f 100644 --- a/src/main/java/com/github/joelittlejohn/embedmongo/StartEmbeddedMongoMojo.java +++ b/src/main/java/com/github/joelittlejohn/embedmongo/StartEmbeddedMongoMojo.java @@ -33,6 +33,9 @@ import java.util.concurrent.TimeUnit; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import com.github.joelittlejohn.embedmongo.log.Loggers; @@ -66,161 +69,76 @@ import de.flapdoodle.embed.process.store.IArtifactStore; * When invoked, this goal starts an instance of mongo. The required binaries * are downloaded if no mongo release is found in <code>~/.embedmongo</code>. * - * @goal start - * @phase pre-integration-test * @see <a * href="http://github.com/flapdoodle-oss/embedmongo.flapdoodle.de">http://github.com/flapdoodle-oss/embedmongo.flapdoodle.de</a> */ -public class StartEmbeddedMongoMojo extends AbstractMojo { +@Mojo(name="start", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST) +public class StartEmbeddedMongoMojo extends AbstractEmbeddedMongoMojo { private static final String PACKAGE_NAME = StartEmbeddedMongoMojo.class.getPackage().getName(); public static final String MONGOD_CONTEXT_PROPERTY_NAME = PACKAGE_NAME + ".mongod"; - /** - * The port MongoDB should run on. - * - * @parameter expression="${embedmongo.port}" default-value="27017" - * @since 0.1.0 - */ - private int port; - - /** - * Whether a random free port should be used for MongoDB instead of the one - * specified by {@code port}. If {@code randomPort} is {@code true}, the - * random port chosen will be available in the Maven project property - * {@code embedmongo.port}. - * - * @parameter expression="${embedmongo.randomPort}" default-value="false" - * @since 0.1.8 - */ - private boolean randomPort; - - /** - * The version of MongoDB to run e.g. 2.1.1, 1.6 v1.8.2, V2_0_4, - * - * @parameter expression="${embedmongo.version}" default-value="2.2.1" - * @since 0.1.0 - */ - private String version; - /** * The location of a directory that will hold the MongoDB data files. * - * @parameter expression="${embedmongo.databaseDirectory}" * @since 0.1.0 */ + @Parameter(property = "embedmongo.databaseDirectory") private File databaseDirectory; /** * An IP address for the MongoDB instance to be bound to during its * execution. * - * @parameter expression="${embedmongo.bindIp}" * @since 0.1.4 */ + @Parameter(property = "embedmongo.bindIp") private String bindIp; /** - * A proxy hostname to be used when downloading MongoDB distributions. - * - * @parameter expression="${embedmongo.proxyHost}" - * @since 0.1.1 - */ - private String proxyHost; - - /** - * A proxy port to be used when downloading MongoDB distributions. - * - * @parameter expression="${embedmongo.proxyPort}" default-value="80" - * @since 0.1.1 - */ - private int proxyPort; - - /** - * Block immediately and wait until MongoDB is explicitly stopped (eg: - * {@literal <ctrl-c>}). This option makes this goal similar in spirit to - * something like jetty:run, useful for interactive debugging. - * - * @parameter expression="${embedmongo.wait}" default-value="false" - * @since 0.1.2 - */ - private boolean wait; - - /** - * @parameter expression="${embedmongo.logging}" default-value="console" * @since 0.1.3 */ + @Parameter(property = "embedmongo.logging") private String logging; /** - * @parameter expression="${embedmongo.logFile}" - * default-value="embedmongo.log" * @since 0.1.7 */ + @Parameter(property = "embedmongo.logFile", defaultValue = "embedmongo.log") private String logFile; /** - * @parameter expression="${embedmongo.logFileEncoding}" - * default-value="utf-8" * @since 0.1.7 */ + @Parameter(property = "embedmongo.logFileEncoding", defaultValue = "utf-8") private String logFileEncoding; /** * The base URL to be used when downloading MongoDB * - * @parameter expression="${embedmongo.downloadPath}" - * default-value="http://fastdl.mongodb.org/" * @since 0.1.10 */ + @Parameter(property = "embedmongo.downloadPath", defaultValue = "http://fastdl.mongodb.org/") private String downloadPath; - /** - * The proxy user to be used when downloading MongoDB - * - * @parameter expression="${embedmongo.proxyUser}" - * @since 0.1.6 - */ - private String proxyUser; - - /** - * The proxy password to be used when downloading MondoDB - * - * @parameter expression="${embedmongo.proxyPassword}" - * @since 0.1.6 - */ - private String proxyPassword; /** * Should authorization be enabled for MongoDB * - * @parameter expression="${embedmongo.authEnabled}" default-value="false" */ + @Parameter(property = "embedmongo.authEnabled", defaultValue = "false") private boolean authEnabled; - /** - * The maven project. - * - * @parameter expression="${project}" - * @readonly - */ - private MavenProject project; - - /** - * @parameter expression="${embedmongo.skip}" default-value="false" - */ - private boolean skip; + @Override + protected void onSkip() { + getLog().debug("skip=true, not starting embedmongo"); + } @Override @SuppressWarnings("unchecked") - public void execute() throws MojoExecutionException, MojoFailureException { + public void executeStart() throws MojoExecutionException, MojoFailureException { - if (skip) { - getLog().debug("skip=true, not starting embedmongo"); - return; - } - - if (this.proxyHost != null && this.proxyHost.length() > 0) { + if (hasProxyConfigured()) { this.addProxySelector(); } @@ -248,10 +166,12 @@ public class StartEmbeddedMongoMojo extends AbstractMojo { .commandLinePostProcessor(commandLinePostProcessor) .build(); - if (randomPort) { + int port = getPort(); + + if (isRandomPort()) { port = PortUtils.allocateRandomPort(); } - savePortToProjectProperties(); + savePortToProjectProperties(port); IMongodConfig config = new MongodConfigBuilder() .version(getVersion()).net(new Net(bindIp, port, Network.localhostIsIPv6())) @@ -270,7 +190,7 @@ public class StartEmbeddedMongoMojo extends AbstractMojo { try { MongodProcess mongod = executable.start(); - if (wait) { + if (isWait()) { while (true) { try { TimeUnit.MINUTES.sleep(5); @@ -286,14 +206,6 @@ public class StartEmbeddedMongoMojo extends AbstractMojo { } } - /** - * Saves port to the {@link MavenProject#getProperties()} (with the property - * name {@code embedmongo.port}) to allow others (plugins, tests, etc) to - * find the randomly allocated port. - */ - private void savePortToProjectProperties() { - project.getProperties().put("embedmongo.port", String.valueOf(port)); - } private ProcessOutput getOutputConfig() throws MojoFailureException { @@ -321,6 +233,11 @@ public class StartEmbeddedMongoMojo extends AbstractMojo { private void addProxySelector() { // Add authenticator with proxyUser and proxyPassword + final String proxyUser = getProxyUser(); + final String proxyPassword = getProxyPassword(); + final String proxyHost = getProxyHost(); + final int proxyPort = getProxyPort(); + if (proxyUser != null && proxyPassword != null) { Authenticator.setDefault(new Authenticator() { @Override @@ -347,27 +264,6 @@ public class StartEmbeddedMongoMojo extends AbstractMojo { }); } - private IFeatureAwareVersion getVersion() { - String versionEnumName = this.version.toUpperCase().replaceAll("\\.", "_"); - - if (versionEnumName.charAt(0) != 'V') { - versionEnumName = "V" + versionEnumName; - } - - try { - return Version.valueOf(versionEnumName); - } catch (IllegalArgumentException e) { - getLog().warn("Unrecognised MongoDB version '" + this.version + "', this might be a new version that we don't yet know about. Attemping download anyway..."); - return Versions.withFeatures(new IVersion() { - @Override - public String asInDownloadPath() { - return version; - } - }); - } - - } - private String getDataDirectory() { if (databaseDirectory != null) { return databaseDirectory.getAbsolutePath(); diff --git a/src/main/java/com/github/joelittlejohn/embedmongo/StopEmbeddedMongoMojo.java b/src/main/java/com/github/joelittlejohn/embedmongo/StopEmbeddedMongoMojo.java index aaf05cc..41d06d7 100644 --- a/src/main/java/com/github/joelittlejohn/embedmongo/StopEmbeddedMongoMojo.java +++ b/src/main/java/com/github/joelittlejohn/embedmongo/StopEmbeddedMongoMojo.java @@ -28,19 +28,10 @@ import de.flapdoodle.embed.mongo.MongodProcess; * @goal stop * @phase post-integration-test */ -public class StopEmbeddedMongoMojo extends AbstractMojo { - - /** - * @parameter expression="${embedmongo.skip}" default-value="false" - */ - private boolean skip; +public class StopEmbeddedMongoMojo extends AbstractEmbeddedMongoMojo { @Override - public void execute() throws MojoExecutionException, MojoFailureException { - if (skip) { - return; - } - + public void executeStart() throws MojoExecutionException, MojoFailureException { MongodProcess mongod = (MongodProcess)getPluginContext().get(StartEmbeddedMongoMojo .MONGOD_CONTEXT_PROPERTY_NAME); -- GitLab