Locked History Actions

Diff for "sbt/dt_Classpaths"

Differences between revisions 1 and 2
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
Classpaths, sources, and resources ここでは、sbtがどのようにしてcompile,run,testのようなアクションについてクラスパスを構築するかを説明し、
また、これらのオーバライドあるいは拡張の方法について説明する。
Line 7: Line 8:
    Page History == 基本事項 ==
Line 9: Line 10:
This page discusses how sbt builds up classpaths for different actions, like compile, run, and test and how to override or augment these classpaths.
Basics
sbt0.10以降では、classpathはScalaライブラリを含み、(依存で宣言されているならば)Scalaコンパイラをも含む。
Classpath関連のSettingは通常Classpathタイプの値を提供するが、これはSeq[Attributed[File]]の別名になっている。Attributedというのは、それぞれのクラスパスエントリのヘテロジニアスなマップを構成するものである(訳注:?)。
Currently, this allows sbt to associate the Analysis resulting from compilation with the corresponding classpath entry and for managed entries, the ModuleID and Artifact that defined the dependency.
Line 12: Line 14:
In sbt 0.10 and later, classpaths now include the Scala library and (when declared as a dependency) the Scala compiler. Classpath-related settings and tasks typically provide a value of type Classpath. This is an alias for Seq[Attributed[File]]. Attributed is a type that associates a heterogeneous map with each classpath entry. Currently, this allows sbt to associate the Analysis resulting from compilation with the corresponding classpath entry and for managed entries, the ModuleID and Artifact that defined the dependency.

To explicitly extract the raw Seq[File], use the files method implicitly added to Classpath:
明示的に生のSeq[File]を取得するには、Classpathに暗黙的に追加されたfilesメソッドを使用する。
{{{
Line 18: Line 18:
}}}
Line 19: Line 20:
To create a Classpath from a Seq[File], use classpath and to create an Attributed[File] from a File, use Attributed.blank: Seq[File]からClasspathを作成するにはclasspathメソッドを使い、
FileからAttributed[File]を作成するには、Attribugted.blankを使えばいい。
Line 21: Line 23:
{{{
Line 26: Line 29:
}}}
Line 27: Line 31:
Unmanaged v. managed == 非管理下(Unmanaged)と管理下(managed) ==
Line 29: Line 33:
Classpaths, sources, and resources are separated into two main categories: unmanaged and managed. Unmanaged files are manually created files that are outside of the control of the build. They are the inputs to the build. Managed files are under the control of the build. These include generated sources and resources as well as resolved and retrieved dependencies and compiled classes. クラスパス、ソース、リソースは、unmanagedとmanagedという二つのメインカテゴリに分割できる。
unmanagedファイルはマニュアルで作成されたファイルであり、ビルドのコントロール外にあり、それらはビルドの入力になる。
managedファイルはビルドのコントロール下にある。これらには、生成されたソースやリソース、解決され取得された依存、コンパイル済classがある。
Line 31: Line 37:
Tasks that produce managed files should be inserted as follows: managedファイルを生成するタスクは以下のように追加することができる。
Line 33: Line 39:
{{{
Line 36: Line 43:
}}}
Line 37: Line 45:
In this example, generate is some function of type File => Seq[File] that actually does the work. The <+= method is like +=, but allows the right hand side to have inputs (like the difference between := and <<=). So, we are appending a new task to the list of main source generators (sourceGenerators in Compile). この例においてgenerateとは「File => Seq[File]」型の何らかの関数であり、その仕事を実際に行うものだ。
<+=メソッドは+=のようなものだが、右辺に何らかの入力を許す(ちょうど:=と<<=の違いと同じ)。
このようにして、メインのソースジェネレータ(sourceGenerators in Compile)のリストに新たなタスクを追加することができる。
Line 39: Line 49:
To insert a named task, which is the better approach for plugins:
プラグインの場合には、名前付きのタスクを追加したほうがよいだろう。
{{{
Line 46: Line 56:
}}}
Line 47: Line 58:
where mySourceGenerator is defined as:
mySourceGeneratorは以下のように定義されている。
{{{
Line 50: Line 61:
}}}
Line 57: Line 69:
{{{
Line 59: Line 71:
}}}
Line 70: Line 83:
    unmanaged-classpath
    managed-classpath
    external-dependency-classpath
    internal-dependency-classpath
 *unmanaged-classpath
 *managed-classpath
 *external-dependency-classpath
 *internal-dependency-classpath
Line 93: Line 106:
{{{
Line 94: Line 108:
}}}
Line 96: Line 111:
{{{
Line 98: Line 113:

    Home - Overview of sbt
    Getting Started Guide - START HERE
    FAQ - Questions, answered.
    Index - Find types, values, and methods
    Community - source, forums, releases
    Examples
    Detailed Topics - deep dive docs
        Artifacts what to publish
        Best Practices
        Classpaths
        Command Line Reference
        Compiler Plugins
        Console Project
        Cross Build
        Forking
        Global Settings
        Inspecting Settings
        Java Sources
        Launcher
        Library Management
        Local Scala
        Mapping Files
        Migrating to 0.10+
        Parsing Input
        Paths
        Process
        Publishing
        Resolvers
        Running Project Code
        Scripts
        Setup Notes
        Tasks
        TaskInputs
        Testing
        Triggered Execution
        Update Report
    Extending sbt - internals docs
}}}

クラスパス、ソース、リソース

https://github.com/harrah/xsbt/wiki/Classpathsの訳(2011/10/30時点)

ここでは、sbtがどのようにしてcompile,run,testのようなアクションについてクラスパスを構築するかを説明し、 また、これらのオーバライドあるいは拡張の方法について説明する。

基本事項

sbt0.10以降では、classpathはScalaライブラリを含み、(依存で宣言されているならば)Scalaコンパイラをも含む。 Classpath関連のSettingは通常Classpathタイプの値を提供するが、これはSeq[Attributed[File]]の別名になっている。Attributedというのは、それぞれのクラスパスエントリのヘテロジニアスなマップを構成するものである(訳注:?)。 Currently, this allows sbt to associate the Analysis resulting from compilation with the corresponding classpath entry and for managed entries, the ModuleID and Artifact that defined the dependency.

明示的に生のSeq[File]を取得するには、Classpathに暗黙的に追加されたfilesメソッドを使用する。

val cp: Classpath = ...
val raw: Seq[File] = cp.files

Seq[File]からClasspathを作成するにはclasspathメソッドを使い、 FileからAttributed[File]を作成するには、Attribugted.blankを使えばいい。

val raw: Seq[File] = ...
val cp: Classpath = raw.classpath

val rawFile: File = ..
val af: Attributed[File] = Attributed.blank(rawFile)

非管理下(Unmanaged)と管理下(managed)

クラスパス、ソース、リソースは、unmanagedとmanagedという二つのメインカテゴリに分割できる。 unmanagedファイルはマニュアルで作成されたファイルであり、ビルドのコントロール外にあり、それらはビルドの入力になる。 managedファイルはビルドのコントロール下にある。これらには、生成されたソースやリソース、解決され取得された依存、コンパイル済classがある。

managedファイルを生成するタスクは以下のように追加することができる。

sourceGenerators in Compile <+= sourceManaged in Compile map { out =>
        generate(out / "some_directory")
}

この例においてgenerateとは「File => Seq[File]」型の何らかの関数であり、その仕事を実際に行うものだ。 <+=メソッドは+=のようなものだが、右辺に何らかの入力を許す(ちょうど:=と<<=の違いと同じ)。 このようにして、メインのソースジェネレータ(sourceGenerators in Compile)のリストに新たなタスクを追加することができる。

プラグインの場合には、名前付きのタスクを追加したほうがよいだろう。

sourceGenerators in Compile <+= (mySourceGenerator in Compile).task

mySourceGenerator in Compile <<= sourceManaged in Compile map { out =>
    generate(out / "some_directory")
}

mySourceGeneratorは以下のように定義されている。

val mySourceGenerator = TaskKey[Seq[File]](...)

The task method is used to refer to the actual task instead of the result of the task.

For resources, there are similar keys resourceGenerators and resourceManaged. Excluding source files by name

The project base directory is by default a source directory in addition to src/main/scala. You can exclude source files by name (butler.scala in the example below) like:

excludeFilter in unmanagedSources := "butler.scala" 

Read more on How to exclude .scala source file in project folder - Google Groups External v. internal

Classpaths are also divided into internal and external dependencies. The internal dependencies are inter-project dependencies. These effectively put the outputs of one project on the classpath of another project.

External classpaths are the union of the unmanaged and managed classpaths. Keys

For classpaths, the relevant keys are:

  • unmanaged-classpath
  • managed-classpath
  • external-dependency-classpath
  • internal-dependency-classpath

For sources:

  • unmanaged-sources These are by default built up from unmanaged-source-directories, which consists of scala-source and java-source. managed-sources These are generated sources. sources Combines managed-sources and unmanaged-sources. source-generators These are tasks that generate source files. Typically, these tasks will put sources in the directory provided by source-managed.

For resources

  • unmanaged-resources These are by default built up from unmanaged-resource-directories, which by default is resource-directory, excluding files matched by default-excludes. managed-resources By default, this is empty for standard projects. sbt plugins will have a generated descriptor file here. resource-generators These are tasks that generate resource files. Typically, these tasks will put resources in the directory provided by resource-managed.

Use the inspect command for more details. Example

You have a standalone project which uses a library that loads xxx.properties from classpath at run time. You put xxx.properties inside directory "config". When you run "sbt run", you want the directory to be in classpath.

unmanagedClasspath in Runtime <<= (unmanagedClasspath in Runtime, baseDirectory) map { (cp, bd) => cp :+ Attributed.blank(bd / "config") }

Or shorter:

unmanagedClasspath in Runtime <+= (baseDirectory) map { bd => Attributed.blank(bd / "config") }