@@ -8,51 +8,80 @@ import (
8
8
"strings"
9
9
)
10
10
11
+ const (
12
+ modulePattern = "<module>(\\ S|\\ s)*?<\\ /module>"
13
+ dependenciesPattern = "(<dependency>(\\ S|\\ s)*?<\\ /dependency>)|(<plugin>(\\ S|\\ s)*?<\\ /plugin>)"
14
+ )
15
+
16
+ var (
17
+ moduleRegexp = regexp .MustCompile (modulePattern )
18
+ dependenciesRegexp = regexp .MustCompile (dependenciesPattern )
19
+ )
20
+
11
21
func GetVersionProperties (projectPath string , depToPropertyMap map [string ][]string ) error {
12
22
contentBytes , err := os .ReadFile (filepath .Join (projectPath , "pom.xml" )) // #nosec G304
13
23
if err != nil {
14
24
return err
15
25
}
16
- dependenciesPattern := "(<dependency>(.|\\ s)*?<\\ /dependency>)|(<plugin>(.|\\ s)*?<\\ /plugin>)"
17
- dependenciesRegexp , err := regexp .Compile (dependenciesPattern )
26
+ mavenDependencies , err := getDependenciesFromPomXml (contentBytes )
18
27
if err != nil {
19
28
return err
20
29
}
21
- dependencyStrings := dependenciesRegexp .FindAll (contentBytes , - 1 )
22
- for _ , depStr := range dependencyStrings {
23
- dep := & mavenDependency {}
24
- err = xml .Unmarshal (depStr , dep )
25
- if err != nil {
26
- return err
27
- }
28
- depName := dep .GroupId + ":" + dep .ArtifactId
30
+ for _ , mavenDependency := range mavenDependencies {
31
+ depName := mavenDependency .GroupId + ":" + mavenDependency .ArtifactId
29
32
if _ , exist := depToPropertyMap [depName ]; ! exist {
30
33
depToPropertyMap [depName ] = []string {}
31
34
}
32
- if strings .HasPrefix (dep .Version , "${" ) {
33
- depToPropertyMap [depName ] = append (depToPropertyMap [dep .GroupId + ":" + dep .ArtifactId ], strings .TrimPrefix (strings .TrimSuffix (dep .Version , "}" ), "${" ))
35
+ if strings .HasPrefix (mavenDependency .Version , "${" ) {
36
+ depToPropertyMap [depName ] = append (depToPropertyMap [mavenDependency .GroupId + ":" + mavenDependency .ArtifactId ], strings .TrimPrefix (strings .TrimSuffix (mavenDependency .Version , "}" ), "${" ))
34
37
}
35
38
}
36
39
37
- modulePattern := "<module>(.|\\ s)*?<\\ /module>"
38
- moduleRegexp , err := regexp .Compile (modulePattern )
39
- if err != nil {
40
- return err
41
- }
42
- moduleStrings := moduleRegexp .FindAllString (string (contentBytes ), - 1 )
43
- for _ , moduleStr := range moduleStrings {
44
- modulePath := strings .TrimPrefix (strings .TrimSuffix (moduleStr , "</module>" ), "<module>" )
45
- modulePath = strings .TrimSpace (modulePath )
46
- err = GetVersionProperties (filepath .Join (projectPath , modulePath ), depToPropertyMap )
47
- if err != nil {
40
+ for _ , moduleStr := range getMavenModuleFromPomXml (contentBytes ) {
41
+ if err = GetVersionProperties (filepath .Join (projectPath , moduleStr ), depToPropertyMap ); err != nil {
48
42
return err
49
43
}
50
44
}
51
45
return nil
52
46
}
53
47
48
+ // Extract all dependencies from the input pom.xml
49
+ // pomXmlContent - The pom.xml content
50
+ func getDependenciesFromPomXml (pomXmlContent []byte ) ([]mavenDependency , error ) {
51
+ var results []mavenDependency
52
+ dependencyStrings := dependenciesRegexp .FindAll (pomXmlContent , - 1 )
53
+ for _ , depStr := range dependencyStrings {
54
+ dep := & mavenDependency {}
55
+ err := xml .Unmarshal (depStr , dep )
56
+ if err != nil {
57
+ return []mavenDependency {}, err
58
+ }
59
+ dep .trimSpaces ()
60
+ results = append (results , * dep )
61
+ }
62
+ return results , nil
63
+ }
64
+
65
+ // Extract all modules from pom.xml
66
+ // pomXmlContent - The pom.xml content
67
+ func getMavenModuleFromPomXml (pomXmlContent []byte ) []string {
68
+ var results []string
69
+ moduleStrings := moduleRegexp .FindAllString (string (pomXmlContent ), - 1 )
70
+ for _ , moduleStr := range moduleStrings {
71
+ modulePath := strings .TrimPrefix (strings .TrimSuffix (moduleStr , "</module>" ), "<module>" )
72
+ results = append (results , strings .TrimSpace (modulePath ))
73
+ }
74
+ return results
75
+ }
76
+
54
77
type mavenDependency struct {
55
78
GroupId string `xml:"groupId"`
56
79
ArtifactId string `xml:"artifactId"`
57
80
Version string `xml:"version"`
58
81
}
82
+
83
+ func (md * mavenDependency ) trimSpaces () {
84
+ md .GroupId = strings .TrimSpace (md .GroupId )
85
+ md .ArtifactId = strings .TrimSpace (md .ArtifactId )
86
+ md .Version = strings .TrimSpace (md .Version )
87
+ }
0 commit comments