0

I have an ant file called jarPLCExample.xml that takes some class files and produces a jar file. What would the <jar> tag section of this file look like in sbt?

<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 no good for this as it seems you can exclude and include jars, but not source file packages and source file names as needs to be done here.

Obviously important to 'go with the flow': I expect sbt will want to compile from source, so no need to explicitly use .class files as the ant task above does.

I already have a build.sbt in the base directory, and it would be sensible to call this file jarPLCExample.sbt, but I don't know how to get sbt to load one particular file. So instead I will have two projects in the one file and manually set the current project by changing their order. (As they both have the same key the second one seems to overwrite the first one - the projects command will always show only one project).

Chris Murphy
  • 6,411
  • 1
  • 24
  • 42

1 Answers1

0

This answer has two shortcomings. It only includes the applic directory rather than all directories (recursively) below applic. Also it will pick up all SeaDef*.class, no matter which directories they are in.

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"))
  )
Chris Murphy
  • 6,411
  • 1
  • 24
  • 42