Skip to content

Commit

Permalink
Fix error: failed to get effective pom for multiple module pom (Azure…
Browse files Browse the repository at this point in the history
  • Loading branch information
rujche authored Feb 20, 2025
1 parent a1d3f56 commit 0d89779
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 11 deletions.
94 changes: 93 additions & 1 deletion cli/azd/internal/appdetect/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,95 @@ func TestToMavenProject(t *testing.T) {
},
},
{
name: "Test pom with multi modules",
name: "Test pom with multi modules: root pom build first when run help:effective-pom",
testPoms: []testPom{
{
pomFilePath: "pom.xml",
pomContentString: `
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.7</version>
</parent>
<groupId>org.springframework</groupId>
<artifactId>gs-multi-module</artifactId>
<version>0.1.0</version>
<packaging>pom</packaging>
<modules>
<module>library</module>
<module>application</module>
</modules>
</project>
`,
},
{
pomFilePath: filepath.Join("application", "pom.xml"),
pomContentString: `
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework</groupId>
<artifactId>gs-multi-module</artifactId>
<version>0.1.0</version>
</parent>
<groupId>com.example</groupId>
<artifactId>application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>application</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
`,
},
{
pomFilePath: filepath.Join("library", "pom.xml"),
pomContentString: `
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework</groupId>
<artifactId>gs-multi-module</artifactId>
<version>0.1.0</version>
</parent>
<groupId>com.example</groupId>
<artifactId>library</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>library</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
`,
},
},
expected: []dependency{},
},
{
name: "Test pom with multi modules: root pom build last when run help:effective-pom",
testPoms: []testPom{
{
pomFilePath: "pom.xml",
Expand Down Expand Up @@ -209,6 +297,10 @@ func TestToMavenProject(t *testing.T) {
<name>application</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
22 changes: 12 additions & 10 deletions cli/azd/pkg/tools/maven/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,34 +247,36 @@ func (cli *Cli) EffectivePom(ctx context.Context, pomPath string) (string, error
return "", err
}
pomDir := filepath.Dir(pomPath)
runArgs := exec.NewRunArgs(mvnCmd, "help:effective-pom", "-f", pomPath).WithCwd(pomDir)
// Link to "-pl" related doc: https://maven.apache.org/ref/3.1.0/maven-embedder/cli.html
runArgs := exec.NewRunArgs(mvnCmd, "help:effective-pom", "-f", pomPath, "-pl", filepath.Base(pomPath)).WithCwd(pomDir)
result, err := cli.commandRunner.Run(ctx, runArgs)
if err != nil {
return "", fmt.Errorf("mvn help:effective-pom on project '%s' failed: %w", pomPath, err)
}
return getEffectivePomFromConsoleOutput(result.Stdout)
return getEffectivePomStringFromConsoleOutput(result.Stdout)
}

var projectStart = regexp.MustCompile(`^\s*<project `) // the space can not be deleted.
var projectEnd = regexp.MustCompile(`^\s*</project>\s*$`)

func getEffectivePomFromConsoleOutput(consoleOutput string) (string, error) {
func getEffectivePomStringFromConsoleOutput(consoleOutput string) (string, error) {
var builder strings.Builder
scanner := bufio.NewScanner(strings.NewReader(consoleOutput))
inProject := false

projectStarted := false
projectEnded := false
for scanner.Scan() {
line := scanner.Text()
if projectStart.MatchString(line) {
inProject = true
builder.Reset() // for a pom which contains submodule, the effective pom for root pom appears at last.
projectStarted = true
} else if projectEnd.MatchString(line) {
builder.WriteString(line)
inProject = false
projectEnded = true
}
if inProject {
if projectStarted {
builder.WriteString(line)
}
if projectEnded {
break
}
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("failed to scan console output: %w", err)
Expand Down

0 comments on commit 0d89779

Please sign in to comment.