-
Notifications
You must be signed in to change notification settings - Fork 10
/
howto_plugin.html
622 lines (592 loc) · 33 KB
/
howto_plugin.html
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
<!DOCTYPE html>
<html lang="en">
<!--
Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
Licensed under the Apache License, Version 2.0 (see LICENSE).
-->
<head>
<meta charset="utf-8"/>
<title>Developing a Pants Plugin</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="pants-logo.ico">
<!-- In case this is a "test publish", tell search engines where real version lives: -->
<link rel="canonical" href="http://pantsbuild.org/howto_plugin.html">
<link rel="stylesheet" href="bootstrap-custom.min.css">
<link rel="stylesheet" href="bootstrap-custom-theme.min.css">
<link rel="stylesheet" href="docsite.css">
</head>
<body>
<div class="header">
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand navbar-brand-img" href="index.html">
<img src="pants-logo.ico" alt="[pantsbuild logo]">
</a>
<a class="navbar-brand" href="index.html">
Pants v1
</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="/">Docs</a></li>
<li><a href="community.html">Community</a></li>
<li><a href="https://www.github.com/pantsbuild/pants">GitHub</a></li>
<li>
<form class="navbar-form navbar-left search" role="search" action="https://www.google.com/search">
<div class="form-group">
<input type="text" name="as_q" class="form-control query" placeholder="Search">
<input name="as_sitesearch" value="pantsbuild.org" type="hidden">
</div>
</form>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
<div class="deprecated">Pants v1 is no longer maintained. See <a href="https://www.pantsbuild.org/">here</a> for the Pants v2 documentation.</div>
</nav>
</div>
<div class="page">
<div class="container-fluid">
<div class="row">
<div class="col-md-1">
</div>
<div class="col-md-2">
<div class="site-toc">
<ul>
<li class="toc-h1 toc-heading">
Getting Started
</li>
<li class="toc-h1 toc-link ">
<a href="install.html">Installing Pants</a>
</li>
<li class="toc-h1 toc-link ">
<a href="setup_repo.html">Setting Up Pants</a>
</li>
<li class="toc-h1 toc-link ">
<a href="first_tutorial.html">Tutorial</a>
</li>
<li class="toc-h1 toc-link ">
<a href="common_tasks.html">Common Tasks</a>
</li>
<li class="toc-h1 toc-link ">
<a href="orgs.html">Pants for Organizations</a>
</li>
<li class="toc-h1 toc-heading">
Pants Basics
</li>
<li class="toc-h1 toc-link ">
<a href="why_use_pants.html">Why Use Pants?</a>
</li>
<li class="toc-h1 toc-link ">
<a href="first_concepts.html">Pants Concepts</a>
</li>
<li class="toc-h1 toc-link ">
<a href="build_files.html">BUILD files</a>
</li>
<li class="toc-h1 toc-link ">
<a href="target_addresses.html">Target Addresses</a>
</li>
<li class="toc-h1 toc-link ">
<a href="3rdparty.html">Third-Party Dependencies</a>
</li>
<li class="toc-h1 toc-link ">
<a href="options.html">Pants Options</a>
</li>
<li class="toc-h1 toc-link ">
<a href="invoking.html">Invoking Pants</a>
</li>
<li class="toc-h1 toc-link ">
<a href="reporting_server.html">Reporting Server</a>
</li>
<li class="toc-h1 toc-link ">
<a href="ide_support.html">IDE Support</a>
</li>
<li class="toc-h1">
<a class="sidebar-nav-heading" class="toc-drop" data-toggle="collapse" href="#JVM-Support" aria-expanded="false" aria-controls="JVM-Support">JVM Support<span class="caret"></span></a>
<ul class="collapse sidebar-nav sidebar-submenu" id="JVM-Support">
<li class="toc-h1 toc-link ">
<a href="jvm_projects.html">JVM Projects with Pants</a>
</li>
<li class="toc-h1 toc-link ">
<a href="3rdparty_jvm.html">JVM Dependency Management</a>
</li>
<li class="toc-h1 toc-link ">
<a href="scala.html">Scala Support</a>
</li>
<li class="toc-h1 toc-link ">
<a href="publish.html">Publishing Artifacts</a>
</li>
<li class="toc-h1 toc-link ">
<a href="from_maven.html">Pants for Maven Experts</a>
</li>
</ul>
</li>
<li class="toc-h1">
<a class="sidebar-nav-heading" class="toc-drop" data-toggle="collapse" href="#Python-Support" aria-expanded="false" aria-controls="Python-Support">Python Support<span class="caret"></span></a>
<ul class="collapse sidebar-nav sidebar-submenu" id="Python-Support">
<li class="toc-h1 toc-link ">
<a href="python_readme.html">Python Projects with Pants</a>
</li>
<li class="toc-h1 toc-link ">
<a href="3rdparty_py.html">Python 3rdparty Pattern</a>
</li>
</ul>
</li>
<li class="toc-h1 toc-link ">
<a href="go_readme.html">Go support for Pants</a>
</li>
<li class="toc-h1 toc-link ">
<a href="node_readme.html">Node.js Support</a>
</li>
<li class="toc-h1 toc-heading">
Code & Doc Generation
</li>
<li class="toc-h1 toc-link ">
<a href="thrift_deps.html">Thrift</a>
</li>
<li class="toc-h1 toc-link ">
<a href="grpcio_gen.html">Python gRPC + protobufs</a>
</li>
<li class="toc-h1 toc-link ">
<a href="page.html">Markdown</a>
</li>
<li class="toc-h1 toc-heading">
Getting Help
</li>
<li class="toc-h1 toc-link ">
<a href="tshoot.html">Troubleshooting</a>
</li>
<li class="toc-h1 toc-link ">
<a href="community.html">Community</a>
</li>
<li class="toc-h1 toc-heading">
Reference
</li>
<li class="toc-h1 toc-link ">
<a href="build_dictionary.html">Pants BUILD Dictionary</a>
</li>
<li class="toc-h1 toc-link ">
<a href="options_reference.html">Pants Reference</a>
</li>
<li class="toc-h1">
<a class="sidebar-nav-heading" class="toc-drop" data-toggle="collapse" href="#Release-Notes" aria-expanded="false" aria-controls="Release-Notes">Release Notes<span class="caret"></span></a>
<ul class="collapse sidebar-nav sidebar-submenu" id="Release-Notes">
<li class="toc-h1 toc-link ">
<a href="notes-1.30.x.html">1.30.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.29.x.html">1.29.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.28.x.html">1.28.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.27.x.html">1.27.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.26.x.html">1.26.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.25.x.html">1.25.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.24.x.html">1.24.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.23.x.html">1.23.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.22.x.html">1.22.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.21.x.html">1.21.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.20.x.html">1.20.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.19.x.html">1.19.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.18.x.html">1.18.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.17.x.html">1.17.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.16.x.html">1.16.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.15.x.html">1.15.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.14.x.html">1.14.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.13.x.html">1.13.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.12.x.html">1.12.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.11.x.html">1.11.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.10.x.html">1.10.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.9.x.html">1.9.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.8.x.html">1.8.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.7.x.html">1.7.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.6.x.html">1.6.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.5.x.html">1.5.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.4.x.html">1.4.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.3.x.html">1.3.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.2.x.html">1.2.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.1.x.html">1.1.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.0.x.html">1.0.x Stable Releases</a>
</li>
</ul>
</li>
<li class="toc-h1 toc-heading">
Developer
</li>
<li class="toc-h1 toc-link ">
<a href="dev.html">Pants Developer Center</a>
</li>
<li class="toc-h1 toc-link ">
<a href="export.html">Export Format</a>
</li>
<li class="toc-h1 toc-link ">
<a href="architecture.html">Architecture</a>
</li>
<li class="toc-h1 toc-heading">
Blogs
</li>
<li class="toc-h1 toc-link ">
<a href="coursier_migration.html">Twitter's Coursier Migration</a>
</li>
</ul>
</div> <!-- site-toc -->
</div>
<div class="col-md-8">
<div class="content">
<div class="mainflow">
<nav class="pagetoc">
<ul>
<li class="toc-h1"><a href="#a-hello-world-plugin">A "Hello World" plugin</a></li>
<li class="toc-h1"><a href="#simple-configuration">Simple Configuration</a></li>
<li class="toc-h1"><a href="#examples-from-twittercommons">Examples from twitter/commons</a></li>
</ul>
</nav>
<!-- main content start -->
<!-- generated by pants! -->
<style>
.codehilite .hll { background-color: #ffffcc }
.codehilite { background: #f0f0f0; }
.codehilite .c { color: #60a0b0; font-style: italic } /* Comment */
.codehilite .err { border: 1px solid #FF0000 } /* Error */
.codehilite .k { color: #007020; font-weight: bold } /* Keyword */
.codehilite .o { color: #666666 } /* Operator */
.codehilite .ch { color: #60a0b0; font-style: italic } /* Comment.Hashbang */
.codehilite .cm { color: #60a0b0; font-style: italic } /* Comment.Multiline */
.codehilite .cp { color: #007020 } /* Comment.Preproc */
.codehilite .cpf { color: #60a0b0; font-style: italic } /* Comment.PreprocFile */
.codehilite .c1 { color: #60a0b0; font-style: italic } /* Comment.Single */
.codehilite .cs { color: #60a0b0; background-color: #fff0f0 } /* Comment.Special */
.codehilite .gd { color: #A00000 } /* Generic.Deleted */
.codehilite .ge { font-style: italic } /* Generic.Emph */
.codehilite .gr { color: #FF0000 } /* Generic.Error */
.codehilite .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.codehilite .gi { color: #00A000 } /* Generic.Inserted */
.codehilite .go { color: #888888 } /* Generic.Output */
.codehilite .gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */
.codehilite .gs { font-weight: bold } /* Generic.Strong */
.codehilite .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.codehilite .gt { color: #0044DD } /* Generic.Traceback */
.codehilite .kc { color: #007020; font-weight: bold } /* Keyword.Constant */
.codehilite .kd { color: #007020; font-weight: bold } /* Keyword.Declaration */
.codehilite .kn { color: #007020; font-weight: bold } /* Keyword.Namespace */
.codehilite .kp { color: #007020 } /* Keyword.Pseudo */
.codehilite .kr { color: #007020; font-weight: bold } /* Keyword.Reserved */
.codehilite .kt { color: #902000 } /* Keyword.Type */
.codehilite .m { color: #40a070 } /* Literal.Number */
.codehilite .s { color: #4070a0 } /* Literal.String */
.codehilite .na { color: #4070a0 } /* Name.Attribute */
.codehilite .nb { color: #007020 } /* Name.Builtin */
.codehilite .nc { color: #0e84b5; font-weight: bold } /* Name.Class */
.codehilite .no { color: #60add5 } /* Name.Constant */
.codehilite .nd { color: #555555; font-weight: bold } /* Name.Decorator */
.codehilite .ni { color: #d55537; font-weight: bold } /* Name.Entity */
.codehilite .ne { color: #007020 } /* Name.Exception */
.codehilite .nf { color: #06287e } /* Name.Function */
.codehilite .nl { color: #002070; font-weight: bold } /* Name.Label */
.codehilite .nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */
.codehilite .nt { color: #062873; font-weight: bold } /* Name.Tag */
.codehilite .nv { color: #bb60d5 } /* Name.Variable */
.codehilite .ow { color: #007020; font-weight: bold } /* Operator.Word */
.codehilite .w { color: #bbbbbb } /* Text.Whitespace */
.codehilite .mb { color: #40a070 } /* Literal.Number.Bin */
.codehilite .mf { color: #40a070 } /* Literal.Number.Float */
.codehilite .mh { color: #40a070 } /* Literal.Number.Hex */
.codehilite .mi { color: #40a070 } /* Literal.Number.Integer */
.codehilite .mo { color: #40a070 } /* Literal.Number.Oct */
.codehilite .sa { color: #4070a0 } /* Literal.String.Affix */
.codehilite .sb { color: #4070a0 } /* Literal.String.Backtick */
.codehilite .sc { color: #4070a0 } /* Literal.String.Char */
.codehilite .dl { color: #4070a0 } /* Literal.String.Delimiter */
.codehilite .sd { color: #4070a0; font-style: italic } /* Literal.String.Doc */
.codehilite .s2 { color: #4070a0 } /* Literal.String.Double */
.codehilite .se { color: #4070a0; font-weight: bold } /* Literal.String.Escape */
.codehilite .sh { color: #4070a0 } /* Literal.String.Heredoc */
.codehilite .si { color: #70a0d0; font-style: italic } /* Literal.String.Interpol */
.codehilite .sx { color: #c65d09 } /* Literal.String.Other */
.codehilite .sr { color: #235388 } /* Literal.String.Regex */
.codehilite .s1 { color: #4070a0 } /* Literal.String.Single */
.codehilite .ss { color: #517918 } /* Literal.String.Symbol */
.codehilite .bp { color: #007020 } /* Name.Builtin.Pseudo */
.codehilite .fm { color: #06287e } /* Name.Function.Magic */
.codehilite .vc { color: #bb60d5 } /* Name.Variable.Class */
.codehilite .vg { color: #bb60d5 } /* Name.Variable.Global */
.codehilite .vi { color: #bb60d5 } /* Name.Variable.Instance */
.codehilite .vm { color: #bb60d5 } /* Name.Variable.Magic */
.codehilite .il { color: #40a070 } /* Literal.Number.Integer.Long */
</style>
<h1 id="developing-a-pants-plugin">Developing a Pants Plugin</h1>
<p>This page documents how to develop a Pants plugin, a set of code that
defines new Pants functionality. If you
<a href="dev_tasks.html">develop a new task</a>
or target to add to Pants (or to
override an existing part of Pants), a plugin gives you a way to
register your code with Pants.</p>
<p>Much of Pants' own functionality is organized in plugins; see them in
<a href="https://github.com/pantsbuild/pants/tree/master/src/python/pants/backend"><code>src/python/pants/backend/*</code></a>.</p>
<p>A plugin registers its functionality with Pants by defining some
functions in a <code>register.py</code> file in its top directory. For example,
Pants' <code>jvm</code> code registers in
<a href="https://github.com/pantsbuild/pants/blob/master/src/python/pants/backend/jvm/register.py">src/python/pants/backend/jvm/register.py</a>
Pants' backend-loader code assumes your plugin has a <code>register.py</code> file
there.</p>
<h2 id="a-hello-world-plugin">A "Hello World" plugin</h2>
<p>This "Hello World" plugin example shows how to register a plugin with Pants. It defines a new
<code>hello-world</code> goal with two tasks. Here's how to create it:</p>
<ul>
<li>
<p>If you don't have an existing Pants project to work with, create one. Locate its config file,
typically <code>pants.toml</code> in the repo root.</p>
</li>
<li>
<p>Create a directory for your plugins. In this example we will use the <code>plugins/</code> directory in
the repo root, but there is no convention, and you can put them wherever you like.</p>
</li>
<li>
<p>In the <code>plugins/</code> directory, create following filesystem structure:</p>
<div class="codehilite"><pre><span></span>hello/
__init__.py
register.py
tasks/
__init__.py
your_tasks.py
</pre></div>
</li>
<li>
<p><code>__init__.py</code> files can be empty - you're just saying to Python that you created modules.</p>
</li>
<li>
<p>In <code>your_tasks.py</code> place the following content:</p>
<div class="codehilite"><pre><span></span><span class="kn">from</span> <span class="nn">pants.task.task</span> <span class="kn">import</span> <span class="n">Task</span>
<span class="k">class</span> <span class="nc">HelloTask</span><span class="p">(</span><span class="n">Task</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Hello"</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">WorldTask</span><span class="p">(</span><span class="n">Task</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"world!"</span><span class="p">)</span>
</pre></div>
</li>
</ul>
<p><a href="https://github.com/pantsbuild/pants/blob/master/src/python/pants/task/task.py">Task</a> is a simple base
class for your tasks - you must implement the <code>execute</code> method.</p>
<ul>
<li>In <code>register.py</code> place the following content:<div class="codehilite"><pre><span></span><span class="kn">from</span> <span class="nn">pants.goal.goal</span> <span class="kn">import</span> <span class="n">Goal</span>
<span class="kn">from</span> <span class="nn">pants.goal.task_registrar</span> <span class="kn">import</span> <span class="n">TaskRegistrar</span> <span class="k">as</span> <span class="n">task</span>
<span class="kn">from</span> <span class="nn">hello.tasks.your_tasks</span> <span class="kn">import</span> <span class="n">HelloTask</span><span class="p">,</span> <span class="n">WorldTask</span>
<span class="k">def</span> <span class="nf">register_goals</span><span class="p">():</span>
<span class="n">Goal</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"hello-world"</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="s2">"Say hello to your world"</span><span class="p">)</span>
<span class="n">task</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'hello'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="n">HelloTask</span><span class="p">)</span><span class="o">.</span><span class="n">install</span><span class="p">(</span><span class="s1">'hello-world'</span><span class="p">)</span>
<span class="n">task</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'world'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="n">WorldTask</span><span class="p">)</span><span class="o">.</span><span class="n">install</span><span class="p">(</span><span class="s1">'hello-world'</span><span class="p">)</span>
</pre></div>
</li>
</ul>
<p>This creates a new goal named <code>hello-world</code>, and registers the two tasks to it.</p>
<ul>
<li>In <code>pants.toml</code> place the following content:<div class="codehilite"><pre><span></span><span class="k">[GLOBAL]</span>
<span class="n">pants_version</span> <span class="o">=</span> <span class="s">"1.26.0"</span>
<span class="n">pythonpath</span> <span class="o">=</span> <span class="k">["%(buildroot)s/plugins"]</span>
<span class="n">backend_packages</span> <span class="o">=</span> <span class="k">["hello"]</span>
</pre></div>
</li>
</ul>
<p><code>backend_packages</code> defines which plugins you want to use in your project.</p>
<p>If you want to use custom plugins directly from source when building in the same repo, you need
to put them on the <code>pythonpath</code> so Pants can find them. </p>
<ul>
<li>You are ready to use your plugin! First try to find your goal by typing <code>./pants goals</code>:<div class="codehilite"><pre><span></span>...
hello-world: Say hello to your world
...
</pre></div>
</li>
</ul>
<p>Now you can use your plugin by typing <code>./pants hello-world</code>:</p>
<div class="codehilite"><pre><span></span> ::
...
Executing tasks in goals: hello-world
XX:XX:XX 00:00 [hello-world]
XX:XX:XX 00:00 [hello]Hello
XX:XX:XX 00:00 [world]world!
XX:XX:XX 00:00 [complete]
SUCCESS
</pre></div>
<p>Note that to consume the custom plugin as a published artifact (say on PyPI), instead of
directly from the repo, then instead of <code>backend_packages</code> and <code>pythonpath</code> you would set <code>plugins</code>:</p>
<div class="codehilite"><pre><span></span> :::toml
[GLOBAL]
pants_version = "1.26.0"
plugins = ["myorg.hello==1.7.6"]
</pre></div>
<p>Similarly, if your custom plugin is consumed directly from the repo, but has dependencies on
published artifacts, then you list those in <code>plugins</code>:</p>
<div class="codehilite"><pre><span></span> :::toml
[GLOBAL]
pants_version = "1.26.0"
pythonpath = ["%(buildroot)s/plugins"]
backend_packages = ["hello"]
plugins = ["some.dependency==4.5.11"]
</pre></div>
<p>See below for more details. </p>
<h2 id="simple-configuration">Simple Configuration</h2>
<p>If you want to extend Pants without adding any 3rd-party libraries that aren't already referenced by
Pants, you can use the following technique using sources stored directly in your repo.
All you need to do is name the package where the plugin is defined, and the pythonpath entry to
load it from.</p>
<p>In the example below, the stock <code>JvmBinary</code> target is subclassed so that a custom task (not shown)
can consume it specifically but disregard regular <code>JvmBinary</code> instances (using <code>isinstance()</code>).</p>
<ul>
<li>
<p>Define a home for your plugins. In this example we'll use a top-level <code>plugins/</code> directory but
this is not special.</p>
</li>
<li>
<p>Create an empty <code>plugins/hadoop/targets/__init__.py</code> to define a python package structure
for your custom hadoop target type plugin. Also create empty <code>__init__.py</code> files in each
directory up to but not including the root directory of your python package layout; in this case,
just <code>plugins/hadoop/__init__.py</code>.</p>
</li>
<li>
<p>Create a python module for your custom target type in <code>plugins/hadoop/targets/hadoop_binary.py</code>:</p>
<div class="codehilite"><pre><span></span><span class="c1"># plugins/hadoop/targets/hadoop_binary.py</span>
<span class="kn">from</span> <span class="nn">pants.backend.jvm.targets.jvm_binary</span> <span class="kn">import</span> <span class="n">JvmBinary</span>
<span class="k">class</span> <span class="nc">HadoopBinary</span><span class="p">(</span><span class="n">JvmBinary</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</li>
<li>
<p>Create <code>plugins/hadoop/register.py</code> to register the elements exposed by your plugin to BUILD file
authors:</p>
<div class="codehilite"><pre><span></span><span class="c1"># plugins/hadoop/register.py</span>
<span class="kn">from</span> <span class="nn">pants.build_graph.build_file_aliases</span> <span class="kn">import</span> <span class="n">BuildFileAliases</span>
<span class="kn">from</span> <span class="nn">hadoop.targets.hadoop_binary</span> <span class="kn">import</span> <span class="n">HadoopBinary</span>
<span class="k">def</span> <span class="nf">build_file_aliases</span><span class="p">():</span>
<span class="k">return</span> <span class="n">BuildFileAliases</span><span class="p">(</span>
<span class="n">targets</span><span class="o">=</span><span class="p">{</span>
<span class="c1"># NB: This allows a HadoopBinary target to be created in a BUILD file using the</span>
<span class="c1"># hadoop_binary alias.</span>
<span class="s1">'hadoop_binary'</span><span class="p">:</span> <span class="n">HadoopBinary</span><span class="p">,</span>
<span class="p">},</span>
<span class="p">)</span>
</pre></div>
</li>
<li>
<p>In <code>pants.toml</code>, add your new plugin package to the list of backends to load when pants starts.
This instructs pants to load a module named <code>hadoop.register</code>.</p>
<div class="codehilite"><pre><span></span><span class="k">[GLOBAL]</span>
<span class="n">pythonpath</span> <span class="o">=</span> <span class="k">['%(buildroot)s/plugins']</span>
<span class="n">backend_packages</span> <span class="o">=</span> <span class="k">['hadoop']</span>
</pre></div>
</li>
</ul>
<p>Note that you can also set the PYTHONPATH in your <code>./pants</code> wrapper script, instead of in
<code>pants.toml</code>, if you have other reasons to do so. Either way, pants will look for a <code>register.py</code>
file for each backend package you list by prefixing that package with the python path; ie roughly
<code>pythonpath + backend package + register.py</code> is tried for each pythonpath prefix until a
<code>register.py</code> is found, in this example <code>plugins/hadoop/register.py</code>.</p>
<h2 id="examples-from-twittercommons">Examples from <code>twitter/commons</code></h2>
<p>For an example of a code repo with plugins to add features to Pants when building in that repo,
take a look at <a href="https://github.com/twitter/commons"><code>twitter/commons</code></a>, especially its
<a href="https://github.com/twitter/commons/tree/32011ab5351fea23e8c70e24e752540b06d1389f/pants-plugins"><code>pants-plugins</code> directory</a>.</p>
<p>The repo's <a href="https://github.com/twitter/commons/blob/32011ab5351fea23e8c70e24e752540b06d1389f/pants.ini"><code>pants.ini</code> file</a> (the format used before <code>pants.toml</code>) has a
<code>backend_packages</code> entry listing the plugin packages (packages with <code>register.py</code> files):</p>
<div class="codehilite"><pre><span></span><span class="k">[GLOBAL]</span>
<span class="na">pythonpath: [</span>
<span class="na">"%(buildroot)s/pants-plugins/src/python",</span>
<span class="na">]</span>
<span class="na">backend_packages: [</span>
<span class="na">'twitter.common.pants.jvm.extras',</span>
<span class="na">'twitter.common.pants.python.commons',</span>
<span class="na">'pants.contrib.python.checks',</span>
<span class="na">]</span>
</pre></div>
<p>The <a href="https://github.com/twitter/commons/blob/master/pants-plugins/src/python/twitter/common/pants/jvm/extras/register.py"><code>...jvm/extras/register.py</code></a>
file registers a <code>checkstyle</code> goal. To find the code for this task, come back to the
<code>pantsbuild/pants</code> repo: Pants defines the
<a href="https://github.com/pantsbuild/pants/blob/master/src/python/pants/backend/jvm/tasks/checkstyle.py"><code>Checkstyle</code> task class</a> but doesn't register it.
But other Pants workspaces can register it, as <code>twitter/commons</code> illustrates.</p>
<!-- main content end -->
<div class="generated">
Generated by <a href="docs.html">publish_docs</a>
from dist/markdown/html/src/docs/howto_plugin.html 2022-12-03T01:08:59.530128
</div>
</div> <!-- mainflow -->
</div> <!-- content -->
</div>
<div class="col-md-1">
</div>
</div> <!-- row -->
</div> <!-- container-fluid -->
</div> <!-- page -->
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha384-I6F5OKECLVtK/BL+8iSLDEHowSAfUo76ZL9+kGAgTRdiByINKJaqTPH/QVNS1VDb" crossorigin="anonymous"></script>
<script src="bootstrap-custom.min.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-78111411-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>