Skip to content
Snippets Groups Projects
Commit 76c74f12 authored by Joe Littlejohn's avatar Joe Littlejohn
Browse files

Add support for downloads via a proxy

Since it may not be preferable to add -Dhttp.proxyHost since this would
affect the whole build, you can now set proxyHost and proxyPort _only_
for the Mongo download.
parent 5d18ffd9
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,8 @@ Usage
<port>37017</port> <!-- optional, default 27017 -->
<version>2.0.4</version> <!-- optional, default 2.1.1 -->
<databaseDirectory>/tmp/mongotest</databaseDirectory> <!-- optional, default is a new dir in java.io.tmpdir -->
<proxyHost>myproxy.company.com</proxyHost> <!-- optional, default is none -->
<proxyPort>8080</proxyPort> <!-- optional, default 80 -->
</configuration>
</execution>
<execution>
......@@ -39,4 +41,5 @@ Notes
-----
* If you omit/forget the `stop` goal, any Mongo process spawned by the `start` goal will be stopped when the JVM terminates.
* If you want to run many Maven builds in parallel using Jenkins, try the [Port Allocator Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Port+Allocator+Plugin) to avoid port conflicts.
\ No newline at end of file
* If you want to run many Maven builds in parallel using Jenkins, try the [Port Allocator Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Port+Allocator+Plugin) to avoid port conflicts.
* If you need to use a proxy to download MongoDB then you can either use `-Dhttp.proxyHost` and `-Dhttp.proxyPort` as additional Maven arguments (this will affect the entire build) or instruct the plugin to use a proxy when downloading Mongo by adding the `proxyHost` and `proxyPort` configuration properties.
......@@ -32,11 +32,10 @@
</licenses>
<scm>
<url>http://github.com/joelittlejohn/embedmongo-maven-plugin/tree/master</url>
<connection>scm:git:git@github.com:joelittlejohn/embedmongo-maven-plugin.git</connection>
<developerConnection>scm:git:git@github.com:joelittlejohn/embedmongo-maven-plugin.git</developerConnection>
<tag>HEAD</tag>
</scm>
<url>http://github.com/joelittlejohn/embedmongo-maven-plugin/tree/master</url>
<connection>scm:git:git@github.com:joelittlejohn/embedmongo-maven-plugin.git</connection>
<developerConnection>scm:git:git@github.com:joelittlejohn/embedmongo-maven-plugin.git</developerConnection>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
......
......@@ -15,9 +15,18 @@
*/
package com.github.joelittlejohn.embedmongo;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.List;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
......@@ -29,7 +38,6 @@ import de.flapdoodle.embedmongo.MongodProcess;
import de.flapdoodle.embedmongo.config.MongodConfig;
import de.flapdoodle.embedmongo.distribution.Version;
import de.flapdoodle.embedmongo.runtime.Network;
import static java.util.Arrays.*;
/**
* When invoked, this goal starts an instance of mongo. The required binaries
......@@ -69,10 +77,30 @@ public class StartEmbeddedMongoMojo extends AbstractMojo {
*/
private File databaseDirectory;
/**
* 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;
@Override
@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException, MojoFailureException {
if (this.proxyHost != null && this.proxyHost.length() > 0) {
this.addProxySelector();
}
MongodExecutable executable;
try {
executable = MongoDBRuntime.getDefaultInstance().prepare(new MongodConfig(getVersion(), port, Network.localhostIsIPv6(), getDataDirectory()));
......@@ -90,6 +118,24 @@ public class StartEmbeddedMongoMojo extends AbstractMojo {
}
}
private void addProxySelector() {
final ProxySelector defaultProxySelector = ProxySelector.getDefault();
ProxySelector.setDefault(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
if (uri.getHost().equals("fastdl.mongodb.org")) {
return singletonList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
} else {
return defaultProxySelector.select(uri);
}
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
});
}
private Version getVersion() throws MojoExecutionException {
String flapdoodleCompatibleVersionString = this.version.toUpperCase().replaceAll("\\.", "_");
......@@ -100,7 +146,7 @@ public class StartEmbeddedMongoMojo extends AbstractMojo {
try {
return Version.valueOf(flapdoodleCompatibleVersionString);
} catch (IllegalArgumentException e) {
throw new MojoExecutionException("Unrecognised MongoDB version: '" + this.version +
throw new MojoExecutionException("Unrecognised MongoDB version: '" + this.version +
"', try one of the following: \n" + asList(Version.class.getEnumConstants()) + "\n", e);
}
......
......@@ -23,7 +23,7 @@
<plugin>
<groupId>com.github.joelittlejohn.embedmongo</groupId>
<artifactId>embedmongo-maven-plugin</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
<executions>
<execution>
<id>start</id>
......
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