Heterogeneous thoughts on software.

Relay Maven Over Ssh Using Vertx

The following Groovy 2.0 script is a mechanism for providing http access to file inside a network to which you only have ssh access.

Vertx Maven RelaySource
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Grab(group='org.vert-x', module='vertx-lang-groovy', version='1.3.1.final')

import org.vertx.groovy.core.Vertx

def sshCommand = 'ssh root@dcmcfarland.com'
def targetUrl = 'http://repo1.maven.org/maven2/'

def vertx = Vertx.newVertx()
def rand = new Random()

println "Starting maven ssh relay for $targetUrl"

vertx.createHttpServer().requestHandler { request ->
  println "Downloading: $targetUrl$request.uri"
  def targetFile = new File("/tmp/" + rand.nextLong())

  ["sh", "-c", "$sshCommand 
        'wget -O - $targetUrl$request.uri' > ${targetFile.absolutePath}"].execute().text

  request.response.sendFile targetFile.absolutePath
  targetFile.delete()
}.listen(8888)

// Don't kill main thread until application is terminated.
synchronized (vertx) {
  vertx.wait();
}

An example use case

  1. Configure your build or maven setting by adding localhost:8888 as a new repository.
  2. Set targetUrl to the base location of your internal maven repository i.e. http://repo1.maven.org/maven2/
  3. Update sshCommand providing your username and server location i.e. ssh root@dcmcfarland
  4. Run: groovy VertxMavenRelay.groovy
  5. To test everything is working access http://repo1.maven.org/maven2/com/google/inject/guice/ open your browser and go to http://localhost:8888/com/google/inject/guice/ the file listings for guice should display. If you now run a maven build maven will be able to access files in the internal repository.

N.B. If ssh-keys have not been configured to provide passwordless ssh access, the terminal in which VertxMavenRelay is running will request your password every time a http request is made.

See you next time.