forked from gaia-pipeline/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_java.go
101 lines (85 loc) · 2.7 KB
/
build_java.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
package pipeline
import (
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/gaia-pipeline/gaia"
"github.com/satori/go.uuid"
)
var (
mavenBinaryName = "mvn"
)
const (
javaFolder = "java"
javaFinalJarName = "plugin-jar-with-dependencies.jar"
mavenTargetFolder = "target"
)
// BuildPipelineJava is the real implementation of BuildPipeline for java
type BuildPipelineJava struct {
Type gaia.PipelineType
}
// PrepareEnvironment prepares the environment before we start the build process.
func (b *BuildPipelineJava) PrepareEnvironment(p *gaia.CreatePipeline) error {
// create uuid for destination folder
uuid := uuid.Must(uuid.NewV4(), nil)
// Create local temp folder for clone
rootPath := filepath.Join(gaia.Cfg.HomePath, tmpFolder, javaFolder)
cloneFolder := filepath.Join(rootPath, srcFolder, uuid.String())
err := os.MkdirAll(cloneFolder, 0700)
if err != nil {
return err
}
// Set new generated path in pipeline obj for later usage
p.Pipeline.Repo.LocalDest = cloneFolder
p.Pipeline.UUID = uuid.String()
return err
}
// ExecuteBuild executes the java build process
func (b *BuildPipelineJava) ExecuteBuild(p *gaia.CreatePipeline) error {
// Look for maven executeable
path, err := exec.LookPath(mavenBinaryName)
if err != nil {
gaia.Cfg.Logger.Debug("cannot find maven executeable", "error", err.Error())
return err
}
env := os.Environ()
// Set command args for build
args := []string{
"clean",
"compile",
"assembly:single",
}
// Execute and wait until finish or timeout
output, err := executeCmd(path, args, env, p.Pipeline.Repo.LocalDest)
p.Output = string(output)
if err != nil {
gaia.Cfg.Logger.Debug("cannot build pipeline", "error", err.Error(), "output", string(output))
return err
}
return nil
}
// CopyBinary copies the final compiled archive to the
// destination folder.
func (b *BuildPipelineJava) CopyBinary(p *gaia.CreatePipeline) error {
// Define src and destination
src := filepath.Join(p.Pipeline.Repo.LocalDest, mavenTargetFolder, javaFinalJarName)
dest := filepath.Join(gaia.Cfg.PipelinePath, appendTypeToName(p.Pipeline.Name, p.Pipeline.Type))
// Copy binary
if err := copyFileContents(src, dest); err != nil {
return err
}
// Set +x (execution right) for pipeline
return os.Chmod(dest, 0766)
}
// SavePipeline saves the current pipeline configuration.
func (b *BuildPipelineJava) SavePipeline(p *gaia.Pipeline) error {
dest := filepath.Join(gaia.Cfg.PipelinePath, appendTypeToName(p.Name, p.Type))
p.ExecPath = dest
p.Type = gaia.PTypeJava
p.Name = strings.TrimSuffix(filepath.Base(dest), typeDelimiter+gaia.PTypeJava.String())
p.Created = time.Now()
// Our pipeline is finished constructing. Save it.
return storeService.PipelinePut(p)
}