zoukankan      html  css  js  c++  java
  • 搭建简单SBT工程实践

        在本机jdk(主要配置环境变量)、scala(主要配置环境变量)、sbt(主要配置①私服repositories  ②sbtconfig.txt)都已经安装且配置好的情况下。

    repositories:

    [repositories]
    local
      Ivy Repositories: http://xxxIP:port/repositories/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
      Maven Repositories: http://xxxIP:port/repositories/

     使用OSChina提供的Maven Center镜像库以及Ivy依赖,修改配置之前可以先备份本地原有配置。

    (参考:https://segmentfault.com/a/1190000002474507)

    [repositories]
    local
    oschina:http://maven.oschina.net/content/groups/public/ 
    oschina-ivy:http://maven.oschina.net/content/groups/public/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
    typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
    sonatype-oss-releases
    maven-central
    sonatype-oss-snapshots

    可惜使用sbt命令后,依然出现依赖下载失败的情况:

    看来ChinaOS不可用,修改为阿里云,发现可用:

    [repositories]
    local
    nexus-aliyun:http://maven.aliyun.com/nexus/content/groups/public
    nexus-aliyun-ivy:http://maven.aliyun.com/nexus/content/groups/public/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
    typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly

    sbtconfig.txt

    (官方文档:http://www.scala-sbt.org/0.13/docs/Proxy-Repositories.html):

    官方解释:http://www.scala-sbt.org/0.13/docs/Launcher-Configuration.html

    # Set the java args to high
    
    -Xmx1024M     #JVM最大允许分配的堆内存,按需分配   # Xms JVM初始分配的堆内存
    -Xss10M       #设置每个线程的堆栈大小
    
    -XX:MaxPermSize=1024m   #非堆区分配的内存的最大内存;持久代大小
    -XX:ReservedCodeCacheSize=512m   #代码缓存
    
    
    # Set the extra SBT options
    
    -Dsbt.log.format=true
    
    -Dsbt.override.build.repos=true  #This setting is used to specify that all sbt project added resolvers should be ignored in favor of those configured in the repositories configuration. Using this with a properly configured
    
    -Dsbt.ivy.home=D:/Tools/sbt_project/ivy2  #ivy的cache等,保存到指定的文件夹;不配置的话sbt下载的jar包都会默认放到C盘的用户目录下
    
    -Dfile.encoding=UTF8
    
    -Dsbt.boot.directory=D:/Tools/sbt_project/boot #The directory defined here is used to store all cached JARs resolved launcher.
    
    -Dhttp.proxyHost=xxx.xxx.com   #代理设置
    
    -Dhttp.proxyPort=xx

     第一种,建立好sbt工程,再用idea开发

    1.按sbt规范,建好project、src等文件夹以及build.sbt、plugins.sbt文件,基本结构(官方文档(有中文版):http://www.scala-sbt.org/0.13/docs/zh-cn/Organizing-Build.html):

    2.build.sbt 配置:

    name := "SimpleApp"   //任意
    
    version := "1.0"     // 任意
    
    scalaVersion := "2.10.4"   //与安装的scala版本对应

    3.plugins.sbt  为了能使用sbt gen-idea  等命令,要引入相关的插件,配置:

    addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")   //sbt gen-idea
    
    addSbtPlugin("io.spray" % "sbt-revolver" % "0.7.2")
    
    addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.5")
    
    addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")    //sbt assembly

    4.打开命令行,进入项目根目录,输入sbt gen-idea生成工程。

    5.工程开发中要使用到配置文件

    ①在resource目录下建立application.conf文件

    ②要使用到com.typesafe.config的jar包,为了让sbt托管依赖。需要在build.sbt文件中添加依赖。

    通过查http://mvnrepository.com,查到依赖写法:

    libraryDependencies += "com.typesafe" % "config" % "1.3.0"

    需要注意,com.typesafe.config的jar包是否还依赖其他jar包,如果有其他依赖,也需要引入。

    第二种:通过Idea创建sbt工程

    1.idea安装sbt插件:File->Settings->Plugins 搜索sbt,然后安装。(sbt插件版本和本地安装scala版本有关联)

    2.新建sbt工程,选择工程所在路径、输入工程名即可:

    自动生成sbt规范目录:

    自动生成的build.sbt内容:

    name := "Project_Test"

    version := "1.0"

    这样创建的工程,已经可以通过sbt命令行执行

    sbt update

    sbt compile

    sbt package ……

    还不可以执行sbt gen-idea(现在已经是idea工程,不执行gen-idea命令也没关系了)、 sbt assembly

    如果需要gen-idea,在plugins.sbt配置相关插件(修改后立即生效)即可,例如:

    addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")

    添加完成后,如果执行sbt gen-idea 命令,由于build.sbt文件中没有配置scala版本,导致会去下载默认的一个scala的一些jar包。

    在build.sbt文件中,设置scala版本: 

     scalaVersion in ThisBuild := "2.10.4"

    3.为了打包 sbt assembly   (只使用sbt的 package 命令打包,是不会把第三方库打包进去的,需要sbt的assembly pulgin,它的任务,就是负责把所有依赖的jar包都打成一个 fat jar)

    在plugins.sbt文件中配置插件,例如:

    addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")

    这样配置完后还不够,还需要在build.sbt中配置:

    import sbtassembly.Plugin.AssemblyKeys._

    assemblySettings jarName in assembly :
    = "test_project.jar" mainClass in assembly := Some("com.test.Boot") //package 相对于,例如 src/main/scala目录

    4.配置完成,进入工程所在目录,使用命令行执行sbt assembly,完成打包,包在工程目录的 argetscala-2.10下

     使用assembly打包出来,包名一般为 配置的name.jar,package打包,包名一般为 name_scalaVersion前两段-version.jar

    SimpleApp.jar
    SimpleApp_2.10-1.0.jar

    参考:

    http://www.cnblogs.com/zhangqingping/p/4997324.html

  • 相关阅读:
    MVC ORM 架构
    Kubernetes 第八章 Pod 控制器
    Kubernetes 第七章 Configure Liveness and Readiness Probes
    Kubernetes 第六章 pod 资源对象
    Kubernetes 第五章 YAML
    Kubernetes 核心组件
    Kubernetes 架构原理
    Kubernetes 第四章 kubectl
    Kubernetes 第三章 kubeadm
    yum 配置及yum 源配置
  • 原文地址:https://www.cnblogs.com/yongwangzhiqian/p/5780108.html
Copyright © 2011-2022 走看看