gradle-dependencies-sorter
This JVM CLI app and companion Gradle plugin can sort the dependencies of a build.gradle[.kts] script.
Usage
CLI
./path/to/sort <path(s) that contain build.gradle[.kts] scripts>
# for example, use this to sort the full repo
./path/to/sort .
# for example, use this to sort a sub-tree
./path/to/sort features
# for example, use this to sort individual files
./path/to/sort my/app/build.gradle[.kts] my/app2/build.gradle[.kts]
# for example, use this to sort a subtree and a file
./path/to/sort features my/app2/build.gradle[.kts]
# Check sort status
./path/to/sort -m check <paths as above>
./path/to/sort --mode check <paths as above>
# Sort direct calls in matching Gradle DSL blocks
./path/to/sort --block sqldelight.databases.create .
Gradle plugin
Apply it
// build.gradle[.kts]
plugins {
id("com.squareup.sort-dependencies") version "<<version>>"
}
Sort it
./gradlew :my:app:sortDependencies
Check it
./gradlew :my:app:checkSortDependencies
This task is automatically added as a dependency to the check task.
Configure it
sortDependencies {
// Defines a custom version of the SortDependencies CLI to use
version.set(/* custom version */)
// When true, a blank line will be inserted between dependencies of different configurations. Enabled by default.
insertBlankLines = false
// Sort direct calls in matching Gradle DSL blocks.
blocks("sqldelight.databases.create")
// true by default, meaning that 'checkSortDependencies' is a dependency of 'check'
check(true)
}
Dependency declarations inside any dependencies {} block are always sorted, including nested blocks such as kotlin.sourceSets.commonMain.dependencies. Calls inside dependencies.constraints {} are also sorted without any additional configuration. Other Gradle DSL blocks must be configured with blocks(...) or the CLI's --block option.
For example, blocks("sqldelight.databases.create") changes this:
sqldelight {
databases {
create("Database") {
dependency(project(":schema:shared"))
dependency(project(":schema:analytics"))
}
}
}
to this:
sqldelight {
databases {
create("Database") {
dependency(project(":schema:analytics"))
dependency(project(":schema:shared"))
}
}
}
sqldelight.databases.create matches both create("main") {} and create<Database>("main") {}, including when they are nested inside another block such as subprojects {}.
Pass several paths to blocks(...) to configure more than one kind of block.
Only calls directly inside the matched block are sorted. Other statements stay in place.
For SQLDelight's Groovy Database {} syntax, you would use blocks("sqldelight.databases.Database") instead.
Test it
./gradlew check
CLI
A fat jar of the CLI is available on Maven Central. Replace $version with the latest version.
https://repo1.maven.org/maven2/com/squareup/sort-gradle-dependencies-app/$version/sort-gradle-dependencies-app-$version-all.jar
Build it
Publish everything to maven local
./gradlew publishToMavenLocal
This will push all of this project's artifacts to ~/.m2/repository/.
Build zip distribution
./gradlew :app:shadowDistZip
This creates a zip file of the distribution at app/build/distributions/. This archive may be installed anywhere you
like.
Install CLI app
./gradlew :app:installShadowDist
This will install the app to app/build/install/app-shadow/.
Pre-OSS Contributors
License
Copyright 2022 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.
