0
# Build Task Types
1
2
Specialized task types for common build operations including package creation, publishing, and test execution with built-in conventions and workflow automation.
3
4
## Capabilities
5
6
### Package Task
7
8
Creates a PackageTask that generates distributable packages in various formats (tar, zip, jar, etc.).
9
10
```javascript { .api }
11
/**
12
* Creates a PackageTask for generating distributable packages
13
* @param {string} name - The name of the project
14
* @param {string} version - The current project version
15
* @param {string[]} [prereqs] - Prerequisites to run before packaging
16
* @param {Function} definition - Function that defines package contents and format
17
* @returns {PackageTask} The created package task instance
18
*/
19
function packageTask(name, version, prereqs, definition);
20
```
21
22
**Usage Examples:**
23
24
```javascript
25
// Basic package task
26
const pkg = packageTask('myproject', '1.0.0', function () {
27
this.packageFiles.include([
28
'lib/**',
29
'bin/**',
30
'README.md',
31
'package.json'
32
]);
33
this.packageFiles.exclude(['lib/**/*.test.js']);
34
this.needTarGz = true;
35
});
36
37
// Package with prerequisites and custom options
38
const pkg = packageTask('webapp', '2.1.0', ['build', 'test'], function () {
39
this.packageDir = 'dist/packages';
40
this.packageFiles.include([
41
'build/**',
42
'public/**',
43
'package.json',
44
'README.md'
45
]);
46
47
// Create multiple archive types
48
this.needTar = true;
49
this.needTarGz = true;
50
this.needZip = true;
51
52
// Archive options
53
this.archiveNoBaseDir = true;
54
this.archiveChangeDir = 'build';
55
});
56
57
// JAR package for Java-style projects
58
const pkg = packageTask('java-tools', '1.5.2', function () {
59
this.packageFiles.include([
60
'dist/**/*.class',
61
'resources/**',
62
'META-INF/**'
63
]);
64
this.needJar = true;
65
this.manifestFile = 'META-INF/MANIFEST.MF';
66
});
67
```
68
69
**PackageTask Properties:**
70
71
```javascript { .api }
72
interface PackageTask {
73
name: string; // Project name
74
version: string; // Project version
75
prereqs: string[]; // Prerequisites
76
packageDir: string; // Package directory (default: 'pkg')
77
packageFiles: FileList; // Files to include in package
78
needTar: boolean; // Create .tgz archive
79
needTarGz: boolean; // Create .tar.gz archive
80
needTarBz2: boolean; // Create .tar.bz2 archive
81
needJar: boolean; // Create .jar archive
82
needZip: boolean; // Create .zip archive
83
manifestFile: string; // JAR manifest file path
84
tarCommand: string; // Tar command (default: 'tar')
85
jarCommand: string; // Jar command (default: 'jar')
86
zipCommand: string; // Zip command (default: 'zip')
87
archiveNoBaseDir: boolean; // Archive without base directory
88
archiveChangeDir: string; // Change to directory before archiving
89
archiveContentDir: string; // Specific content directory to archive
90
91
packageName(): string; // Get package name
92
packageDirPath(): string; // Get full package directory path
93
}
94
```
95
96
**Created Tasks:**
97
- `package` - Build the complete package
98
- `repackage` - Rebuild the package
99
- `clobberPackage` - Remove package directory
100
- `clobber` - Alias for clobberPackage
101
102
### Publish Task
103
104
Creates a PublishTask that handles version management and package publishing to repositories.
105
106
```javascript { .api }
107
/**
108
* Creates a PublishTask for version management and package publishing
109
* @param {string} name - The project name
110
* @param {string[]} [prereqs] - Prerequisites to run before publishing
111
* @param {Object} [opts] - Publishing options
112
* @param {Function} definition - Function that defines publish configuration
113
* @returns {PublishTask} The created publish task instance
114
*/
115
function publishTask(name, prereqs, opts, definition);
116
117
/**
118
* Legacy alias for publishTask - creates a PublishTask for npm publishing
119
* @param {string} name - The project name
120
* @param {string[]} [prereqs] - Prerequisites to run before publishing
121
* @param {Object} [opts] - Publishing options
122
* @param {Function} definition - Function that defines publish configuration
123
* @returns {PublishTask} The created publish task instance
124
*/
125
function npmPublishTask(name, prereqs, opts, definition);
126
```
127
128
**Publishing Options:**
129
130
```javascript { .api }
131
interface PublishOptions {
132
publishCmd?: string; // Publish command template (default: 'npm publish %filename')
133
publishMessage?: string; // Success message (default: 'BOOM! Published.')
134
gitCmd?: string; // Git command (default: 'git')
135
versionFiles?: string[]; // Files to update with version (default: ['package.json'])
136
}
137
```
138
139
**Usage Examples:**
140
141
```javascript
142
// Basic npm publish task
143
const pub = publishTask('mypackage', ['package'], function () {
144
this.packageFiles.include([
145
'lib/**',
146
'package.json',
147
'README.md'
148
]);
149
});
150
151
// Custom publish configuration
152
const pub = publishTask('webapp', ['build', 'test', 'package'], {
153
publishCmd: 'npm publish --registry=https://my-registry.com %filename',
154
publishMessage: 'Successfully published to private registry!',
155
versionFiles: ['package.json', 'bower.json', 'VERSION.txt']
156
}, function () {
157
this.packageFiles.include([
158
'dist/**',
159
'package.json',
160
'README.md',
161
'CHANGELOG.md'
162
]);
163
});
164
165
// Custom publish command function
166
const pub = publishTask('enterprise-app', ['package'], function () {
167
this.packageFiles.include(['dist/**', 'package.json']);
168
169
this.createPublishCommand = function (version) {
170
return [
171
'aws s3 cp ' + this.packageDirPath() + '/' + this.packageName() + '.tar.gz s3://releases/',
172
'curl -X POST https://api.internal.com/releases -d "version=' + version + '"'
173
];
174
};
175
});
176
```
177
178
**Created Tasks:**
179
- `publish` - Create new version and release
180
- `publishExisting` - Release existing version without version bump
181
- `version` - Update version and push to git
182
- `release` - Package, publish, and cleanup
183
- Various internal tasks (`publish:fetchTags`, `publish:updateVersionFiles`, etc.)
184
185
### Test Task
186
187
Creates a TestTask that provides a framework for running test suites with setup/teardown support.
188
189
```javascript { .api }
190
/**
191
* Creates a TestTask for running test suites
192
* @param {string} name - The project name
193
* @param {string[]} [prereqs] - Prerequisites to run before testing
194
* @param {Function} definition - Function that defines test configuration
195
* @returns {TestTask} The created test task instance
196
*/
197
function testTask(name, prereqs, definition);
198
```
199
200
**Usage Examples:**
201
202
```javascript
203
// Basic test task
204
const test = testTask('myproject', function () {
205
this.testFiles.include('test/**/*.js');
206
});
207
208
// Test task with prerequisites and custom name
209
const test = testTask('webapp', ['build'], function () {
210
this.testName = 'spec'; // Creates 'spec' task instead of 'test'
211
this.testFiles.include([
212
'test/unit/**/*.js',
213
'test/integration/**/*.js'
214
]);
215
this.testFiles.exclude('test/**/*.helper.js');
216
});
217
218
// Hidden test task (not shown in jake -T)
219
const test = testTask('internal-tests', function () {
220
this.showDescription = false;
221
this.testFiles.include('test/internal/**/*.js');
222
});
223
```
224
225
**Test File Structure:**
226
227
Test files should export functions for individual tests and optional setup/teardown:
228
229
```javascript
230
// test/math.test.js
231
exports.before = function () {
232
// Setup before all tests in this file
233
console.log('Setting up math tests');
234
};
235
236
exports.after = function () {
237
// Cleanup after all tests in this file
238
console.log('Cleaning up math tests');
239
};
240
241
exports.beforeEach = function () {
242
// Setup before each test
243
this.calculator = new Calculator();
244
};
245
246
exports.afterEach = function () {
247
// Cleanup after each test
248
this.calculator = null;
249
};
250
251
exports.testAddition = function () {
252
const result = this.calculator.add(2, 3);
253
if (result !== 5) {
254
throw new Error('Addition test failed');
255
}
256
console.log('Addition test passed');
257
};
258
259
exports.testSubtraction = function () {
260
const result = this.calculator.subtract(5, 3);
261
if (result !== 2) {
262
throw new Error('Subtraction test failed');
263
}
264
console.log('Subtraction test passed');
265
};
266
```
267
268
**TestTask Properties:**
269
270
```javascript { .api }
271
interface TestTask {
272
testName: string; // Name of test namespace and task (default: 'test')
273
testFiles: FileList; // List of test files to load
274
showDescription: boolean; // Show task in `jake -T` (default: true)
275
totalTests: number; // Total number of tests to run
276
executedTests: number; // Number of successfully executed tests
277
}
278
```
279
280
**Created Tasks:**
281
- `test` (or custom testName) - Main task to run all tests
282
- `test:run` (or custom) - Internal task that executes test files
283
284
## Build Workflow Examples
285
286
### Complete CI/CD Pipeline
287
288
```javascript
289
// Complete build and deploy pipeline
290
desc('Clean build artifacts');
291
task('clean', function () {
292
jake.rmRf('dist');
293
jake.rmRf('coverage');
294
});
295
296
desc('Install dependencies');
297
task('install', function () {
298
jake.exec(['npm install'], { printStdout: true });
299
});
300
301
desc('Lint source code');
302
task('lint', ['install'], function () {
303
jake.exec(['eslint src/**/*.js'], { printStdout: true });
304
});
305
306
desc('Run unit tests');
307
task('test:unit', ['install'], function () {
308
jake.exec(['mocha test/unit/**/*.js'], { printStdout: true });
309
});
310
311
desc('Run integration tests');
312
task('test:integration', ['install'], function () {
313
jake.exec(['mocha test/integration/**/*.js'], { printStdout: true });
314
});
315
316
desc('Run all tests');
317
task('test', ['test:unit', 'test:integration']);
318
319
desc('Build application');
320
task('build', ['clean', 'lint', 'test'], function () {
321
jake.exec(['webpack --mode=production'], { printStdout: true });
322
});
323
324
// Package task
325
const pkg = packageTask('myapp', '1.0.0', ['build'], function () {
326
this.packageFiles.include([
327
'dist/**',
328
'package.json',
329
'README.md',
330
'LICENSE'
331
]);
332
this.needTarGz = true;
333
});
334
335
// Publish task
336
const pub = publishTask('myapp', ['package'], function () {
337
this.packageFiles.include([
338
'dist/**',
339
'package.json',
340
'README.md'
341
]);
342
});
343
344
desc('Complete CI/CD pipeline');
345
task('ci', ['clean', 'install', 'lint', 'test', 'build', 'package']);
346
347
desc('Deploy to production');
348
task('deploy', ['ci', 'publish']);
349
```
350
351
### Multi-Environment Builds
352
353
```javascript
354
// Environment-specific builds
355
namespace('build', function () {
356
desc('Build for development');
357
task('dev', ['clean'], function () {
358
process.env.NODE_ENV = 'development';
359
jake.exec(['webpack --mode=development'], { printStdout: true });
360
});
361
362
desc('Build for staging');
363
task('staging', ['clean', 'test'], function () {
364
process.env.NODE_ENV = 'staging';
365
jake.exec(['webpack --mode=production'], { printStdout: true });
366
});
367
368
desc('Build for production');
369
task('prod', ['clean', 'test', 'lint'], function () {
370
process.env.NODE_ENV = 'production';
371
jake.exec(['webpack --mode=production --optimize-minimize'], { printStdout: true });
372
});
373
});
374
375
// Environment-specific packages
376
const devPkg = packageTask('myapp', '1.0.0-dev', ['build:dev'], function () {
377
this.packageDir = 'packages/dev';
378
this.packageFiles.include(['dist/**']);
379
this.needZip = true;
380
});
381
382
const prodPkg = packageTask('myapp', '1.0.0', ['build:prod'], function () {
383
this.packageDir = 'packages/prod';
384
this.packageFiles.include(['dist/**']);
385
this.needTarGz = true;
386
});
387
```