cjuexuan / mynote

236 stars 34 forks source link

maven vs sbt #16

Open cjuexuan opened 8 years ago

cjuexuan commented 8 years ago

sbt vs maven

  1. sbt项目用ivy2,可以使用maven的包
  2. sbt可以进行增量编译,这个特性还是非常有吸引力,因为scala的编译速度是堪比c++的
  3. sbt提供了一个scala的console,并且导入默认的一些包,非常方便的交互
  4. 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进行项目管理

配置版本,组织:

<groupId>com.ximalaya</groupId>
<artifactId>testsbtpublish</artifactId>
<version>1.0-SNAPSHOT</version>
organization := "com.ximalaya"

name := "testsbtpublish"

version := "1.0-SNAPSHOT"

加入额外的源:

    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala-Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </repository>
    </repositories>
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

        <sourceDirectory>src/main/scala</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resource</directory>
            </resource>
            <resource>
                <directory>src/main/conf</directory>
            </resource>
        </resources>
scalaSource in Compile := baseDirectory.value / "src/main/scala"

scalaSource in Test := baseDirectory.value / "src/test"

unmanagedResourceDirectories in Compile += baseDirectory.value / "src/main/resources"

导入依赖包,并排除不需要的jar包

        <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>
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))

发布:

    <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>
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

测试:

<dependency>
    <groupId>org.scalatest</groupId>
    <artifactId>scalatest_2.11</artifactId>
    <version>3.0.0-M16-SNAP3</version>
    <scope>test</scope>
</dependency>
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")