A powerful Go library for parsing and manipulating Gradle build files. Extract dependencies, plugins, repositories, and other configuration data with ease. Features structured editing capabilities for programmatic modifications while preserving original formatting.
π Complete Documentation: https://scagogogo.github.io/gradle-parser/
π δΈζζζ‘£ | π Getting Started | π API Reference | π‘ Examples
- Parse Gradle build files (supports both
build.gradle
andbuild.gradle.kts
) - Extract project metadata (group, name, version, description)
- Parse and categorize dependencies with scope classification
- Analyze plugin configurations and detect project types (Android/Kotlin/Spring Boot)
- Process repository configurations including custom repositories and authentication
- Precise modifications: Update dependency versions, plugin versions, project properties
- Minimal diff: Preserve original formatting, modify only necessary parts
- Source location tracking: Record exact positions of elements in source files
- Batch operations: Apply multiple modifications in a single operation
- Support for both Groovy DSL and Kotlin DSL syntax
- Customizable parser configuration for different requirements
- Multi-module Gradle project support
- Comprehensive error handling and validation
go get github.com/scagogogo/gradle-parser/pkg/api
package main
import (
"fmt"
"github.com/scagogogo/gradle-parser/pkg/api"
)
func main() {
// Parse a Gradle file
result, err := api.ParseFile("path/to/build.gradle")
if err != nil {
panic(err)
}
// Access project information
project := result.Project
fmt.Printf("Project: %s\n", project.Name)
fmt.Printf("Group: %s\n", project.Group)
fmt.Printf("Version: %s\n", project.Version)
// List dependencies
for _, dep := range project.Dependencies {
fmt.Printf("Dependency: %s:%s:%s (%s)\n",
dep.Group, dep.Name, dep.Version, dep.Scope)
}
// List plugins
for _, plugin := range project.Plugins {
fmt.Printf("Plugin: %s", plugin.ID)
if plugin.Version != "" {
fmt.Printf(" v%s", plugin.Version)
}
fmt.Println()
}
// List repositories
for _, repo := range project.Repositories {
fmt.Printf("Repository: %s (%s)\n", repo.Name, repo.URL)
}
}
// Extract dependencies directly
dependencies, err := api.GetDependencies("build.gradle")
if err != nil {
log.Fatal(err)
}
// Group dependencies by scope
dependencySets := api.DependenciesByScope(dependencies)
for _, set := range dependencySets {
fmt.Printf("Scope: %s\n", set.Scope)
for _, dep := range set.Dependencies {
fmt.Printf(" %s:%s:%s\n", dep.Group, dep.Name, dep.Version)
}
}
// Extract plugin information
plugins, err := api.GetPlugins("build.gradle")
if err != nil {
log.Fatal(err)
}
// Detect project types
if api.IsAndroidProject(plugins) {
fmt.Println("Android project detected")
}
if api.IsKotlinProject(plugins) {
fmt.Println("Kotlin project detected")
}
if api.IsSpringBootProject(plugins) {
fmt.Println("Spring Boot project detected")
}
// Extract repository configurations
repos, err := api.GetRepositories("build.gradle")
if err != nil {
log.Fatal(err)
}
for _, repo := range repos {
fmt.Printf("Repository: %s (%s)\n", repo.Name, repo.URL)
}
// Simple version updates
newText, err := api.UpdateDependencyVersion("build.gradle", "mysql", "mysql-connector-java", "8.0.31")
if err != nil {
log.Fatal(err)
}
newText, err = api.UpdatePluginVersion("build.gradle", "org.springframework.boot", "2.7.2")
if err != nil {
log.Fatal(err)
}
// Create an editor for batch modifications
editor, err := api.CreateGradleEditor("build.gradle")
if err != nil {
log.Fatal(err)
}
// Perform multiple modifications
editor.UpdateProperty("version", "1.0.0")
editor.UpdatePluginVersion("org.springframework.boot", "2.7.2")
editor.UpdateDependencyVersion("com.google.guava", "guava", "31.1-jre")
editor.AddDependency("org.apache.commons", "commons-text", "1.9", "implementation")
// Apply all modifications
serializer := editor.NewGradleSerializer(editor.GetSourceMappedProject().OriginalText)
finalText, err := serializer.ApplyModifications(editor.GetModifications())
if err != nil {
log.Fatal(err)
}
// Generate diff for review
diffLines := serializer.GenerateDiff(editor.GetModifications())
for _, diffLine := range diffLines {
fmt.Println(diffLine.String())
}
// Create custom parser options
options := api.DefaultOptions()
options.SkipComments = true
options.ParsePlugins = true
options.ParseDependencies = true
options.ParseRepositories = true
// Create custom parser
parser := api.NewParser(options)
result, err := parser.ParseFile("build.gradle")
if err != nil {
log.Fatal(err)
}
The project uses a modular design with clean separation of concerns:
βββ pkg/ # Core packages
β βββ api/ # Main API interface
β βββ config/ # Configuration parsing
β βββ dependency/ # Dependency parsing
β βββ editor/ # Structured editor
β βββ model/ # Data models
β βββ parser/ # Parser core
β βββ util/ # Utility functions
βββ examples/ # Example code
βββ 01_basic/ # Basic usage
βββ 02_dependencies/ # Dependency extraction
βββ 03_plugins/ # Plugin extraction
βββ 04_repositories/ # Repository extraction
βββ 05_complete/ # Complete features
βββ 06_editor/ # Structured editing
βββ sample_files/ # Sample Gradle files
Explore the examples directory for comprehensive code samples demonstrating different features:
- Basic Usage: Simple parsing and data extraction
- Advanced Features: Complex parsing scenarios and customization
- Structured Editing: Programmatic file modifications
- Project Analysis: Complete project inspection and reporting
Run the comprehensive test suite:
# Run all tests
go test ./...
# Run test suite with coverage
cd test && ./scripts/run-tests.sh
# Run examples
cd examples && ./run-all-examples.sh
Test Coverage: Target >90%
This project uses GitHub Actions for comprehensive quality assurance:
- π CI: Multi-version Go testing, code validation, example verification
- π Quality: Code coverage, security scanning, complexity analysis
- π Docs: Documentation building and deployment
- π Release: Automated releases and asset building
Quality Standards:
- β Unit and integration tests
- π Code quality checks (golangci-lint)
- π‘οΈ Security vulnerability scanning
- π Performance benchmarking
- π Documentation link validation
Thanks to all the amazing people who have contributed to this project! π
CC11001100 π» π π¨ π§ |
Adam Korczynski π π§ |
- π» Code
- π Documentation
- π¨ Design
- π§ Maintenance
- π Bug fixes
- π§ Tools
Contributions are welcome! Please see the Contributing Guide for details.
# Clone the repository
git clone https://github.com/scagogogo/gradle-parser.git
cd gradle-parser
# Install dependencies
go mod download
# Run tests
go test ./...
# Try examples
cd examples/01_basic && go run main.go
Found a bug or have a feature request? Please report it in GitHub Issues.
- Enhanced Kotlin DSL support
- Performance optimizations
- More editing capabilities
- IDE plugin support
- Additional Gradle DSL syntax support
MIT License - See LICENSE file for details.
β If this project helps you, please give it a star!
Made with β€οΈ by scagogogo