upcrob
upcrob

Reputation: 165

Spring Boot with Groovy and External Libraries

I'm working on building what is effectively a throwaway Spring Boot application. Using the CLI, I can get a basic page up and working (see https://spring.io/guides/gs/spring-boot/). What I haven't figured out how to do, however, is how I can add external dependencies (ie. third-party JAR files) to the compile or runtime classpath when I use either the "spring run" or "spring jar" commands. Note that these external dependencies are local to my computer and are not stored in an artifact repository. Is there a simple way to do this?

Upvotes: 0

Views: 675

Answers (2)

Andy Wilkinson
Andy Wilkinson

Reputation: 116091

If the jars aren't in an artifact repository, the easiest way to add them to the classpath is to use -cp when running your app or creating its jar.

For example:

spring run -cp foo.jar app.groovy

Or:

spring jar -cp foo.jar app.jar app.groovy

In the spring jar case, anything that's added to the classpath using -cp will be packaged inside the resulting jar (app.jar in this case) ensuring that it's self-contained.

Upvotes: 3

cfrick
cfrick

Reputation: 37008

you can use groovy's @Grab notation (there is also spring grab call). E.g.

@Grab('joda-time:joda-time:2.5')

@RestController
class ThisWillActuallyRun {

    @RequestMapping("/")
    String home() {
        return new org.joda.time.DateTime().toString()
    }

}

Upvotes: 1

Related Questions