User1
User1

Reputation: 41221

Adding a Spring dependency into a gradle task

I want to add a special gradle task on my machine as an init script. The script is in ~/.gradle/init.d. Let's call it servertest.gradle. It looks like this:

import org.springframework.http.MediaType

allprojects {
    task servertest {
        doLast {
            MediaType.parseMediaType("application/json")
        }
    }
}

I can run the task, but it says this:

> startup failed:
  initialization script '/home/user1/.gradle/init.d/servertest.gradle': 1: unable to resolve class org.springframework.http.MediaType
   @ line 1, column 1.
     import org.springframework.http.MediaType
     ^

  1 error

Of course, what I really need to do is more complicated than this, but this is a simplified example of using a Spring Library in a Gradle Task.

How do I import the spring libraries into a task?

Upvotes: 2

Views: 449

Answers (1)

Rao
Rao

Reputation: 21379

Basically, dependency missing.

The MediaType class is in spring-web library.

Here is how the gradle file with dependency should look like:

import org.springframework.http.MediaType
initscript {
    repositories {
        mavenCentral()
    }
    dependencies {
       // https://mvnrepository.com/artifact/org.springframework/spring-web
       classpath group: 'org.springframework', name: 'spring-web', version: '4.3.11.RELEASE'
    }
}

allprojects {
    task servertest {
        doLast {
            MediaType.parseMediaType("application/json")
        }
    }
}

Upvotes: 3

Related Questions