From dd102b8fe11e0df55d8fc9b805605ce8d04166cf Mon Sep 17 00:00:00 2001 From: haozhang Date: Thu, 14 Nov 2024 20:31:31 +0800 Subject: [PATCH 1/5] prompt if detected kafka but no spring-cloud-azure found, can help to add this dep --- cli/azd/internal/appdetect/appdetect.go | 16 ++- cli/azd/internal/appdetect/java.go | 49 ++++++-- cli/azd/internal/repository/app_init.go | 142 +++++++++++++++++++++++- 3 files changed, 197 insertions(+), 10 deletions(-) diff --git a/cli/azd/internal/appdetect/appdetect.go b/cli/azd/internal/appdetect/appdetect.go index 480f9ad865f..75007fa97f9 100644 --- a/cli/azd/internal/appdetect/appdetect.go +++ b/cli/azd/internal/appdetect/appdetect.go @@ -150,8 +150,9 @@ func (a AzureDepServiceBus) ResourceDisplay() string { } type AzureDepEventHubs struct { - Names []string - UseKafka bool + Names []string + UseKafka bool + SpringBootVersion string } func (a AzureDepEventHubs) ResourceDisplay() string { @@ -166,6 +167,14 @@ func (a AzureDepStorageAccount) ResourceDisplay() string { return "Azure Storage Account" } +type SpringCloudAzureDep struct { + Version string +} + +func (a SpringCloudAzureDep) ResourceDisplay() string { + return "Spring Cloud Azure Starter" +} + type Project struct { // The language associated with the project. Language Language @@ -182,6 +191,9 @@ type Project struct { // The path to the project directory. Path string + // The package file relative path to the project path + PackageFileRelPath string + // A short description of the detection rule applied. DetectionRule string diff --git a/cli/azd/internal/appdetect/java.go b/cli/azd/internal/appdetect/java.go index f725af813a2..0a0a387b61e 100644 --- a/cli/azd/internal/appdetect/java.go +++ b/cli/azd/internal/appdetect/java.go @@ -52,10 +52,11 @@ func (jd *javaDetector) DetectProject(ctx context.Context, path string, entries } _ = currentRoot // use currentRoot here in the analysis - result, err := detectDependencies(project, &Project{ - Language: Java, - Path: path, - DetectionRule: "Inferred by presence of: pom.xml", + result, err := detectDependencies(currentRoot, project, &Project{ + Language: Java, + Path: path, + PackageFileRelPath: entry.Name(), + DetectionRule: "Inferred by presence of: pom.xml", }) if err != nil { return nil, fmt.Errorf("detecting dependencies: %w", err) @@ -128,7 +129,7 @@ func readMavenProject(filePath string) (*mavenProject, error) { return &project, nil } -func detectDependencies(mavenProject *mavenProject, project *Project) (*Project, error) { +func detectDependencies(currentRoot *mavenProject, mavenProject *mavenProject, project *Project) (*Project, error) { // how can we tell it's a Spring Boot project? // 1. It has a parent with a groupId of org.springframework.boot and an artifactId of spring-boot-starter-parent // 2. It has a dependency with a groupId of org.springframework.boot and an artifactId that starts with @@ -145,7 +146,9 @@ func detectDependencies(mavenProject *mavenProject, project *Project) (*Project, } } applicationProperties := make(map[string]string) + var springBootVersion string if isSpringBoot { + springBootVersion = detectSpringBootVersion(currentRoot, mavenProject) applicationProperties = readProperties(project.Path) } @@ -232,8 +235,15 @@ func detectDependencies(mavenProject *mavenProject, project *Project) (*Project, } } project.AzureDeps = append(project.AzureDeps, AzureDepEventHubs{ - Names: destinations, - UseKafka: true, + Names: destinations, + UseKafka: true, + SpringBootVersion: springBootVersion, + }) + } + + if dep.GroupId == "com.azure.spring" && dep.ArtifactId == "spring-cloud-azure-starter" { + project.AzureDeps = append(project.AzureDeps, SpringCloudAzureDep{ + Version: dep.Version, }) } } @@ -248,6 +258,31 @@ func detectDependencies(mavenProject *mavenProject, project *Project) (*Project, return project, nil } +func detectSpringBootVersion(currentRoot *mavenProject, mavenProject *mavenProject) string { + if currentRoot != nil { + if currentRoot.Parent.ArtifactId == "spring-boot-starter-parent" { + return currentRoot.Parent.Version + } else { + for _, dep := range currentRoot.DependencyManagement.Dependencies { + if dep.ArtifactId == "spring-boot-dependencies" { + return dep.Version + } + } + } + } else { + if mavenProject.Parent.ArtifactId == "spring-boot-starter-parent" { + return mavenProject.Parent.Version + } else { + for _, dep := range mavenProject.DependencyManagement.Dependencies { + if dep.ArtifactId == "spring-boot-dependencies" { + return dep.Version + } + } + } + } + return "unknown" +} + func readProperties(projectPath string) map[string]string { // todo: do we need to consider the bootstrap.properties result := make(map[string]string) diff --git a/cli/azd/internal/repository/app_init.go b/cli/azd/internal/repository/app_init.go index 388117bd7c5..19391ce2f04 100644 --- a/cli/azd/internal/repository/app_init.go +++ b/cli/azd/internal/repository/app_init.go @@ -2,6 +2,7 @@ package repository import ( "context" + "errors" "fmt" "maps" "os" @@ -128,10 +129,32 @@ func (i *Initializer) InitFromApp( i.console.StopSpinner(ctx, title, input.StepDone) var prjAppHost []appdetect.Project - for _, prj := range projects { + for index, prj := range projects { if prj.Language == appdetect.DotNetAppHost { prjAppHost = append(prjAppHost, prj) } + + if prj.Language == appdetect.Java { + var hasKafkaDep bool + var springBootVersion string + var hasSpringCloudAzureDep bool + for _, dep := range prj.AzureDeps { + if eventHubs, ok := dep.(appdetect.AzureDepEventHubs); ok && eventHubs.UseKafka { + hasKafkaDep = true + springBootVersion = eventHubs.SpringBootVersion + } + if _, ok := dep.(appdetect.SpringCloudAzureDep); ok { + hasSpringCloudAzureDep = true + } + } + + if hasKafkaDep && !hasSpringCloudAzureDep { + err := processSpringCloudAzureDepByPrompt(i.console, ctx, &projects[index], springBootVersion) + if err != nil { + return err + } + } + } } if len(prjAppHost) > 1 { @@ -600,3 +623,120 @@ func (i *Initializer) prjConfigFromDetect( return config, nil } + +func processSpringCloudAzureDepByPrompt(console input.Console, ctx context.Context, project *appdetect.Project, springBootVersion string) error { + continueOption, err := console.Select(ctx, input.ConsoleOptions{ + Message: "Detected Kafka dependency but no spring-cloud-azure-starter found. Select an option", + Options: []string{ + "Exit then I will manually add this dependency", + "Continue and help me add this dependency", + "Continue and do not add this dependency", + "Continue and do not provision Azure Event Hubs for Kafka", + }, + }) + if err != nil { + return err + } + + switch continueOption { + case 0: + return errors.New("you have to manually add dependency com.azure.spring:spring-cloud-azure-starter by following https://github.com/Azure/azure-sdk-for-java/wiki/Spring-Versions-Mapping") + case 1: + // determine spring cloud azure version by spring boot version + version := "" + if strings.HasPrefix(springBootVersion, "2.") { + version = "4.19.0" + } else if strings.HasPrefix(springBootVersion, "3.") { + version = "5.18.0" + } else { + version, err = promptSpringBootVersion(console, ctx) + if err != nil { + return err + } + } + // append spring cloud azure dependency + packageFile := filepath.Join(project.Path, project.PackageFileRelPath) + err := appendSpringCloudAzureDep(packageFile, version) + if err != nil { + return err + } + return nil + case 2: + return nil + case 3: + // remove Kafka Azure Dep + var result []appdetect.AzureDep + for _, dep := range project.AzureDeps { + if eventHubs, ok := dep.(appdetect.AzureDepEventHubs); !(ok && eventHubs.UseKafka) { + result = append(result, dep) + } + } + project.AzureDeps = result + return nil + } + return nil +} + +func promptSpringBootVersion(console input.Console, ctx context.Context) (string, error) { + selection, err := console.Select(ctx, input.ConsoleOptions{ + Message: "No spring boot version detected, what is your spring boot version?", + Options: []string{ + "Spring Boot 2.x", + "Spring Boot 3.x", + }, + }) + if err != nil { + return "", err + } + + switch selection { + case 0: + return "2.x", nil + case 1: + return "3.x", nil + default: + panic("unhandled selection") + } +} + +func createDependencyEntry(version string) string { + return fmt.Sprintf(` + + com.azure.spring + spring-cloud-azure-starter + %s + + `, version) +} + +func appendSpringCloudAzureDep(packageFilePath string, version string) error { + fileData, err := os.ReadFile(packageFilePath) + if err != nil { + fmt.Printf("Failed to read file: %v\n", err) + return err + } + pomContent := string(fileData) + + dependencyEntry := createDependencyEntry(version) + depMgmtSectionStart := "" + depMgmtSectionEnd := "" + if depMgmtStartIdx := strings.Index(pomContent, depMgmtSectionStart); depMgmtStartIdx != -1 { + depMgmtEndIdx := strings.Index(pomContent, depMgmtSectionEnd) + pomContent1 := appendDep(pomContent[:depMgmtStartIdx], dependencyEntry) + pomContent2 := appendDep(pomContent[depMgmtEndIdx:], dependencyEntry) + pomContent = pomContent1 + pomContent[depMgmtStartIdx:depMgmtEndIdx] + pomContent2 + } else { + pomContent = appendDep(pomContent, dependencyEntry) + } + + err = os.WriteFile(packageFilePath, []byte(pomContent), 0644) + if err != nil { + return nil + } + return nil +} + +func appendDep(content string, insertion string) string { + depSectionEnd := "" + return strings.Replace(content, depSectionEnd, insertion+"\n "+depSectionEnd, 1) +} From 9b0a34dd199f30f00987cf409a2c666b4c60607f Mon Sep 17 00:00:00 2001 From: haozhang Date: Thu, 14 Nov 2024 20:48:52 +0800 Subject: [PATCH 2/5] remove auto-add dep code --- cli/azd/internal/appdetect/appdetect.go | 8 +-- cli/azd/internal/appdetect/java.go | 13 ++-- cli/azd/internal/repository/app_init.go | 91 +------------------------ 3 files changed, 10 insertions(+), 102 deletions(-) diff --git a/cli/azd/internal/appdetect/appdetect.go b/cli/azd/internal/appdetect/appdetect.go index 75007fa97f9..cfdf5b60172 100644 --- a/cli/azd/internal/appdetect/appdetect.go +++ b/cli/azd/internal/appdetect/appdetect.go @@ -150,9 +150,8 @@ func (a AzureDepServiceBus) ResourceDisplay() string { } type AzureDepEventHubs struct { - Names []string - UseKafka bool - SpringBootVersion string + Names []string + UseKafka bool } func (a AzureDepEventHubs) ResourceDisplay() string { @@ -191,9 +190,6 @@ type Project struct { // The path to the project directory. Path string - // The package file relative path to the project path - PackageFileRelPath string - // A short description of the detection rule applied. DetectionRule string diff --git a/cli/azd/internal/appdetect/java.go b/cli/azd/internal/appdetect/java.go index 0a0a387b61e..2a8ae4a928a 100644 --- a/cli/azd/internal/appdetect/java.go +++ b/cli/azd/internal/appdetect/java.go @@ -53,10 +53,9 @@ func (jd *javaDetector) DetectProject(ctx context.Context, path string, entries _ = currentRoot // use currentRoot here in the analysis result, err := detectDependencies(currentRoot, project, &Project{ - Language: Java, - Path: path, - PackageFileRelPath: entry.Name(), - DetectionRule: "Inferred by presence of: pom.xml", + Language: Java, + Path: path, + DetectionRule: "Inferred by presence of: pom.xml", }) if err != nil { return nil, fmt.Errorf("detecting dependencies: %w", err) @@ -151,6 +150,7 @@ func detectDependencies(currentRoot *mavenProject, mavenProject *mavenProject, p springBootVersion = detectSpringBootVersion(currentRoot, mavenProject) applicationProperties = readProperties(project.Path) } + _ = springBootVersion databaseDepMap := map[DatabaseDep]struct{}{} for _, dep := range mavenProject.Dependencies { @@ -235,9 +235,8 @@ func detectDependencies(currentRoot *mavenProject, mavenProject *mavenProject, p } } project.AzureDeps = append(project.AzureDeps, AzureDepEventHubs{ - Names: destinations, - UseKafka: true, - SpringBootVersion: springBootVersion, + Names: destinations, + UseKafka: true, }) } diff --git a/cli/azd/internal/repository/app_init.go b/cli/azd/internal/repository/app_init.go index 19391ce2f04..10152ebbaae 100644 --- a/cli/azd/internal/repository/app_init.go +++ b/cli/azd/internal/repository/app_init.go @@ -136,12 +136,10 @@ func (i *Initializer) InitFromApp( if prj.Language == appdetect.Java { var hasKafkaDep bool - var springBootVersion string var hasSpringCloudAzureDep bool for _, dep := range prj.AzureDeps { if eventHubs, ok := dep.(appdetect.AzureDepEventHubs); ok && eventHubs.UseKafka { hasKafkaDep = true - springBootVersion = eventHubs.SpringBootVersion } if _, ok := dep.(appdetect.SpringCloudAzureDep); ok { hasSpringCloudAzureDep = true @@ -149,7 +147,7 @@ func (i *Initializer) InitFromApp( } if hasKafkaDep && !hasSpringCloudAzureDep { - err := processSpringCloudAzureDepByPrompt(i.console, ctx, &projects[index], springBootVersion) + err := processSpringCloudAzureDepByPrompt(i.console, ctx, &projects[index]) if err != nil { return err } @@ -624,12 +622,11 @@ func (i *Initializer) prjConfigFromDetect( return config, nil } -func processSpringCloudAzureDepByPrompt(console input.Console, ctx context.Context, project *appdetect.Project, springBootVersion string) error { +func processSpringCloudAzureDepByPrompt(console input.Console, ctx context.Context, project *appdetect.Project) error { continueOption, err := console.Select(ctx, input.ConsoleOptions{ Message: "Detected Kafka dependency but no spring-cloud-azure-starter found. Select an option", Options: []string{ "Exit then I will manually add this dependency", - "Continue and help me add this dependency", "Continue and do not add this dependency", "Continue and do not provision Azure Event Hubs for Kafka", }, @@ -642,28 +639,8 @@ func processSpringCloudAzureDepByPrompt(console input.Console, ctx context.Conte case 0: return errors.New("you have to manually add dependency com.azure.spring:spring-cloud-azure-starter by following https://github.com/Azure/azure-sdk-for-java/wiki/Spring-Versions-Mapping") case 1: - // determine spring cloud azure version by spring boot version - version := "" - if strings.HasPrefix(springBootVersion, "2.") { - version = "4.19.0" - } else if strings.HasPrefix(springBootVersion, "3.") { - version = "5.18.0" - } else { - version, err = promptSpringBootVersion(console, ctx) - if err != nil { - return err - } - } - // append spring cloud azure dependency - packageFile := filepath.Join(project.Path, project.PackageFileRelPath) - err := appendSpringCloudAzureDep(packageFile, version) - if err != nil { - return err - } return nil case 2: - return nil - case 3: // remove Kafka Azure Dep var result []appdetect.AzureDep for _, dep := range project.AzureDeps { @@ -676,67 +653,3 @@ func processSpringCloudAzureDepByPrompt(console input.Console, ctx context.Conte } return nil } - -func promptSpringBootVersion(console input.Console, ctx context.Context) (string, error) { - selection, err := console.Select(ctx, input.ConsoleOptions{ - Message: "No spring boot version detected, what is your spring boot version?", - Options: []string{ - "Spring Boot 2.x", - "Spring Boot 3.x", - }, - }) - if err != nil { - return "", err - } - - switch selection { - case 0: - return "2.x", nil - case 1: - return "3.x", nil - default: - panic("unhandled selection") - } -} - -func createDependencyEntry(version string) string { - return fmt.Sprintf(` - - com.azure.spring - spring-cloud-azure-starter - %s - - `, version) -} - -func appendSpringCloudAzureDep(packageFilePath string, version string) error { - fileData, err := os.ReadFile(packageFilePath) - if err != nil { - fmt.Printf("Failed to read file: %v\n", err) - return err - } - pomContent := string(fileData) - - dependencyEntry := createDependencyEntry(version) - depMgmtSectionStart := "" - depMgmtSectionEnd := "" - if depMgmtStartIdx := strings.Index(pomContent, depMgmtSectionStart); depMgmtStartIdx != -1 { - depMgmtEndIdx := strings.Index(pomContent, depMgmtSectionEnd) - pomContent1 := appendDep(pomContent[:depMgmtStartIdx], dependencyEntry) - pomContent2 := appendDep(pomContent[depMgmtEndIdx:], dependencyEntry) - pomContent = pomContent1 + pomContent[depMgmtStartIdx:depMgmtEndIdx] + pomContent2 - } else { - pomContent = appendDep(pomContent, dependencyEntry) - } - - err = os.WriteFile(packageFilePath, []byte(pomContent), 0644) - if err != nil { - return nil - } - return nil -} - -func appendDep(content string, insertion string) string { - depSectionEnd := "" - return strings.Replace(content, depSectionEnd, insertion+"\n "+depSectionEnd, 1) -} From 32adccffafea28058c9eada4443874c9b732b0aa Mon Sep 17 00:00:00 2001 From: haozhang Date: Thu, 14 Nov 2024 21:28:08 +0800 Subject: [PATCH 3/5] add UT --- cli/azd/internal/appdetect/java.go | 2 +- cli/azd/internal/appdetect/java_test.go | 84 +++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 cli/azd/internal/appdetect/java_test.go diff --git a/cli/azd/internal/appdetect/java.go b/cli/azd/internal/appdetect/java.go index 2a8ae4a928a..cbbfa924849 100644 --- a/cli/azd/internal/appdetect/java.go +++ b/cli/azd/internal/appdetect/java.go @@ -268,7 +268,7 @@ func detectSpringBootVersion(currentRoot *mavenProject, mavenProject *mavenProje } } } - } else { + } else if mavenProject != nil { if mavenProject.Parent.ArtifactId == "spring-boot-starter-parent" { return mavenProject.Parent.Version } else { diff --git a/cli/azd/internal/appdetect/java_test.go b/cli/azd/internal/appdetect/java_test.go new file mode 100644 index 00000000000..799d3ecc75a --- /dev/null +++ b/cli/azd/internal/appdetect/java_test.go @@ -0,0 +1,84 @@ +package appdetect + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestDetectSpringBootVersion(t *testing.T) { + tests := []struct { + name string + currentRoot *mavenProject + project *mavenProject + expectedVersion string + }{ + { + "unknown", + nil, + nil, + "unknown", + }, + { + "project.parent", + nil, + &mavenProject{ + Parent: parent{ + GroupId: "org.springframework.boot", + ArtifactId: "spring-boot-starter-parent", + Version: "2.x", + }, + }, + "2.x", + }, + { + "project.dependencyManagement", + nil, + &mavenProject{ + DependencyManagement: dependencyManagement{ + Dependencies: []dependency{ + { + GroupId: "org.springframework.boot", + ArtifactId: "spring-boot-dependencies", + Version: "2.x", + }, + }, + }, + }, + "2.x", + }, + { + "root.parent", + &mavenProject{ + Parent: parent{ + GroupId: "org.springframework.boot", + ArtifactId: "spring-boot-starter-parent", + Version: "3.x", + }, + }, + nil, + "3.x", + }, + { + "root.dependencyManagement", + &mavenProject{ + DependencyManagement: dependencyManagement{ + Dependencies: []dependency{ + { + GroupId: "org.springframework.boot", + ArtifactId: "spring-boot-dependencies", + Version: "3.x", + }, + }, + }, + }, + nil, + "3.x", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + version := detectSpringBootVersion(tt.currentRoot, tt.project) + assert.Equal(t, tt.expectedVersion, version) + }) + } +} From 63ff8f89a4a07a5955d1c496cd5c0596e9b08916 Mon Sep 17 00:00:00 2001 From: haozhang Date: Fri, 15 Nov 2024 09:45:45 +0800 Subject: [PATCH 4/5] resolve comments --- cli/azd/internal/appdetect/appdetect.go | 1 - cli/azd/internal/appdetect/java.go | 32 +------------------------ cli/azd/internal/repository/app_init.go | 4 ++-- 3 files changed, 3 insertions(+), 34 deletions(-) diff --git a/cli/azd/internal/appdetect/appdetect.go b/cli/azd/internal/appdetect/appdetect.go index 40b44995102..26b76e96e73 100644 --- a/cli/azd/internal/appdetect/appdetect.go +++ b/cli/azd/internal/appdetect/appdetect.go @@ -167,7 +167,6 @@ func (a AzureDepStorageAccount) ResourceDisplay() string { } type SpringCloudAzureDep struct { - Version string } func (a SpringCloudAzureDep) ResourceDisplay() string { diff --git a/cli/azd/internal/appdetect/java.go b/cli/azd/internal/appdetect/java.go index cbbfa924849..19eea14c27a 100644 --- a/cli/azd/internal/appdetect/java.go +++ b/cli/azd/internal/appdetect/java.go @@ -145,12 +145,9 @@ func detectDependencies(currentRoot *mavenProject, mavenProject *mavenProject, p } } applicationProperties := make(map[string]string) - var springBootVersion string if isSpringBoot { - springBootVersion = detectSpringBootVersion(currentRoot, mavenProject) applicationProperties = readProperties(project.Path) } - _ = springBootVersion databaseDepMap := map[DatabaseDep]struct{}{} for _, dep := range mavenProject.Dependencies { @@ -241,9 +238,7 @@ func detectDependencies(currentRoot *mavenProject, mavenProject *mavenProject, p } if dep.GroupId == "com.azure.spring" && dep.ArtifactId == "spring-cloud-azure-starter" { - project.AzureDeps = append(project.AzureDeps, SpringCloudAzureDep{ - Version: dep.Version, - }) + project.AzureDeps = append(project.AzureDeps, SpringCloudAzureDep{}) } } @@ -257,31 +252,6 @@ func detectDependencies(currentRoot *mavenProject, mavenProject *mavenProject, p return project, nil } -func detectSpringBootVersion(currentRoot *mavenProject, mavenProject *mavenProject) string { - if currentRoot != nil { - if currentRoot.Parent.ArtifactId == "spring-boot-starter-parent" { - return currentRoot.Parent.Version - } else { - for _, dep := range currentRoot.DependencyManagement.Dependencies { - if dep.ArtifactId == "spring-boot-dependencies" { - return dep.Version - } - } - } - } else if mavenProject != nil { - if mavenProject.Parent.ArtifactId == "spring-boot-starter-parent" { - return mavenProject.Parent.Version - } else { - for _, dep := range mavenProject.DependencyManagement.Dependencies { - if dep.ArtifactId == "spring-boot-dependencies" { - return dep.Version - } - } - } - } - return "unknown" -} - func readProperties(projectPath string) map[string]string { // todo: do we need to consider the bootstrap.properties result := make(map[string]string) diff --git a/cli/azd/internal/repository/app_init.go b/cli/azd/internal/repository/app_init.go index 6ae3dbd36fb..93a5cff75f7 100644 --- a/cli/azd/internal/repository/app_init.go +++ b/cli/azd/internal/repository/app_init.go @@ -795,8 +795,8 @@ func processSpringCloudAzureDepByPrompt(console input.Console, ctx context.Conte Message: "Detected Kafka dependency but no spring-cloud-azure-starter found. Select an option", Options: []string{ "Exit then I will manually add this dependency", - "Continue and do not add this dependency", - "Continue and do not provision Azure Event Hubs for Kafka", + "Continue without this dependency, and provision Azure Event Hubs for Kafka", + "Continue without this dependency, and not provision Azure Event Hubs for Kafka", }, }) if err != nil { From c208b754caa4f6e1b795bcf892cb5c3a48cca0bc Mon Sep 17 00:00:00 2001 From: haozhang Date: Fri, 15 Nov 2024 09:47:44 +0800 Subject: [PATCH 5/5] remove unused ones --- cli/azd/internal/appdetect/java.go | 4 +- cli/azd/internal/appdetect/java_test.go | 84 ------------------------- 2 files changed, 2 insertions(+), 86 deletions(-) delete mode 100644 cli/azd/internal/appdetect/java_test.go diff --git a/cli/azd/internal/appdetect/java.go b/cli/azd/internal/appdetect/java.go index 19eea14c27a..73b6b36bd9a 100644 --- a/cli/azd/internal/appdetect/java.go +++ b/cli/azd/internal/appdetect/java.go @@ -52,7 +52,7 @@ func (jd *javaDetector) DetectProject(ctx context.Context, path string, entries } _ = currentRoot // use currentRoot here in the analysis - result, err := detectDependencies(currentRoot, project, &Project{ + result, err := detectDependencies(project, &Project{ Language: Java, Path: path, DetectionRule: "Inferred by presence of: pom.xml", @@ -128,7 +128,7 @@ func readMavenProject(filePath string) (*mavenProject, error) { return &project, nil } -func detectDependencies(currentRoot *mavenProject, mavenProject *mavenProject, project *Project) (*Project, error) { +func detectDependencies(mavenProject *mavenProject, project *Project) (*Project, error) { // how can we tell it's a Spring Boot project? // 1. It has a parent with a groupId of org.springframework.boot and an artifactId of spring-boot-starter-parent // 2. It has a dependency with a groupId of org.springframework.boot and an artifactId that starts with diff --git a/cli/azd/internal/appdetect/java_test.go b/cli/azd/internal/appdetect/java_test.go deleted file mode 100644 index 799d3ecc75a..00000000000 --- a/cli/azd/internal/appdetect/java_test.go +++ /dev/null @@ -1,84 +0,0 @@ -package appdetect - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestDetectSpringBootVersion(t *testing.T) { - tests := []struct { - name string - currentRoot *mavenProject - project *mavenProject - expectedVersion string - }{ - { - "unknown", - nil, - nil, - "unknown", - }, - { - "project.parent", - nil, - &mavenProject{ - Parent: parent{ - GroupId: "org.springframework.boot", - ArtifactId: "spring-boot-starter-parent", - Version: "2.x", - }, - }, - "2.x", - }, - { - "project.dependencyManagement", - nil, - &mavenProject{ - DependencyManagement: dependencyManagement{ - Dependencies: []dependency{ - { - GroupId: "org.springframework.boot", - ArtifactId: "spring-boot-dependencies", - Version: "2.x", - }, - }, - }, - }, - "2.x", - }, - { - "root.parent", - &mavenProject{ - Parent: parent{ - GroupId: "org.springframework.boot", - ArtifactId: "spring-boot-starter-parent", - Version: "3.x", - }, - }, - nil, - "3.x", - }, - { - "root.dependencyManagement", - &mavenProject{ - DependencyManagement: dependencyManagement{ - Dependencies: []dependency{ - { - GroupId: "org.springframework.boot", - ArtifactId: "spring-boot-dependencies", - Version: "3.x", - }, - }, - }, - }, - nil, - "3.x", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - version := detectSpringBootVersion(tt.currentRoot, tt.project) - assert.Equal(t, tt.expectedVersion, version) - }) - } -}