以下の記事の続きです。
依存ライブラリの一覧まで取れているのだから、それらに対応するPOMファイルの情報を読み取ると、POMファイルに記載されているライセンスやデベロッパー情報などが読み取れるのではないか?
以下のプロジェクトを参考にやってみます。
POMファイルを読み取るにはMavenXpp3Readerを使いますので、
org.apache.maven:maven-modelが必要になります
まずは、pomに対するconfigurationを作成します。
val pomConfigurationName = "pomReleaseRuntimeClasspath"
configurations.create(pomConfigurationName)
val pomConfiguration = configurations[pomConfigurationName] ?: return@register
val configuration = configurations["releaseRuntimeClasspath"] ?: return@register
configuration.resolvedConfiguration
.lenientConfiguration
.allModuleDependencies
.getResolvedArtifacts()
.distinct()
.sortedBy { it.id.displayName }
.forEach { artifact ->
val id = artifact.moduleVersion.id
project.dependencies.add(pomConfigurationName, "${id.group}:${id.name}:${id.version}@pom")
}
getResolvedArtifactsは以下のように定義しておきます。
fun Set<ResolvedDependency>.getResolvedArtifacts(): List<ResolvedArtifact> =
flatMap {
if (it.moduleVersion == "unspecified") {
it.children.getResolvedArtifacts()
} else {
it.allModuleArtifacts
}
}
そうしたら、pomConfigurationからPOMファイルの情報を読み出し、MavenXpp3ReaderでModuleに変換します。
あとはModuleのフィールドアクセスで情報が読み出せます。
val mavenReader = MavenXpp3Reader()
pomConfiguration.resolvedConfiguration
.lenientConfiguration
.artifacts
.filter { it.type == "pom" }
.forEach {
println("name=${it.id.displayName}")
val model = mavenReader.read(ReaderFactory.newXmlReader(it.file), false)
println(" ${model.groupId}:${model.artifactId}:${model.version}")
println(" Project:")
println(" ${model.name}")
println(" ${model.description}")
println(" ${model.url}")
if (model.inceptionYear.isNullOrEmpty()) {
println(" Copyright © XXXX")
} else {
println(" Copyright © ${model.inceptionYear}")
}
println(" License:")
model.licenses.forEach {
println(" ${it.name}")
println(" ${it.url}")
}
println(" Developer:")
model.developers.forEach {
println(" ${it.name}")
}
}
これの出力は以下のようになり、各ライブラリのPOMファイルに記載された各種データを読み出すことができます
name=activity-1.10.1.pom (androidx.activity:activity:1.10.1)
androidx.activity:activity:1.10.1
Project:
Activity
Provides the base Activity subclass and the relevant hooks to build a composable structure on top.
https://developer.android.com/jetpack/androidx/releases/activity#1.10.1
Copyright © 2018
License:
The Apache Software License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0.txt
Developer:
The Android Open Source Project
メタ情報は必須というわけではないので、ライブラリによっては設定されていない場合もありますが、先に紹介したプラグインのような依存ライブラリのライセンス一覧を出力するプラグインを比較的容易に開発できるかもしれないですね。