Data Models
This section documents the data structures used to represent parsed Gradle projects. These models provide a structured way to access project information, dependencies, plugins, and other build configuration.
Core Models
Project
Represents a complete Gradle project with all its components.
type Project struct {
// Basic project information
Group string `json:"group"`
Name string `json:"name"`
Version string `json:"version"`
Description string `json:"description"`
// Java/JVM configuration
SourceCompatibility string `json:"sourceCompatibility"`
TargetCompatibility string `json:"targetCompatibility"`
// Custom properties
Properties map[string]string `json:"properties"`
// Project components
Plugins []*Plugin `json:"plugins"`
Dependencies []*Dependency `json:"dependencies"`
Repositories []*Repository `json:"repositories"`
SubProjects []*Project `json:"subProjects"`
Tasks []*Task `json:"tasks"`
Extensions map[string]any `json:"extensions"`
// File information
FilePath string `json:"filePath"`
}
Fields:
Group
: Maven group ID (e.g., "com.example")Name
: Project nameVersion
: Project versionDescription
: Project descriptionSourceCompatibility
: Java source compatibility versionTargetCompatibility
: Java target compatibility versionProperties
: Custom project propertiesPlugins
: List of applied pluginsDependencies
: List of project dependenciesRepositories
: List of configured repositoriesSubProjects
: Sub-projects in multi-module setupTasks
: Custom tasks defined in the buildExtensions
: Plugin extensions and configurationsFilePath
: Path to the source build file
Dependency
Represents a project dependency.
type Dependency struct {
Group string `json:"group"`
Name string `json:"name"`
Version string `json:"version"`
Scope string `json:"scope"`
Transitive bool `json:"transitive"`
Raw string `json:"raw"`
}
Fields:
Group
: Maven group ID (e.g., "org.springframework")Name
: Artifact name (e.g., "spring-core")Version
: Version string (e.g., "5.3.21")Scope
: Dependency scope (e.g., "implementation", "testImplementation")Transitive
: Whether transitive dependencies are includedRaw
: Original dependency declaration from build file
Example:
// Dependency: org.springframework:spring-core:5.3.21
dep := &Dependency{
Group: "org.springframework",
Name: "spring-core",
Version: "5.3.21",
Scope: "implementation",
Raw: "implementation 'org.springframework:spring-core:5.3.21'",
}
Plugin
Represents a Gradle plugin configuration.
type Plugin struct {
ID string `json:"id"`
Version string `json:"version,omitempty"`
Apply bool `json:"apply"`
Config map[string]interface{} `json:"config,omitempty"`
}
Fields:
ID
: Plugin identifier (e.g., "java", "org.springframework.boot")Version
: Plugin version (optional)Apply
: Whether the plugin is applied (true by default)Config
: Plugin-specific configuration
Example:
// Plugin: id 'org.springframework.boot' version '2.7.0'
plugin := &Plugin{
ID: "org.springframework.boot",
Version: "2.7.0",
Apply: true,
}
Repository
Represents a repository configuration.
type Repository struct {
Name string `json:"name"`
URL string `json:"url"`
Type string `json:"type"`
}
Fields:
Name
: Repository name (e.g., "mavenCentral", "google")URL
: Repository URLType
: Repository type (e.g., "maven", "ivy")
Example:
// Repository: mavenCentral()
repo := &Repository{
Name: "mavenCentral",
URL: "https://repo1.maven.org/maven2/",
Type: "maven",
}
Task
Represents a Gradle task definition.
type Task struct {
Name string `json:"name"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Group string `json:"group,omitempty"`
DependsOn []string `json:"dependsOn,omitempty"`
Config map[string]interface{} `json:"config,omitempty"`
}
Fields:
Name
: Task nameType
: Task type (e.g., "Copy", "Jar")Description
: Task descriptionGroup
: Task group for organizationDependsOn
: List of task dependenciesConfig
: Task-specific configuration
Result Models
ParseResult
Contains the complete result of parsing a Gradle file.
type ParseResult struct {
Project *Project `json:"project"`
RawText string `json:"rawText,omitempty"`
Errors []error `json:"errors,omitempty"`
Warnings []string `json:"warnings,omitempty"`
ParseTime string `json:"parseTime,omitempty"`
}
Fields:
Project
: Parsed project informationRawText
: Original file content (if collection enabled)Errors
: Fatal parsing errorsWarnings
: Non-fatal parsing warningsParseTime
: Time taken to parse the file
DependencySet
Groups dependencies by scope for easier analysis.
type DependencySet struct {
Scope string `json:"scope"`
Dependencies []*Dependency `json:"dependencies"`
}
Fields:
Scope
: Dependency scope (e.g., "implementation", "testImplementation")Dependencies
: List of dependencies in this scope
Source Mapping Models
For advanced use cases requiring precise source location tracking:
SourceMappedProject
Extended project model with source location information.
type SourceMappedProject struct {
*Project
// Source mapping information
SourceMappedDependencies []*SourceMappedDependency
SourceMappedPlugins []*SourceMappedPlugin
SourceMappedProperties []*SourceMappedProperty
SourceMappedRepositories []*SourceMappedRepository
// Original file information
FilePath string
OriginalText string
Lines []string
}
SourcePosition
Represents a position in the source file.
type SourcePosition struct {
Line int `json:"line"`
Column int `json:"column"`
Start int `json:"start"`
End int `json:"end"`
Length int `json:"length"`
}
SourceRange
Represents a range in the source file.
type SourceRange struct {
Start SourcePosition `json:"start"`
End SourcePosition `json:"end"`
}
Usage Examples
Accessing Project Information
result, err := api.ParseFile("build.gradle")
if err != nil {
log.Fatal(err)
}
project := result.Project
fmt.Printf("Project: %s\n", project.Name)
fmt.Printf("Group: %s\n", project.Group)
fmt.Printf("Version: %s\n", project.Version)
Working with Dependencies
for _, dep := range project.Dependencies {
fmt.Printf("Dependency: %s:%s", dep.Group, dep.Name)
if dep.Version != "" {
fmt.Printf(":%s", dep.Version)
}
fmt.Printf(" (%s)\n", dep.Scope)
}
Analyzing Plugins
for _, plugin := range project.Plugins {
fmt.Printf("Plugin: %s", plugin.ID)
if plugin.Version != "" {
fmt.Printf(" v%s", plugin.Version)
}
if !plugin.Apply {
fmt.Printf(" (not applied)")
}
fmt.Println()
}
Checking Repositories
for _, repo := range project.Repositories {
fmt.Printf("Repository: %s", repo.Name)
if repo.URL != "" {
fmt.Printf(" (%s)", repo.URL)
}
fmt.Println()
}
JSON Serialization
All models support JSON serialization for easy integration with web APIs and data storage:
result, err := api.ParseFile("build.gradle")
if err != nil {
log.Fatal(err)
}
// Serialize to JSON
jsonData, err := json.MarshalIndent(result.Project, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(jsonData))
Type Safety
All models use Go's type system to ensure data integrity:
- String fields for textual data
- Slices for collections
- Maps for key-value configurations
- Pointers for optional relationships
- Interfaces for extensible configurations