Examples
This section provides practical examples of using Gradle Parser in various scenarios. Each example includes complete, runnable code with explanations.
Available Examples
Basic Usage
- Basic Parsing - Simple file parsing and information extraction
- Dependency Analysis - Working with project dependencies
- Plugin Detection - Analyzing plugins and project types
- Repository Parsing - Extracting repository configurations
Advanced Features
- Structured Editing - Modifying Gradle files programmatically
- Custom Parser - Configuring parser options for specific needs
Running Examples
All examples are designed to be self-contained and runnable. To run an example:
- Create a new Go module:
mkdir gradle-parser-example
cd gradle-parser-example
go mod init example
- Install Gradle Parser:
go get github.com/scagogogo/gradle-parser/pkg/api
Copy the example code into
main.go
Create a sample
build.gradle
file (or use the provided samples)Run the example:
go run main.go
Sample Files
The examples use various sample Gradle files. You can find these in the project repository under examples/sample_files/
:
build.gradle
- Standard Groovy DSL build filebuild.gradle.kts
- Kotlin DSL build fileapp/build.gradle
- Android app modulecommon/build.gradle
- Library modulesettings.gradle
- Multi-module project settings
Example Categories
🔍 Parsing & Analysis
Learn how to parse Gradle files and extract information:
- Project metadata (name, version, group)
- Dependencies with scope analysis
- Plugin configurations
- Repository settings
✏️ Editing & Modification
Discover how to modify Gradle files programmatically:
- Update dependency versions
- Modify plugin configurations
- Add new dependencies
- Preserve formatting and minimize diffs
🛠️ Advanced Usage
Explore advanced features and customization:
- Custom parser configurations
- Source location tracking
- Error handling strategies
- Performance optimization
Common Use Cases
Build Tool Integration
// Parse and analyze a project
result, err := api.ParseFile("build.gradle")
if err != nil {
return err
}
// Check for outdated dependencies
for _, dep := range result.Project.Dependencies {
if isOutdated(dep) {
fmt.Printf("Outdated: %s:%s:%s\n", dep.Group, dep.Name, dep.Version)
}
}
Dependency Management
// Update all Spring Boot dependencies
editor, err := api.CreateGradleEditor("build.gradle")
if err != nil {
return err
}
springBootVersion := "2.7.2"
err = editor.UpdatePluginVersion("org.springframework.boot", springBootVersion)
if err != nil {
return err
}
Project Analysis
// Analyze project type and generate report
plugins, err := api.GetPlugins("build.gradle")
if err != nil {
return err
}
if api.IsAndroidProject(plugins) {
fmt.Println("Android project detected")
// Android-specific analysis
}
Tips for Examples
- Start Simple - Begin with basic parsing examples before moving to advanced features
- Use Real Files - Test examples with actual Gradle files from your projects
- Handle Errors - Always include proper error handling in your code
- Experiment - Modify the examples to explore different scenarios
- Check Output - Verify that parsing results match your expectations
Contributing Examples
Have a useful example to share? We welcome contributions! Please:
- Follow the existing example format
- Include complete, runnable code
- Add clear explanations and comments
- Test with various Gradle file formats
- Submit a pull request with your example
Getting Help
If you have questions about the examples or need help with specific use cases:
- Check the API Reference for detailed documentation
- Visit our GitHub Discussions
- Open an Issue for bugs or feature requests