MvnTree 调用mvn dependency:tree解析依赖 pom: pom文件信息
(ctx context.Context, pom *Pom)
| 528 | // MvnTree 调用mvn dependency:tree解析依赖 |
| 529 | // pom: pom文件信息 |
| 530 | func MvnTree(ctx context.Context, pom *Pom) *model.DepGraph { |
| 531 | |
| 532 | if !config.Conf().Optional.Dynamic { |
| 533 | return nil |
| 534 | } |
| 535 | |
| 536 | if pom == nil { |
| 537 | return nil |
| 538 | } |
| 539 | |
| 540 | if _, err := exec.LookPath("mvn"); err != nil { |
| 541 | return nil |
| 542 | } |
| 543 | |
| 544 | cmd := exec.CommandContext(ctx, "mvn", "dependency:tree") |
| 545 | cmd.Dir = filepath.Dir(pom.File.Abspath()) |
| 546 | output, err := cmd.CombinedOutput() |
| 547 | if err != nil { |
| 548 | // logs.Warn(err) |
| 549 | return nil |
| 550 | } |
| 551 | |
| 552 | // 记录当前处理的依赖树数据 |
| 553 | var lines []string |
| 554 | // 标记是否在依赖范围内树 |
| 555 | tree := false |
| 556 | // 捕获依赖树起始位置 |
| 557 | title := regexp.MustCompile(`--- [^\n]+ ---`) |
| 558 | |
| 559 | scan := bufio.NewScanner(bytes.NewBuffer(output)) |
| 560 | for scan.Scan() { |
| 561 | line := strings.TrimPrefix(scan.Text(), "[INFO] ") |
| 562 | if title.MatchString(line) { |
| 563 | tree = true |
| 564 | continue |
| 565 | } |
| 566 | if tree && strings.Trim(line, "-") == "" { |
| 567 | tree = false |
| 568 | root := parseMvnTree(lines) |
| 569 | if root != nil && root.Name == pom.ArtifactId { |
| 570 | root.Path = pom.File.Relpath() |
| 571 | return root |
| 572 | } |
| 573 | lines = nil |
| 574 | continue |
| 575 | } |
| 576 | if tree { |
| 577 | lines = append(lines, line) |
| 578 | continue |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | return nil |
| 583 | } |
| 584 | |
| 585 | // parseMvnTree 解析 mvn dependency:tree 的输出 |
| 586 | func parseMvnTree(lines []string) *model.DepGraph { |