Licensee:终极 Gradle 许可证验证插件,保护你的项目免受许可证污染
Licensee:终极 Gradle 许可证验证插件,保护你的项目免受许可证污染
【免费下载链接】licenseeGradle plugin which validates the licenses of your dependency graph match what you expect项目地址: https://gitcode.com/gh_mirrors/lic/licensee
Licensee 是一款强大的 Gradle 插件,能够自动验证项目依赖图中的许可证是否符合预期,防止许可证污染风险。无论是开源项目还是商业应用,都能通过 Licensee 轻松管理依赖许可证,确保项目合规性。
为什么需要 Licensee?🤔
想象一下,你的闭源产品依赖于 Apache 2.0 许可的组件:
implementation 'com.example:example:1.0'当你升级到新版本后:
-implementation 'com.example:example:1.0' +implementation 'com.example:example:1.1'这个新版本可能引入了 GPL 许可的依赖,这与闭源产品不兼容。你可能在依赖树差异中看到了新依赖,但你检查过它的许可证吗?
-+--- com.example:example:1.0 ++--- com.example:example:1.1 + \--- com.other:other:2.5使用 Licensee 插件,你只需简单配置允许的许可证:
licensee { allow('Apache-2.0') }此时构建会立即失败并提示:
> Task :app:licensee FAILED com.other:other:2.5 - SPDX identifier 'GPL-3.0-or-later' is NOT allowed危机轻松化解!
快速开始:3 步集成 Licensee 🚀
第 1 步:添加插件依赖
在项目的build.gradle中添加:
buildscript { repositories { mavenCentral() } dependencies { classpath 'app.cash.licensee:licensee-gradle-plugin:1.14.1' } } apply plugin: 'app.cash.licensee'第 2 步:应用支持的平台插件
Licensee 需要与以下任一插件配合使用:
java-library/javacom.android.application/com.android.libraryorg.jetbrains.kotlin.jvm/org.jetbrains.kotlin.js/org.jetbrains.kotlin.multiplatform
第 3 步:配置允许的许可证
licensee { allow('Apache-2.0') // 允许 Apache 2.0 许可证 allow('MIT') // 允许 MIT 许可证 }核心功能详解 🔍
自动依赖图扫描
插件会自动遍历整个传递依赖图,因此只需应用到"叶子"模块(如应用程序和服务)。对于库项目,则应应用到每个库模块。
灵活的许可证管理
SPDX 许可证标识符
使用 SPDX 标准标识符允许常见许可证:
licensee { allow('Apache-2.0') allow('MIT') allow('BSD-3-Clause') }完整的 SPDX 许可证列表可在 spdx.org/licenses/ 查看。
自定义许可证 URL
对于非 SPDX 标准的许可证,可以直接允许其 URL:
licensee { allowUrl('https://example.com/custom-license.html') { because '公司内部自定义许可证,兼容 Apache-2.0' } }特定依赖豁免
对于缺少或错误许可证信息的依赖,可以单独允许:
licensee { allowDependency('com.jetbrains', 'annotations', '16.0.1') { because 'Apache-2.0,但许可证 URL 存在拼写错误,新版本已修复' } }强大的忽略机制
忽略内部/商业依赖
licensee { // 忽略整个组 ignoreDependencies('com.mycompany.internal') { because '内部闭源库' } // 忽略特定组件 ignoreDependencies('com.example.sdk', 'sdk') { because '商业 SDK,已购买许可' } }传递依赖忽略
危险但有时必要的功能,可忽略整个依赖树分支:
licensee { ignoreDependencies('com.other.sdk', 'sdk') { transitive = true because '商业 SDK 及其依赖' } }详细报告生成
插件会在构建目录下生成报告文件:
artifacts.json
包含依赖图中每个组件的许可证信息的 JSON 文件。validation.txt
纯文本报告,显示每个组件及其许可证是否被允许。
报告默认生成位置:
- 常规项目:
<buildDir>/reports/licensee - Android 项目:
<buildDir>/reports/licensee/<variant name>/
Android 资产集成
对于 Android 项目,可以将许可证报告打包到 APK 资产中:
licensee { bundleAndroidAsset = true androidAssetReportPath = "licenses/artifacts.json" // 自定义路径 }报告将可通过assets/licenses/artifacts.json访问。
高级使用技巧 💡
开发版本使用
如需使用开发快照版本,添加快照仓库:
buildscript { repositories { mavenCentral() maven { url 'https://central.sonatype.com/repository/maven-snapshots/' } } dependencies { classpath 'app.cash.licensee:licensee-gradle-plugin:1.15.0-SNAPSHOT' } }任务集成
licensee任务会自动添加为check任务的依赖,确保每次构建都进行许可证验证。对于 Android 和 Kotlin 多平台项目,会为每个变体生成特定任务(如licenseeDebug)。
许可证数据更新
运行updateLicenses任务可更新嵌入的 SPDX 许可证数据:
./gradlew updateLicenses总结
Licensee 提供了简单而强大的方式来保护你的项目免受许可证冲突的风险。通过自动化依赖许可证验证流程,它让开发者可以专注于功能开发,同时确保项目合规性。无论是小型开源项目还是大型商业应用,Licensee 都是管理依赖许可证的理想选择。
License
Copyright 2021 Square, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.【免费下载链接】licenseeGradle plugin which validates the licenses of your dependency graph match what you expect项目地址: https://gitcode.com/gh_mirrors/lic/licensee
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
