Python脚本输出build.Gradle文件的依赖项 [英] Python Script to out put dependencies of build.Gradle File
问题描述
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')
}
我正在尝试读取build.gradle文件,并且仅输出.gradle文件的依赖项.我需要输出为json格式,以便我可以填充数据库.理想情况下
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.
推荐答案
build.gradle
文件是一个脚本,我建议您不要解析脚本文件,因为它没有稳定的数据结构
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?
我认为通过执行gradle命令添加依赖项的最佳方法是:
I think the best way to add dependencies by execute gradle command, something like:
import os
os.system('./gradlew app:dependencies --configuration implementation')
您可以找到gradle插件,这些插件可以帮助我们将依赖项检索到项目中.然后通过python脚本执行gradle命令.
You can find gradle plugins which help we retrieve dependencies into project instead. Then execute gradle command via python script.
这篇关于Python脚本输出build.Gradle文件的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!