-
Notifications
You must be signed in to change notification settings - Fork 30
/
pom_flavour.go
64 lines (57 loc) · 1.44 KB
/
pom_flavour.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package importcmd
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
)
const (
MAVEN = "maven"
APPSERVER = "appserver"
LIBERTY = "liberty"
DROPWIZARD = "dropwizard"
)
func PomFlavour(packsDir, pomPath string) (string, error) {
b, err := os.ReadFile(pomPath)
if err != nil {
return "", nil
}
s := string(b)
if strings.Contains(s, "<packaging>war</packaging>") &&
strings.Contains(s, "org.eclipse.microprofile") {
return LIBERTY, nil
}
if strings.Contains(s, "<groupId>io.dropwizard") {
return DROPWIZARD, nil
}
if strings.Contains(s, "<groupId>org.apache.tomcat") {
return APPSERVER, nil
}
// java.version is used by Spring Boot
version, ok := getProp(s, "java.version")
if !ok {
// maven-compiler-plugin 3.6 and later versions supports and recommends maven.compiler.release
version, ok = getProp(s, "maven.compiler.release")
}
if !ok {
// older versions of maven-compiler-plugin uses maven.compiler.target
version, ok = getProp(s, "maven.compiler.target")
}
if ok {
pack := "maven-java" + version
if exists, _ := files.DirExists(filepath.Join(packsDir, pack)); exists {
return pack, nil
}
}
return MAVEN, nil
}
func getProp(pom, prop string) (string, bool) {
propPattern := regexp.MustCompile(fmt.Sprintf("<%s>(\\d+)</%s>", prop, prop))
matches := propPattern.FindStringSubmatch(pom)
if matches != nil {
return matches[1], true
}
return "", false
}