Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maven vs sbt #13

Open
cjuexuan opened this issue Apr 13, 2016 · 0 comments
Open

maven vs sbt #13

cjuexuan opened this issue Apr 13, 2016 · 0 comments
Labels

Comments

@cjuexuan
Copy link
Member

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进行项目管理

配置版本,组织:

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

@cjuexuan cjuexuan added the sbt label Apr 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant