This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MavenBuilder.ts
136 lines (124 loc) · 4.49 KB
/
MavenBuilder.ts
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
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
GitProject,
LocalProject,
RemoteRepoRef,
} from "@atomist/automation-client";
import {
FulfillableGoalDetails,
goal,
Goal,
ProgressLog,
spawnLog,
SpawnLogResult,
} from "@atomist/sdm";
import {
Builder,
BuildInProgress,
} from "@atomist/sdm-pack-build";
import { AppInfo } from "@atomist/sdm/lib/spi/deploy/Deployment";
import _ = require("lodash");
import { determineMavenCommand } from "../mavenCommand";
import { MavenProjectIdentifier } from "../parse/pomParser";
import { VersionedArtifact } from "../VersionedArtifact";
import { MavenOptions } from "./helpers";
/**
* Build with Maven in the local automation client.
* This implementation requires Java and maven on the classpath.
* Note it is NOT intended for use for multiple organizations. It's OK
* for one organization to use inside its firewall, but there is potential
* vulnerability in builds of unrelated tenants getting at each others
* artifacts.
*/
export function mavenBuilder(args: MavenArgs[] = [],
deploymentUnitFileLocator: (p: LocalProject, mpi: VersionedArtifact) => string =
(p, mpi) => `${p.baseDir}/target/${mpi.artifact}-${mpi.version}.jar`): Builder {
return async goalInvocation => {
const { configuration, credentials, progressLog, id } = goalInvocation;
return configuration.sdm.projectLoader.doWithProject({ credentials, id, readOnly: true }, async p => {
// Find the artifact info from Maven
const va = await MavenProjectIdentifier(p);
const appId = { ...va, name: va.artifact, id };
const buildResult = await mavenPackage(p, progressLog, args);
const rb = new UpdatingBuild(id, buildResult);
rb.ai = appId;
rb.deploymentUnitFile = deploymentUnitFileLocator(p, va);
return rb;
});
};
}
class UpdatingBuild implements BuildInProgress {
public ai: AppInfo;
public deploymentUnitFile: string;
constructor(public repoRef: RemoteRepoRef,
public buildResult: SpawnLogResult) {
}
get appInfo(): AppInfo {
return this.ai;
}
}
export interface MavenArgs {
name: string;
value?: string;
type?: MavenArgType;
}
export enum MavenArgType {
Property,
Option,
}
export async function mavenPackage(p: GitProject,
progressLog: ProgressLog,
args: MavenArgs[] = [],
mavenGoal: string = "package"): Promise<SpawnLogResult> {
const command = await determineMavenCommand(p);
return spawnLog(
command,
[
mavenGoal,
...MavenOptions,
..._.flatten(args.filter(
a => a.type === MavenArgType.Option)
.map(a => [`${a.name.length <= 3 ? "-" : "--"}${a.name}`, !!a.value ? `${a.value}` : undefined]))
.filter(a => !!a),
...args.filter(
a => a.type === MavenArgType.Property || !a.type)
.map(a => `-D${a.name}${!!a.value ? `=${a.value}` : ""}`),
],
{
cwd: p.baseDir,
log: progressLog,
},
);
}
/**
* Run an arbitrary Maven goal with the given arguments.
* Does not take account of any generated artifact.
* @param details goal details
* @param {string} mavenGoal
* @param args
* @return {Builder}
*/
export function mavenRunner(details: FulfillableGoalDetails,
mavenGoal: string,
args: MavenArgs[] = []): Goal {
return goal(details, async goalInvocation => {
const { configuration, credentials, progressLog, id } = goalInvocation;
return configuration.sdm.projectLoader.doWithProject({ credentials, id, readOnly: true }, async p => {
await mavenPackage(p, progressLog, args, mavenGoal);
});
});
}