/
Scripts.html
256 lines (237 loc) · 14.6 KB
/
Scripts.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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Scripts, REPL, and Dependencies — sbt Documentation</title>
<link rel="stylesheet" href="../_static/style.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/issuetracker.css" type="text/css" />
<link rel="stylesheet" href="../_static/base.css" type="text/css" />
<link rel="stylesheet" href="../_static/docs.css" type="text/css" />
<link rel="stylesheet" href="../_static/syntax.css" type="text/css" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Exo:300,400,600,700" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '0.12.2-RC2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../../../versions.js"></script>
<script type="text/javascript" src="../_static/set-versions.js"></script>
<link rel="top" title="sbt Documentation" href="../index.html" />
<link rel="up" title="Detailed Topics" href="index.html" />
<link rel="next" title="Setup Notes" href="Setup-Notes.html" />
<link rel="prev" title="Running Project Code" href="Running-Project-Code.html" />
</head>
<body>
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<ul class="nav">
<li><a href="http://www.scala-sbt.org/">Home</a></li>
<li><a href="../index.html">Documentation</a></li>
<li><a href="../Getting-Started/Setup.html">Download</a></li>
<li><a href="http://groups.google.com/group/simple-build-tool">Mailing List</a></li>
<li><a href="http://github.com/sbt/sbt">Project</a></li>
</ul>
</div>
</div>
</div>
<div class="main">
<div class="container">
<div class="page-title">Scripts, REPL, and Dependencies</div>
<div class="search-box">
<form method="get" action="http://www.google.com/search" id="search">
<input type="search" placeholder="Search 0.12.2-RC2 on Google" name="q" results="10" size="29" id="search-input">
<input type="hidden" name="sitesearch" value="http://www.scala-sbt.org/release/docs/">
<input type="submit" id="submit-search">
</form>
</div>
</div>
<div class="main-container">
<div class="container">
<div class="row">
<div class="span12">
<ul class="breadcrumb">
<li>
<span class="divider">|</span> <a href="Setup-Notes.html">Setup Notes</a> <span class="divider">»</span>
</li>
<li>
<span class="divider">></span><a href="index.html">Detailed Topics</a>
</li>
<li>
<a href="../index.html">Contents</a>
</li>
<li>
<span class="divider">«</span> <a href="Running-Project-Code.html">Running Project Code</a> <span class="divider">|</span>
</li>
<li>
<select id="versions"><option selected value="0.12.2-RC2">Version 0.12.2-RC2</option></select>
</li>
</ul>
</div>
</div>
<div class="row">
<div class="span9">
<div class="section" id="scripts-repl-and-dependencies">
<h1>Scripts, REPL, and Dependencies<a class="headerlink" href="#scripts-repl-and-dependencies" title="Permalink to this headline">¶</a></h1>
<p>sbt has two alternative entry points that may be used to:</p>
<ul class="simple">
<li>Compile and execute a Scala script containing dependency declarations
or other sbt settings</li>
<li>Start up the Scala REPL, defining the dependencies that should be on
the classpath</li>
</ul>
<p>These entry points should be considered experimental. A notable
disadvantage of these approaches is the startup time involved.</p>
<div class="section" id="setup">
<h2>Setup<a class="headerlink" href="#setup" title="Permalink to this headline">¶</a></h2>
<p>To set up these entry points, you can either use
<a class="reference external" href="https://github.com/n8han/conscript">conscript</a> or manually construct
the startup scripts. In addition, there is a <a class="reference external" href="https://github.com/paulp/xsbtscript">setup
script</a> for the script mode that
only requires a JRE installed.</p>
<div class="section" id="setup-with-conscript">
<h3>Setup with Conscript<a class="headerlink" href="#setup-with-conscript" title="Permalink to this headline">¶</a></h3>
<p>Install <a class="reference external" href="https://github.com/n8han/conscript">conscript</a>.</p>
<div class="highlight-console"><div class="highlight"><pre><span class="go">cs sbt/sbt --branch 0.12.0</span>
</pre></div>
</div>
<p>This will create two scripts: <tt class="docutils literal"><span class="pre">screpl</span></tt> and <tt class="docutils literal"><span class="pre">scalas</span></tt>.</p>
</div>
<div class="section" id="manual-setup">
<h3>Manual Setup<a class="headerlink" href="#manual-setup" title="Permalink to this headline">¶</a></h3>
<p>Duplicate your standard <tt class="docutils literal"><span class="pre">sbt</span></tt> script, which was set up according to
<a class="reference internal" href="../Getting-Started/Setup.html"><em>Setup</em></a>, as <tt class="docutils literal"><span class="pre">scalas</span></tt> and <tt class="docutils literal"><span class="pre">screpl</span></tt> (or
whatever names you like).</p>
<p><tt class="docutils literal"><span class="pre">scalas</span></tt> is the script runner and should use <tt class="docutils literal"><span class="pre">sbt.ConsoleMain</span></tt> as
the main class, by adding the <tt class="docutils literal"><span class="pre">-Dsbt.main.class=sbt.ScriptMain</span></tt>
parameter to the <tt class="docutils literal"><span class="pre">java</span></tt> command. Its command line should look like:</p>
<div class="highlight-console"><div class="highlight"><pre><span class="go">java -Dsbt.main.class=sbt.ScriptMain -Dsbt.boot.directory=/home/user/.sbt/boot -jar sbt-launch.jar "$@"</span>
</pre></div>
</div>
<p>For the REPL runner <tt class="docutils literal"><span class="pre">screpl</span></tt>, use <tt class="docutils literal"><span class="pre">sbt.ConsoleMain</span></tt> as the main
class:</p>
<div class="highlight-console"><div class="highlight"><pre><span class="go">java -Dsbt.main.class=sbt.ConsoleMain -Dsbt.boot.directory=/home/user/.sbt/boot -jar sbt-launch.jar "$@"</span>
</pre></div>
</div>
<p>In each case, <tt class="docutils literal"><span class="pre">/home/user/.sbt/boot</span></tt> should be replaced with wherever
you want sbt's boot directory to be; you might also need to give more
memory to the JVM via <tt class="docutils literal"><span class="pre">-Xms512M</span> <span class="pre">-Xmx1536M</span></tt> or similar options, just
like shown in <a class="reference internal" href="../Getting-Started/Setup.html"><em>Setup</em></a>.</p>
</div>
</div>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
<div class="section" id="sbt-script-runner">
<h3>sbt Script runner<a class="headerlink" href="#sbt-script-runner" title="Permalink to this headline">¶</a></h3>
<p>The script runner can run a standard Scala script, but with the
additional ability to configure sbt. sbt settings may be embedded in the
script in a comment block that opens with <tt class="docutils literal"><span class="pre">/***</span></tt>.</p>
<div class="section" id="example">
<h4>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h4>
<p>Copy the following script and make it executable. You may need to adjust
the first line depending on your script name and operating system. When
run, the example should retrieve Scala, the required dependencies,
compile the script, and run it directly. For example, if you name it
<tt class="docutils literal"><span class="pre">dispatch_example.scala</span></tt>, you would do on Unix:</p>
<div class="highlight-console"><div class="highlight"><pre><span class="go">chmod u+x dispatch_example.scala</span>
<span class="go">./dispatch_example.scala</span>
</pre></div>
</div>
<div class="highlight-scala"><div class="highlight"><pre><span class="o">#!/</span><span class="n">usr</span><span class="o">/</span><span class="n">bin</span><span class="o">/</span><span class="n">env</span> <span class="n">scalas</span>
<span class="o">!</span><span class="k">#</span>
<span class="cm">/***</span>
<span class="cm">scalaVersion := "2.9.0-1"</span>
<span class="cm">libraryDependencies ++= Seq(</span>
<span class="cm"> "net.databinder" %% "dispatch-twitter" % "0.8.3",</span>
<span class="cm"> "net.databinder" %% "dispatch-http" % "0.8.3"</span>
<span class="cm">)</span>
<span class="cm">*/</span>
<span class="k">import</span> <span class="nn">dispatch.</span><span class="o">{</span> <span class="n">json</span><span class="o">,</span> <span class="nc">Http</span><span class="o">,</span> <span class="nc">Request</span> <span class="o">}</span>
<span class="k">import</span> <span class="nn">dispatch.twitter.Search</span>
<span class="k">import</span> <span class="nn">json.</span><span class="o">{</span> <span class="nc">Js</span><span class="o">,</span> <span class="nc">JsObject</span> <span class="o">}</span>
<span class="k">def</span> <span class="n">process</span><span class="o">(</span><span class="n">param</span><span class="k">:</span> <span class="kt">JsObject</span><span class="o">)</span> <span class="k">=</span> <span class="o">{</span>
<span class="k">val</span> <span class="nc">Search</span><span class="o">.</span><span class="n">text</span><span class="o">(</span><span class="n">txt</span><span class="o">)</span> <span class="k">=</span> <span class="n">param</span>
<span class="k">val</span> <span class="nc">Search</span><span class="o">.</span><span class="n">from_user</span><span class="o">(</span><span class="n">usr</span><span class="o">)</span> <span class="k">=</span> <span class="n">param</span>
<span class="k">val</span> <span class="nc">Search</span><span class="o">.</span><span class="n">created_at</span><span class="o">(</span><span class="n">time</span><span class="o">)</span> <span class="k">=</span> <span class="n">param</span>
<span class="s">"("</span> <span class="o">+</span> <span class="n">time</span> <span class="o">+</span> <span class="s">")"</span> <span class="o">+</span> <span class="n">usr</span> <span class="o">+</span> <span class="s">": "</span> <span class="o">+</span> <span class="n">txt</span>
<span class="o">}</span>
<span class="nc">Http</span><span class="o">.</span><span class="n">x</span><span class="o">((</span><span class="nc">Search</span><span class="o">(</span><span class="s">"#scala"</span><span class="o">)</span> <span class="n">lang</span> <span class="s">"en"</span><span class="o">)</span> <span class="o">~></span> <span class="o">(</span><span class="k">_</span> <span class="n">map</span> <span class="n">process</span> <span class="n">foreach</span> <span class="n">println</span><span class="o">))</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="sbt-repl-with-dependencies">
<h3>sbt REPL with dependencies<a class="headerlink" href="#sbt-repl-with-dependencies" title="Permalink to this headline">¶</a></h3>
<p>The arguments to the REPL mode configure the dependencies to use when
starting up the REPL. An argument may be either a jar to include on the
classpath, a dependency definition to retrieve and put on the classpath,
or a resolver to use when retrieving dependencies.</p>
<p>A dependency definition looks like:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">organization</span><span class="o">%</span><span class="n">module</span><span class="o">%</span><span class="n">revision</span>
</pre></div>
</div>
<p>Or, for a cross-built dependency:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">organization</span><span class="o">%%</span><span class="n">module</span><span class="o">%</span><span class="n">revision</span>
</pre></div>
</div>
<p>A repository argument looks like:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="s">"id at url"</span>
</pre></div>
</div>
<div class="section" id="id2">
<h4>Example:<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h4>
<p>To add the Sonatype snapshots repository and add Scalaz 7.0-SNAPSHOT to
REPL classpath:</p>
<div class="highlight-console"><div class="highlight"><pre><span class="go">screpl "sonatype-releases at https://oss.sonatype.org/content/repositories/snapshots/" "org.scalaz%%scalaz-core%7.0-SNAPSHOT"</span>
</pre></div>
</div>
<p>This syntax was a quick hack. Feel free to improve it. The relevant
class is
<a class="reference external" href="../../sxr/IvyConsole.scala.html">IvyConsole</a>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="span3"><p class="contents-title">Contents</p>
<div id="scroller-anchor">
<div id="scroller">
<div id="toc"><ul>
<li><a class="reference internal" href="#">Scripts, REPL, and Dependencies</a><ul>
<li><a class="reference internal" href="#setup">Setup</a><ul>
<li><a class="reference internal" href="#setup-with-conscript">Setup with Conscript</a></li>
<li><a class="reference internal" href="#manual-setup">Manual Setup</a></li>
</ul>
</li>
<li><a class="reference internal" href="#usage">Usage</a><ul>
<li><a class="reference internal" href="#sbt-script-runner">sbt Script runner</a><ul>
<li><a class="reference internal" href="#example">Example</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sbt-repl-with-dependencies">sbt REPL with dependencies</a><ul>
<li><a class="reference internal" href="#id2">Example:</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div></div>
</div>
</div>
</div>
</div>
</body>
</html>