将创建jar文件的ant脚本转换为sbt

时间:2015-07-03 14:54:22

标签: ant sbt sbt-assembly

我有一个名为jarPLCExample.xml的ant文件,它接受一些类文件并生成一个jar文件。在sbt中,此文件的<jar>标记部分会是什么样的?

<project name="jar_myplc" default="jar_myplc" basedir=".">

<property file="../resources/v2.properties"/>

<property name="dist.dir" value="C://where_jar_files_go/lib"/>
<property name="dist.file" value="${dist.dir}/PLC.jar"/>

<target name="jar_myplc">
  <tstamp>
    <format property="TODAY" pattern="dd/MM/yyyy hh:mm aa" locale="au"/>
  </tstamp>
  <delete file="${dist.file}" quiet="true"/>
  <jar destfile="${dist.file}">
    <fileset dir="${v2.out.root}" includes="com/companyname/server/applic/**/*.class"/>  
    <fileset dir="${v2.out.root}" includes="com/companyname/common/utils/SeaDef*.class"/>  
  </jar>
  <copy file="${dist.file}" todir="C:/deploy"/>        
</target>

sbt-assembly对此没有好处,因为它似乎可以排除和包含jar,但不需要在此处完成源文件包和源文件名。

显然重要的是&#39;顺其自然:我希望sbt能够从源代码编译,所以不需要显式使用.class文件作为上面的ant任务。

我已经在基本目录中有一个build.sbt,调用这个文件jarPLCExample.sbt是明智的,但我不知道如何让sbt加载一个特定的文件。因此,我将在一个文件中有两个项目,并通过更改其顺序手动设置当前项目。 (因为它们都有相同的密钥,第二个似乎覆盖了第一个密钥 - projects命令将始终只显示一个项目)。

1 个答案:

答案 0 :(得分:0)

这个答案有两个缺点。它只包含applic目录,而不是applic下面的所有目录(递归)。此外,它将获取所有SeaDef*.class,无论它们在哪个目录中。

def filter3(file: File, greatGrandParent:String, grandParent:String, parent:String, excludes:List[String]):Boolean = {
  file.getParentFile.getName == parent && 
  file.getParentFile.getParentFile.getName == grandParent && 
  file.getParentFile.getParentFile.getParentFile.getName == greatGrandParent && 
  !excludes.exists( _ == file.getName)      
}

/*
 * <fileset dir="${v2.out.root}" includes="com/companyname/server/applic/**/*.class"/>  
 * <fileset dir="${v2.out.root}" includes="com/companyname/common/utils/SeaDef*.class"/>
 */    
lazy val jarPLCExample = project.in(file(".")).
  settings(commonSettings: _*).
  settings(
    includeFilter in (Compile, unmanagedSources) := 
      new SimpleFileFilter(file => filter3(file, "companyname", "server", "applic", List())) ||
      new SimpleFileFilter(file => file.getName.startsWith("SeaDef"))
  )
相关问题