Python Script to out put dependencies of build.Gradle File

孤街浪徒 提交于 2020-02-25 04:10:11

问题


My Python Script
import re
filename = "build.gradle"
pattern = re.compile(r"dependencies", re.IGNORECASE)
with open(filename, "rt") as in_file:
 for line in in_file:
  if pattern.search(line) != None:
      #for line in in_file:
          #print(line)




   print(line, end='')

My Build.Gradle File
buildscript {
    ext {
        springBootVersion = '2.1.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

I am trying to read in the build.gradle file, and output only the dependencies of the .gradle file. I need the out put to be json format so i can populate a database. ideally like

org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}
org.springframework.boot:spring-boot-starter-web
org.springframework.boot:spring-boot-starter-test

I have tried numerous ways but cannot seem to get the output i require. Thanks in advance.


回答1:


build.gradle file is a script, I recommended you shouldn't parsing a script file because it not have stable structure of data.

For example: You have to regex to replace data from string, sometime user define a variable same name match the regex patten?

I think the best way to add dependencies by execute gradle command, something like:

import os
os.system('./gradlew app:dependencies --configuration implementation')

You can find gradle plugins which help we retrieve dependencies into project instead. Then execute gradle command via python script.



来源:https://stackoverflow.com/questions/54465981/python-script-to-out-put-dependencies-of-build-gradle-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!