diff --git a/README.md b/README.md index e14bf3e558be3fae9745d868ae1624abef0eb522..cdb46637cf5d7f99327b333db9ec74db937b2fd1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/pom.xml b/pom.xml index 3de50014b534d6d1d3fd9fe623536aeda6188df6..37e39a09dedf28b90867eec10013c3233d294092 100644 --- a/pom.xml +++ b/pom.xml @@ -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> diff --git a/src/main/java/com/github/joelittlejohn/embedmongo/StartEmbeddedMongoMojo.java b/src/main/java/com/github/joelittlejohn/embedmongo/StartEmbeddedMongoMojo.java index 5c76e86025fb91cb4a11b306274c484778d44690..4f8ad6b4e93074dd5266f2ae4be4486853a0f111 100644 --- a/src/main/java/com/github/joelittlejohn/embedmongo/StartEmbeddedMongoMojo.java +++ b/src/main/java/com/github/joelittlejohn/embedmongo/StartEmbeddedMongoMojo.java @@ -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); } diff --git a/src/test/resources/example/pom.xml b/src/test/resources/example/pom.xml index 61053d1cd19349ce9d36693f6bee3f836492e01a..3f016fc96edb7039930980d69b81767ac7db4601 100644 --- a/src/test/resources/example/pom.xml +++ b/src/test/resources/example/pom.xml @@ -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>