play framework + Primer sbt environment to build

Use of a sbt

SBT = (not so) Simple Build Tool, is scala build tool, the same as the status of java maven. It is designed to allow simple project can be a simple configuration, and complex projects can be complex configurations.

1 sbt project directory

And maven, like, sbt have agreed to a common directory structure is much simpler to use the agreed structure will work behind. Project configuration can be defined in build.sbt file can also be defined in the base / project / build.scala file, under normal circumstances build.sbt is sufficient, unless the multi-project or need a lot of special items defined

Base /   
     build.sbt // build configuration file 
     / project // also build part of the configuration 
          build.scala // advanced configuration, optional 
     / src 
         / main 
         / Scala 
         / the Java 
         / Resources 
     / the Test 
         / Scala 
         / the Java 
         / Resources 

 Base behalf of the project the root directory

2.sbt command (if mvn command)

sbt What commands are available? Enter the help command a query that lists a bunch of commands available, such as exit, reload, etc., do not know the role of a command? help command name, such as input display help exit exit command role.

Commands listed in not commonly used commands compile, test, etc.? Because those are not sbt command instead of the current task of the project. Enter the command tasks, you can see compile, test, package, and so the task explained.

You want to view the configuration of the project? With the show command, enter the show name, see the name of the current project, the current project to see the show libraryDependencies input dependent libraries, libraryDependencies too long to remember? After entering the lib press the tab key! Interactive window there is a tab tips! Enter help show, you can see the role of show command displays a configured value, if after the show with that task, perform the task and display the results of task execution. You can try to see what the results show compile, if you do not want to execute compile, but want to see the explanation command, use the command inspect, inspect command is more complex, the result of the implementation of the output is also more complex, specifically what is the role of command Please help inspect, but have to wait to understand the meaning of the build definition in order to help understand what to say yes. . .

Common tasks, there are compile, test, run, package, doc, please help yourself or the name implies. In addition, these tasks are often some variants, such as package-doc, package-src, etc., view the task with the tasks list of commands, there must be a right for you

There is a particularly strong mandate had brought out about it: console 
input console carriage return, will start a REPL in the current session, do not tell me you do not know scala REPL is the interpreter of the meaning. . . It is that you enter into the scala after a carriage return at the command line interface.

Powerful is, sbt will load your project depends on all the jar package and your own code! You can test your semi-finished products in this interpreter. My template project contains a sample / Account.scala file, a dozen lines of code is very simple, you can look at, and then play Account Account associated classes and objects in the console window, but do not forget to import sample._

Because the dependent jar package also been loaded, so for those of you not familiar with possible third-party libraries, you can have lots of fun in the console in! This function is to force, with who knows who.

Incidentally, in the way, there are three sbt command execution modes: 
1, interactive, i.e., the above described 
2, batch-type, i.e., enter the command name sbt performed at the command line, such as the compile will sbt compiled code, without entering interactive mode 
3, endless style, in front of the command name with ~ No, that will enter the endless mode, such as ~ compile, compiles the current code, and then listen for the code change, whenever you edit the code and save after, sbt will automatically compile the code, ~ test, too, when you modify the code to automatically compile and run unit tests. Press Enter to exit this mode.

 

3.build definition Interpretation

You should be tried in front of show name and show libraryDependencies, right? The results show that out from your build.sbt file, which is a build definition. Open build.sbt you can see the name: = "sbt11template" There are other bunch xxx: = xxxx, it is clear that this is a key-value pair, sbt is to read the configuration file and build a key-value of . map but not key in build.sbt inside: = value, but the key:. = expression of each line in the file is actually a scala statement, you can not try to put 
name: = "sbt11template" changed 
name: = { "sbt11template" .toUpperCase} 
and then reload, and then show name, you will see capitalize the sBT11TEMPLATE

: = Is the most common method is to effect expression of the key set to the value, if the same key is assigned a plurality of times, the value of the latter will overwrite the previous ones. For simple type of key, such as name, version and so on.

Other common methods are 
+ =, the value added to the current value, the set suitable for the type of key, such as libraryDependencies

+ + =, A collection of value added in the current collection

~ =, The cattle X, you write a function in the back ~ = ~ = the current value of the key to pass your function, then the function result as a new value, for example, you can name: = xxx back again a 
name ~ = {_. toUpperCase}, as the name is capitalized

<< = The value assigned to the current key to another key, such as auther << = name, this method there is advanced usage, you can combine a number of other key values, and assigned to the current key, with documentation examples

name <<= (name, organization, version) { (n, o, v) => "project " + n + " from " + o + " version " + v }
  • 1

Also apply to the collection type version 
<+ = and <= ++ 
these official documents grammar in this 
https://github.com/harrah/xsbt/wiki/Getting-Started-More-About-Settings

 

4.

Dependency Management

For third-party libraries do not intend to official repository management, build a lib directory under the project directory, the jar package thrown into the herd.

Sbt hope to be managed at build.sbt was added with the following syntax

libraryDependencies += groupID % artifactID % revision % configuration
  • 1

% Configuration is optional, represents a dependent libraries only in specific configurations, such as the template in the project "org.specs2" %% "specs2"% "1.7.1"% "test" is a unit testing framework, only It needs test.

If your eyesight is good, you will see where there is a %%, instead of one per cent, which indicates that the requested sbt scala looking compiled with the current version of your jar package configuration, since the translation of the results of different versions scala be incompatible (tragedy), scala hope the community will solve the incompatibility problems. . .

Dependence on the java language to write a jar library, this is no problem, such as libraryDependencies + = "org.slf4j"% "slf4j-api"% "1.6.4" do not need a %%

Once configured dependency, operational sbt update, sbt automatically to the official maven repository and scala library jar to find those packages and downloaded to .ivy2 directory of your user directory inside a different project if you used the same library, sbt downloaded once is enough.

If you want to download from the repository sbt your own configuration, the use of this syntax:

resolvers += name at location
  • 1

such as

resolvers += "Scala-Tools Maven2 Snapshots Repository" at "http://scala-tools.org/repo-snapshots"
  • 1

Everything is configured through the key classes, key list in http://harrah.github.com/xsbt/latest/sxr/Keys.scala.html 

The use of two play

1 .sbt download and install (slightly)

2. paly framework installation

In fact, there is no need to install, just beginning to build the project directly from the corresponding template

If the idea is the Ultimate version of the charges, do nothing, you can build the project directly play2

If the community edition, that would not work, it can only be used by importing

(1) create a project, depending on the template g8

Java seed template

sbt new playframework/play-java-seed.g8

Scala seed template

sbt new playframework/play-scala-seed.g8

(2) can now enter the project name, organization name, Scala version, Play version, SBT Version:

(3) try to run the project, sbt run directly run

Sbt into the environment or the command line, then enter the run

(4) idea import item by introducing build sbt

(5) waiting for the completion of loading the depending sbt

(6) sbt shell or sbt run run run sbt run sbt console

You can open the browser to http: // localhost: 9090 saw welcome to play

(7) compiler command test with compile, test command

Guess you like

Origin www.cnblogs.com/linjiaqin/p/11058408.html