Skip to content
Snippets Groups Projects
Commit 928d7521 authored by Pablo's avatar Pablo
Browse files

Rename goal import to mongo-import

Added AbstractEmbeddedMongoMojo to add common funcionality and parameters
Added annotations support to have inheritable parameters
parent bf5cc0da
No related branches found
No related tags found
No related merge requests found
......@@ -10,3 +10,4 @@
* Alexander Dietrich <dietrich@adobe.com>
* Brandon Chapman <bchapman@bridge2solutions.com>
* Carlos Ortiz <carlos.ortiz@craftercms.org>
* Pablo Diaz <padilo@gmail.com>
\ No newline at end of file
......@@ -53,6 +53,17 @@
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
<executions>
<execution>
<id>default-descriptor</id>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.mycila.maven-license-plugin</groupId>
<artifactId>maven-license-plugin</artifactId>
......@@ -95,6 +106,12 @@
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
......
/**
* Copyright © 2015 Pablo Diaz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.joelittlejohn.embedmongo;
import de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.mongo.distribution.Versions;
import de.flapdoodle.embed.process.distribution.IVersion;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
/**
* Created by pablo on 28/03/15.
*/
public abstract class AbstractEmbeddedMongoMojo extends AbstractMojo {
@Parameter(property = "embedmongo.skip", defaultValue = "false")
private boolean skip;
@Parameter(property = "embedmongo.port", defaultValue = "27017")
private int port;
@Parameter(property = "embedmongo.randomPort", defaultValue = "false")
private boolean randomPort;
@Parameter(property = "embedmongo.version", defaultValue = "2.2.1")
private String version;
@Component
protected MavenProject project;
@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
if(!skip){
executeEmbeddedMongo();
}
}
// FIXME: This is a copy/paste version of StartEmbeddedMongoMojo
protected 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;
}
});
}
}
protected Integer getPort() {
String portStr = project.getProperties().getProperty("embedmongo.port");
if(StringUtils.isNotBlank(portStr)){
return Integer.valueOf(portStr);
}else{
return port;
}
}
public abstract void executeEmbeddedMongo() throws MojoExecutionException, MojoFailureException;
}
/**
* Copyright © 2012 Pablo Diaz
* Copyright © 2015 Pablo Diaz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -18,71 +18,45 @@ package com.github.joelittlejohn.embedmongo;
import de.flapdoodle.embed.mongo.MongoImportExecutable;
import de.flapdoodle.embed.mongo.MongoImportProcess;
import de.flapdoodle.embed.mongo.MongoImportStarter;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.config.*;
import de.flapdoodle.embed.mongo.config.IMongoImportConfig;
import de.flapdoodle.embed.mongo.config.MongoImportConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.config.Timeout;
import de.flapdoodle.embed.process.runtime.Network;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @goal import
* @phase pre-integration-test
*/
public class ImportEmbeddedMongoMojo extends AbstractMojo {
/**
* @parameter
*/
@Mojo(name="mongo-import", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST)
public class ImportEmbeddedMongoMojo extends AbstractEmbeddedMongoMojo {
@Parameter
private ImportDataConfig[] imports;
/**
* @parameter expression="${embedmongo.defaultImportDatabase}"
*/
@Parameter(property = "embedmongo.defaultImportDatabase")
private String defaultImportDatabase;
/**
* @parameter expression="${embedmongo.import.wait}" default-value="false"
*/
private Boolean wait;
/**
* @parameter expression="${embedmongo.parallel}" default-value="false"
*/
@Parameter(property = "embedmongo.parallel", defaultValue = "false")
private Boolean parallel;
/**
* @parameter expression="${embedmongo.skip}" default-value="false"
*/
private boolean skip;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
return;
}
MongodProcess mongod = (MongodProcess)getPluginContext().get(StartEmbeddedMongoMojo.MONGOD_CONTEXT_PROPERTY_NAME);
if(mongod == null) {
throw new MojoExecutionException("Can't import without an EmbeddedMongoDB running");
}
public void executeEmbeddedMongo() throws MojoExecutionException, MojoFailureException {
try {
sendImportScript(mongod);
sendImportScript();
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
private void sendImportScript(MongodProcess mongod) throws IOException, InterruptedException, MojoExecutionException {
private void sendImportScript() throws IOException, InterruptedException, MojoExecutionException {
List<MongoImportProcess> pendingMongoProcess = new ArrayList<MongoImportProcess>();
if(imports == null || imports.length == 0) {
......@@ -91,8 +65,6 @@ public class ImportEmbeddedMongoMojo extends AbstractMojo {
return;
}
IMongodConfig config = mongod.getConfig();
getLog().info("Default import database: " + defaultImportDatabase);
for(ImportDataConfig importData: imports) {
......@@ -107,8 +79,8 @@ public class ImportEmbeddedMongoMojo extends AbstractMojo {
}
IMongoImportConfig mongoImportConfig = new MongoImportConfigBuilder()
.version(config.version())
.net(new Net(config.net().getPort(), Network.localhostIsIPv6()))
.version(getVersion())
.net(new Net(getPort(), Network.localhostIsIPv6()))
.db(database)
.collection(importData.getCollection())
.upsert(importData.getUpsertOnImport())
......@@ -134,11 +106,6 @@ public class ImportEmbeddedMongoMojo extends AbstractMojo {
waitFor(importProcess);
}
if(wait) {
getLog().info("STARTED - MongoDB up and all imports done.");
mongod.waitFor();
}
}
private void waitFor(MongoImportProcess importProcess) throws InterruptedException, MojoExecutionException {
......
......@@ -40,9 +40,9 @@
</configuration>
</execution>
<execution>
<id>import</id>
<id>mongo-import</id>
<goals>
<goal>import</goal>
<goal>mongo-import</goal>
</goals>
<configuration>
<defaultImportDatabase>test</defaultImportDatabase>
......
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