Skip to content

Instantly share code, notes, and snippets.

@skhatri
Created March 13, 2023 02:01
Show Gist options
  • Save skhatri/d7d3b00188471a994f1b6693c5d84234 to your computer and use it in GitHub Desktop.
Save skhatri/d7d3b00188471a994f1b6693c5d84234 to your computer and use it in GitHub Desktop.
Init Extra Gradle Tasks
def getDependencyList(org.gradle.api.Project proj, String configName) {
def dep = []
def dd = new HashSet<String>()
proj.configurations.all { cf ->
if (cf.canBeResolved && cf.name == configName) {
cf.incoming.each { p ->
p.dependencies.each { dt ->
dd.add("$dt.group:$dt.name:$dt.version".toString())
dep << [group: dt.group, name: dt.name, version: dt.version, transitive: false]
}
p.artifacts.each { at ->
String display = at.id.componentIdentifier.toString()
if (!dd.contains(display)) {
String[] parts = display.split(":")
dep << [group: parts[0], name: parts[1], version: parts[2], transitive: true]
}
}
}
}
}
return dep
}
def getDependency(org.gradle.api.Project proj, String configName, String group, String module) {
def dep = []
def dd = new HashSet<String>()
proj.configurations.all { cf ->
if (cf.canBeResolved && cf.name == configName) {
cf.incoming.each { p ->
p.dependencies.each { dt ->
if (group == dt.group && module == dt.name) {
dd.add("$dt.group:$dt.name:$dt.version".toString())
dep << [group: dt.group, name: dt.name, version: dt.version, transitive: false]
}
}
p.artifacts.each { at ->
String display = at.id.componentIdentifier.toString()
String[] parts = display.split(":")
if (group == parts[0] && module == parts[1]) {
if (!dd.contains(display)) {
dep << [group: parts[0], name: parts[1], version: parts[2], transitive: true]
}
}
}
}
}
}
return dep
}
def postRequest(String target, String data, String intent) {
try (def post = new URL(target).openConnection()){
post.setRequestMethod('POST')
post.setDoOutput(true)
post.setRequestProperty('Content-Type', 'application/json')
post.getOutputStream().write(data.getBytes('UTF-8'))
def rs = post.getResponseCode()
if (rs.equals(200)) {
logger.quiet("$intent data uploaded")
} else {
logger.warn("$intent data could not be uploaded")
}
} catch (e) {
logger.error("error sending $intent data to ${target} \n Error: ${e}")
}
}
allprojects {
tasks.register('exportDependencies') {
doLast {
def dependencyList= getDependencyList(project, "runtimeClasspath")
def exportDep= [:]
exportDep["project"] = project.name
exportDep["version"] = project.version
exportDep["dependencies"] = dependencyList
def json = new groovy.json.JsonBuilder()
json exportDep
postRequest("http://localhost:8090/ingest/events", json.toString(), "dependency")
}
}
tasks.register('exportFramework') {
doLast {
def jvmVersion = project.targetCompatibility
def deps = getDependency(project, "runtimeClasspath", "org.springframework.boot", "spring-boot-starter")
def majorVersion = 0
def matching = deps.findAll { dep ->
def matcher = dep["version"] =~ /^(\d+)\.*+\.*+/
if (matcher.hasGroup()) {
def ver = matcher[0][1] as int
if (ver > majorVersion) {
majorVersion = ver
}
}
}
def json = new groovy.json.JsonBuilder()
def exportDep = [:]
exportDep["spring_boot_version"] = majorVersion
exportDep["java_version"] = "${jvmVersion}"
exportDep["project"] = "java upgrade"
exportDep["app_name"] = project.name
exportDep["app_version"] = project.version
json exportDep
logger.quiet("${json.toString()}")
postRequest("http://localhost:8090/ingest/events", json.toString(), "framework")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment