0

My java application produces some visualisations (html, xml+xls) as outputs that cannot be served from the filesystem due to browser safeguards.

I want to make a commant like ./gradlew view that would serve those for me. For example in npm one of the ways is to use webpack-server or some other dependency then run it.

Is there an established way to run a webserver with gradle?

ttzn
  • 2,543
  • 22
  • 26
gcasar
  • 729
  • 1
  • 9
  • 22

1 Answers1

1

I think you should take a look at Ratpack. It can easily be used in a gradle script using the following configurations:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath "io.ratpack:ratpack-gradle:1.7.0"
  }
}

apply plugin: "io.ratpack.ratpack-groovy"
apply plugin: "idea"

repositories {
  jcenter()
}

dependencies {
  runtime "org.slf4j:slf4j-simple:1.7.25"
}

and then you can spin up a simple web server like this:

import static ratpack.groovy.Groovy.ratpack

ratpack {
    handlers {
        get {
            render "Hello World!"
        }
        get(":name") {
            render "Hello $pathTokens.name!"
        }
    }
}

We use it mainly for testing APIs. If you want to serve static contents, you should take a look at asset-pipeline project in GitHub.

zaerymoghaddam
  • 3,037
  • 1
  • 27
  • 33