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格式,这样我就可以填充数据库。理想情况下就像
org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}
org.springframework.boot:spring-boot-starter-web
org.springframework.boot:spring-boot-starter-test我尝试了许多方法,但似乎无法获得所需的输出。提前谢谢。
发布于 2019-06-18 13:05:55
build.gradle文件是一个脚本,我建议你不要解析脚本文件,因为它没有稳定的数据结构。
例如:你必须使用正则表达式来替换字符串中的数据,有时用户定义了一个与正则表达式模式相同的变量。
我认为通过执行gradle命令添加依赖项的最好方法如下:
import os
os.system('./gradlew app:dependencies --configuration implementation')你可以找到gradle插件,它们可以帮助我们将依赖项检索到项目中。然后通过python脚本执行gradle命令。
https://stackoverflow.com/questions/54465981
复制相似问题