-
Notifications
You must be signed in to change notification settings - Fork 170
/
framework_service_maven.go
212 lines (187 loc) · 5.92 KB
/
framework_service_maven.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package project
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/azure/azure-dev/cli/azd/pkg/async"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/tools"
"github.com/azure/azure-dev/cli/azd/pkg/tools/javac"
"github.com/azure/azure-dev/cli/azd/pkg/tools/maven"
"github.com/otiai10/copy"
)
// The default, conventional App Service Java package name
const AppServiceJavaPackageName = "app"
type mavenProject struct {
env *environment.Environment
mavenCli maven.MavenCli
javacCli javac.JavacCli
}
// NewMavenProject creates a new instance of a maven project
func NewMavenProject(env *environment.Environment, mavenCli maven.MavenCli, javaCli javac.JavacCli) FrameworkService {
return &mavenProject{
env: env,
mavenCli: mavenCli,
javacCli: javaCli,
}
}
func (m *mavenProject) Requirements() FrameworkRequirements {
return FrameworkRequirements{
// Maven will automatically restore & build the project if needed
Package: FrameworkPackageRequirements{
RequireRestore: false,
RequireBuild: false,
},
}
}
// Gets the required external tools for the project
func (m *mavenProject) RequiredExternalTools(context.Context) []tools.ExternalTool {
return []tools.ExternalTool{
m.mavenCli,
m.javacCli,
}
}
// Initializes the maven project
func (m *mavenProject) Initialize(ctx context.Context, serviceConfig *ServiceConfig) error {
m.mavenCli.SetPath(serviceConfig.Path(), serviceConfig.Project.Path)
return nil
}
// Restores dependencies using the Maven CLI
func (m *mavenProject) Restore(
ctx context.Context,
serviceConfig *ServiceConfig,
) *async.TaskWithProgress[*ServiceRestoreResult, ServiceProgress] {
return async.RunTaskWithProgress(
func(task *async.TaskContextWithProgress[*ServiceRestoreResult, ServiceProgress]) {
task.SetProgress(NewServiceProgress("Resolving maven dependencies"))
if err := m.mavenCli.ResolveDependencies(ctx, serviceConfig.Path()); err != nil {
task.SetError(fmt.Errorf("resolving maven dependencies: %w", err))
return
}
task.SetResult(&ServiceRestoreResult{})
},
)
}
// Builds the maven project
func (m *mavenProject) Build(
ctx context.Context,
serviceConfig *ServiceConfig,
restoreOutput *ServiceRestoreResult,
) *async.TaskWithProgress[*ServiceBuildResult, ServiceProgress] {
return async.RunTaskWithProgress(
func(task *async.TaskContextWithProgress[*ServiceBuildResult, ServiceProgress]) {
task.SetProgress(NewServiceProgress("Compiling maven project"))
if err := m.mavenCli.Compile(ctx, serviceConfig.Path()); err != nil {
task.SetError(err)
return
}
task.SetResult(&ServiceBuildResult{
Restore: restoreOutput,
BuildOutputPath: serviceConfig.Path(),
})
},
)
}
func (m *mavenProject) Package(
ctx context.Context,
serviceConfig *ServiceConfig,
buildOutput *ServiceBuildResult,
) *async.TaskWithProgress[*ServicePackageResult, ServiceProgress] {
return async.RunTaskWithProgress(
func(task *async.TaskContextWithProgress[*ServicePackageResult, ServiceProgress]) {
packageDest, err := os.MkdirTemp("", "azd")
if err != nil {
task.SetError(fmt.Errorf("creating staging directory: %w", err))
return
}
task.SetProgress(NewServiceProgress("Packaging maven project"))
if err := m.mavenCli.Package(ctx, serviceConfig.Path()); err != nil {
task.SetError(err)
return
}
packageSrcPath := buildOutput.BuildOutputPath
if packageSrcPath == "" {
packageSrcPath = serviceConfig.Path()
}
if serviceConfig.OutputPath != "" {
packageSrcPath = filepath.Join(packageSrcPath, serviceConfig.OutputPath)
} else {
packageSrcPath = filepath.Join(packageSrcPath, "target")
}
packageSrcFileInfo, err := os.Stat(packageSrcPath)
if err != nil {
if serviceConfig.OutputPath == "" {
task.SetError(fmt.Errorf("reading default maven target path %s: %w", packageSrcPath, err))
} else {
task.SetError(fmt.Errorf("reading dist path %s: %w", packageSrcPath, err))
}
return
}
archive := ""
if packageSrcFileInfo.IsDir() {
archive, err = m.discoverArchive(packageSrcPath)
if err != nil {
task.SetError(err)
return
}
} else {
archive = packageSrcPath
if !isSupportedJavaArchive(archive) {
ext := filepath.Ext(archive)
task.SetError(
fmt.Errorf(
//nolint:lll
"file %s with extension %s is not a supported java archive file (.ear, .war, .jar)", ext, archive))
return
}
}
task.SetProgress(NewServiceProgress("Copying deployment package"))
ext := strings.ToLower(filepath.Ext(archive))
err = copy.Copy(archive, filepath.Join(packageDest, AppServiceJavaPackageName+ext))
if err != nil {
task.SetError(fmt.Errorf("copying to staging directory failed: %w", err))
return
}
task.SetResult(&ServicePackageResult{
Build: buildOutput,
PackagePath: packageDest,
})
},
)
}
func isSupportedJavaArchive(archiveFile string) bool {
ext := strings.ToLower(filepath.Ext(archiveFile))
return ext == ".jar" || ext == ".war" || ext == ".ear"
}
func (m *mavenProject) discoverArchive(dir string) (string, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return "", fmt.Errorf("discovering java archive files in %s: %w", dir, err)
}
archiveFiles := []string{}
for _, entry := range entries {
if entry.IsDir() {
continue
}
name := entry.Name()
if isSupportedJavaArchive(name) {
archiveFiles = append(archiveFiles, name)
}
}
switch len(archiveFiles) {
case 0:
return "", fmt.Errorf("no java archive files (.jar, .ear, .war) found in %s", dir)
case 1:
return filepath.Join(dir, archiveFiles[0]), nil
default:
names := strings.Join(archiveFiles, ", ")
return "", fmt.Errorf(
//nolint:lll
"multiple java archive files (.jar, .ear, .war) found in %s: %s. To pick a specific archive to be used, specify the relative path to the archive file using the 'dist' property in azure.yaml",
dir,
names,
)
}
}