1. 程式人生 > >sbt與maven簡單對比

sbt與maven簡單對比

sbt vs maven

  • sbt專案用ivy2,可以使用maven的包
  • sbt可以進行增量編譯,這個特性還是非常有吸引力,因為scala的編譯速度是堪比c++的
  • sbt提供了一個scala的console,並且匯入預設的一些包,非常方便的互動
  • sbt在外掛上支援的並不是那麼好,很多maven中好用的功能,需要用額外的外掛,比如mvn dependency:tree ps:最新版的不需要plugin ,執行sbt test:compile即可,會出現在target/resolution-cache/reports下各種非常詳細的報告
sbt與maven常見命令對比
mvn sbt
install publishLocal
deploy publish
clean clean
package packge
compile compile
test test
dependency:tree using plugin
unsupport console
unsupport run
sbt與maven配置檔案對比:

maven利用pom.xml進行專案管理

配置版本,組織:

  • maven
<groupId>com.ximalaya</groupId>
<artifactId>testsbtpublish</artifactId>
<version>1.0-SNAPSHOT</version>
  • sbt
organization := "com.ximalaya"
name := "testsbtpublish"
version := "1.0-SNAPSHOT"
加入額外的源:
  • maven:
    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala-Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </repository>
    </repositories>
  • sbt:
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"

格式為:

resolvers += name at location

如果需要加入本地的maven庫:

resolvers += "Local Maven Repository" at "file://"+Path.userHome.absolutePath+"/.m2/repository"

或者簡寫為:

resolvers += Resolver.mavenLocal
build source
  • maven:
 <sourceDirectory>src/main/scala</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resource</directory>
            </resource>
            <resource>
                <directory>src/main/conf</directory>
            </resource>
        </resources>
  • sbt:
scalaSource in Compile := baseDirectory.value / "src/main/scala"
scalaSource in Test := baseDirectory.value / "src/test"
unmanagedResourceDirectories in Compile += baseDirectory.value / 
"src/main/resources"
匯入依賴包,並排除不需要的jar包
  • maven:
<dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.10</artifactId>
            <version>${spark.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
  • sbt:
libraryDependencies ++= Seq(
    "org.apache.spark" % "spark-core_2.11" % "1.5.0"  
    )
"log4j" % "log4j" % "1.2.15" excludeAll(
ExclusionRule(organization = "com.sun.jdmk"),
ExclusionRule(organization = "com.sun.jmx"),
ExclusionRule(organization = "javax.jms")

其中:

def exclude(org: String, name: String) = excludeAll(ExclusionRule(org, name))

釋出:

  • maven:
  <distributionManagement>
        <repository>
            <id>artifactory</id>
            <name>xxxxxx</name>
            <url>http://artifactory.xxxxxx.com/artifactory/xxxxxx/</url>
        </repository>
        <snapshotRepository>
            <id>artifactory</id>
            <name>xxxxxx</name>
            <url>http://artifactory.xxxxxxx.com/artifactory/xxxxxxx/</url>
        </snapshotRepository>
    </distributionManagement>
  • sbt:
publishTo := {
  val nexus = "http://artifactory.xxxxxx.com/artifactory/"
  if (isSnapshot.value)
    Some("snapshots" at nexus + "snapshots")
  else
    Some("releases"  at nexus + "releases")
}
//授權
credentials += Credentials(Path.userHome / ".ivy2" / ".credentials")

其中授權檔案為:

realm=Artifactory Realm
host=host
user=userName
password=passWord
測試:
  • maven:
<dependency>
    <groupId>org.scalatest</groupId>
    <artifactId>scalatest_2.11</artifactId>
    <version>3.0.0-M16-SNAP3</version>
    <scope>test</scope>
</dependency>
  • sbt:
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "3.0.0-M16-SNAP3" % "test"
sbt plugins

全域性plugins:~/.sbt/0.13/plugins/plugin.sbt 樹形:addSbtPlugin(“net.virtual-void” % “sbt-dependency-graph” % “0.8.2”)