Skip to content
Snippets Groups Projects
Commit 67055a05 authored by Pablo's avatar Pablo
Browse files

All Mojos now extends AbstractMojo who provides commons parameters and...

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)
parent 8a4dc106
No related branches found
No related tags found
No related merge requests found
...@@ -34,26 +34,100 @@ public abstract class AbstractEmbeddedMongoMojo extends AbstractMojo { ...@@ -34,26 +34,100 @@ public abstract class AbstractEmbeddedMongoMojo extends AbstractMojo {
@Parameter(property = "embedmongo.skip", defaultValue = "false") @Parameter(property = "embedmongo.skip", defaultValue = "false")
private boolean skip; private boolean skip;
/**
* The port MongoDB should run on.
*
* @since 0.1.0
*/
@Parameter(property = "embedmongo.port", defaultValue = "27017") @Parameter(property = "embedmongo.port", defaultValue = "27017")
private int port; 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") @Parameter(property = "embedmongo.randomPort", defaultValue = "false")
private boolean randomPort; 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") @Parameter(property = "embedmongo.version", defaultValue = "2.2.1")
private String version; 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 @Component
protected MavenProject project; protected MavenProject project;
public AbstractEmbeddedMongoMojo() {
}
AbstractEmbeddedMongoMojo(int port) {
this.port = port;
}
@Override @Override
public final void execute() throws MojoExecutionException, MojoFailureException { public final void execute() throws MojoExecutionException, MojoFailureException {
if(!skip){ if(skip) {
executeEmbeddedMongo(); 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() { protected IFeatureAwareVersion getVersion() {
String versionEnumName = this.version.toUpperCase().replaceAll("\\.", "_"); String versionEnumName = this.version.toUpperCase().replaceAll("\\.", "_");
...@@ -84,5 +158,50 @@ public abstract class AbstractEmbeddedMongoMojo extends AbstractMojo { ...@@ -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;
}
} }
...@@ -47,7 +47,7 @@ public class ImportEmbeddedMongoMojo extends AbstractEmbeddedMongoMojo { ...@@ -47,7 +47,7 @@ public class ImportEmbeddedMongoMojo extends AbstractEmbeddedMongoMojo {
private Boolean parallel; private Boolean parallel;
@Override @Override
public void executeEmbeddedMongo() throws MojoExecutionException, MojoFailureException { public void executeStart() throws MojoExecutionException, MojoFailureException {
try { try {
sendImportScript(); sendImportScript();
} catch (Exception e) { } catch (Exception e) {
......
/** /**
* Copyright © 2012 Joe Littlejohn * Copyright © 2012 Pierre-Jean Vardanéga
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,6 +26,9 @@ import com.mongodb.CommandResult; ...@@ -26,6 +26,9 @@ import com.mongodb.CommandResult;
import com.mongodb.DB; import com.mongodb.DB;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.MongoException; 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 * When invoked, this goal connects to an instance of mongo and execute some instructions
...@@ -33,43 +36,37 @@ import com.mongodb.MongoException; ...@@ -33,43 +36,37 @@ import com.mongodb.MongoException;
* *
* You should use the same javascript syntax that you would use in the mongo client. * 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. * Folder that contains all scripts to execute.
* @parameter * @parameter
* @required * @required
*/ */
@Parameter(property = "scriptsDirectory", required = true)
private File scriptsDirectory; 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. * The name of the database where data will be stored.
* @parameter * @parameter
* @required * @required
*/ */
@Parameter(property = "databaseName", required = true)
private String databaseName; private String databaseName;
public InitDataMongoMojo() { public InitDataMongoMojo() {
} }
InitDataMongoMojo(File scriptsDirectory, int port, String databaseName) { InitDataMongoMojo(File scriptsDirectory, int port, String databaseName) {
super(port);
this.scriptsDirectory = scriptsDirectory; this.scriptsDirectory = scriptsDirectory;
this.port = port;
this.databaseName = databaseName; this.databaseName = databaseName;
} }
@Override @Override
public void execute() throws MojoExecutionException, MojoFailureException { public void executeStart() throws MojoExecutionException, MojoFailureException {
DB db = connectToMongoAndGetDatabase(); DB db = connectToMongoAndGetDatabase();
if (scriptsDirectory.isDirectory()) { if (scriptsDirectory.isDirectory()) {
...@@ -113,7 +110,7 @@ public class InitDataMongoMojo extends AbstractMojo { ...@@ -113,7 +110,7 @@ public class InitDataMongoMojo extends AbstractMojo {
MongoClient mongoClient; MongoClient mongoClient;
try { try {
mongoClient = new MongoClient("localhost", port == 0 ? 27017 : port); mongoClient = new MongoClient("localhost", getPort());
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
throw new MojoExecutionException("Unable to connect to mongo instance", e); throw new MojoExecutionException("Unable to connect to mongo instance", e);
} }
......
...@@ -33,6 +33,9 @@ import java.util.concurrent.TimeUnit; ...@@ -33,6 +33,9 @@ import java.util.concurrent.TimeUnit;
import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException; 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 org.apache.maven.project.MavenProject;
import com.github.joelittlejohn.embedmongo.log.Loggers; import com.github.joelittlejohn.embedmongo.log.Loggers;
...@@ -66,161 +69,76 @@ import de.flapdoodle.embed.process.store.IArtifactStore; ...@@ -66,161 +69,76 @@ import de.flapdoodle.embed.process.store.IArtifactStore;
* When invoked, this goal starts an instance of mongo. The required binaries * When invoked, this goal starts an instance of mongo. The required binaries
* are downloaded if no mongo release is found in <code>~/.embedmongo</code>. * are downloaded if no mongo release is found in <code>~/.embedmongo</code>.
* *
* @goal start
* @phase pre-integration-test
* @see <a * @see <a
* href="http://github.com/flapdoodle-oss/embedmongo.flapdoodle.de">http://github.com/flapdoodle-oss/embedmongo.flapdoodle.de</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(); private static final String PACKAGE_NAME = StartEmbeddedMongoMojo.class.getPackage().getName();
public static final String MONGOD_CONTEXT_PROPERTY_NAME = PACKAGE_NAME + ".mongod"; 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. * The location of a directory that will hold the MongoDB data files.
* *
* @parameter expression="${embedmongo.databaseDirectory}"
* @since 0.1.0 * @since 0.1.0
*/ */
@Parameter(property = "embedmongo.databaseDirectory")
private File databaseDirectory; private File databaseDirectory;
/** /**
* An IP address for the MongoDB instance to be bound to during its * An IP address for the MongoDB instance to be bound to during its
* execution. * execution.
* *
* @parameter expression="${embedmongo.bindIp}"
* @since 0.1.4 * @since 0.1.4
*/ */
@Parameter(property = "embedmongo.bindIp")
private String 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 * @since 0.1.3
*/ */
@Parameter(property = "embedmongo.logging")
private String logging; private String logging;
/** /**
* @parameter expression="${embedmongo.logFile}"
* default-value="embedmongo.log"
* @since 0.1.7 * @since 0.1.7
*/ */
@Parameter(property = "embedmongo.logFile", defaultValue = "embedmongo.log")
private String logFile; private String logFile;
/** /**
* @parameter expression="${embedmongo.logFileEncoding}"
* default-value="utf-8"
* @since 0.1.7 * @since 0.1.7
*/ */
@Parameter(property = "embedmongo.logFileEncoding", defaultValue = "utf-8")
private String logFileEncoding; private String logFileEncoding;
/** /**
* The base URL to be used when downloading MongoDB * The base URL to be used when downloading MongoDB
* *
* @parameter expression="${embedmongo.downloadPath}"
* default-value="http://fastdl.mongodb.org/"
* @since 0.1.10 * @since 0.1.10
*/ */
@Parameter(property = "embedmongo.downloadPath", defaultValue = "http://fastdl.mongodb.org/")
private String downloadPath; 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 * Should authorization be enabled for MongoDB
* *
* @parameter expression="${embedmongo.authEnabled}" default-value="false"
*/ */
@Parameter(property = "embedmongo.authEnabled", defaultValue = "false")
private boolean authEnabled; private boolean authEnabled;
/** @Override
* The maven project. protected void onSkip() {
* getLog().debug("skip=true, not starting embedmongo");
* @parameter expression="${project}" }
* @readonly
*/
private MavenProject project;
/**
* @parameter expression="${embedmongo.skip}" default-value="false"
*/
private boolean skip;
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException, MojoFailureException { public void executeStart() throws MojoExecutionException, MojoFailureException {
if (skip) { if (hasProxyConfigured()) {
getLog().debug("skip=true, not starting embedmongo");
return;
}
if (this.proxyHost != null && this.proxyHost.length() > 0) {
this.addProxySelector(); this.addProxySelector();
} }
...@@ -248,10 +166,12 @@ public class StartEmbeddedMongoMojo extends AbstractMojo { ...@@ -248,10 +166,12 @@ public class StartEmbeddedMongoMojo extends AbstractMojo {
.commandLinePostProcessor(commandLinePostProcessor) .commandLinePostProcessor(commandLinePostProcessor)
.build(); .build();
if (randomPort) { int port = getPort();
if (isRandomPort()) {
port = PortUtils.allocateRandomPort(); port = PortUtils.allocateRandomPort();
} }
savePortToProjectProperties(); savePortToProjectProperties(port);
IMongodConfig config = new MongodConfigBuilder() IMongodConfig config = new MongodConfigBuilder()
.version(getVersion()).net(new Net(bindIp, port, Network.localhostIsIPv6())) .version(getVersion()).net(new Net(bindIp, port, Network.localhostIsIPv6()))
...@@ -270,7 +190,7 @@ public class StartEmbeddedMongoMojo extends AbstractMojo { ...@@ -270,7 +190,7 @@ public class StartEmbeddedMongoMojo extends AbstractMojo {
try { try {
MongodProcess mongod = executable.start(); MongodProcess mongod = executable.start();
if (wait) { if (isWait()) {
while (true) { while (true) {
try { try {
TimeUnit.MINUTES.sleep(5); TimeUnit.MINUTES.sleep(5);
...@@ -286,14 +206,6 @@ public class StartEmbeddedMongoMojo extends AbstractMojo { ...@@ -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 { private ProcessOutput getOutputConfig() throws MojoFailureException {
...@@ -321,6 +233,11 @@ public class StartEmbeddedMongoMojo extends AbstractMojo { ...@@ -321,6 +233,11 @@ public class StartEmbeddedMongoMojo extends AbstractMojo {
private void addProxySelector() { private void addProxySelector() {
// Add authenticator with proxyUser and proxyPassword // 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) { if (proxyUser != null && proxyPassword != null) {
Authenticator.setDefault(new Authenticator() { Authenticator.setDefault(new Authenticator() {
@Override @Override
...@@ -347,27 +264,6 @@ public class StartEmbeddedMongoMojo extends AbstractMojo { ...@@ -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() { private String getDataDirectory() {
if (databaseDirectory != null) { if (databaseDirectory != null) {
return databaseDirectory.getAbsolutePath(); return databaseDirectory.getAbsolutePath();
......
...@@ -28,19 +28,10 @@ import de.flapdoodle.embed.mongo.MongodProcess; ...@@ -28,19 +28,10 @@ import de.flapdoodle.embed.mongo.MongodProcess;
* @goal stop * @goal stop
* @phase post-integration-test * @phase post-integration-test
*/ */
public class StopEmbeddedMongoMojo extends AbstractMojo { public class StopEmbeddedMongoMojo extends AbstractEmbeddedMongoMojo {
/**
* @parameter expression="${embedmongo.skip}" default-value="false"
*/
private boolean skip;
@Override @Override
public void execute() throws MojoExecutionException, MojoFailureException { public void executeStart() throws MojoExecutionException, MojoFailureException {
if (skip) {
return;
}
MongodProcess mongod = (MongodProcess)getPluginContext().get(StartEmbeddedMongoMojo MongodProcess mongod = (MongodProcess)getPluginContext().get(StartEmbeddedMongoMojo
.MONGOD_CONTEXT_PROPERTY_NAME); .MONGOD_CONTEXT_PROPERTY_NAME);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment