SBT: Exclude class from Jar

后端 未结 2 1100
感动是毒
感动是毒 2020-12-14 10:21

I am converting a legacy jar project to SBT and for strange reasons that are not easily solved, this project comes with \"javax/servlet/Servlet.class\" insi

2条回答
  •  醉梦人生
    2020-12-14 10:51

    I came up with this solution, it defines a new compile task which depends on the previous compile task (thus effectively allowing me to hook in right after the source is compiled and before it's packaged)

    def mySettings = {
      // add functionality to the standard compile task 
      inConfig(Compile)(Seq(compile in Compile <<= (target,streams,compile in Compile) map{
        (targetDirectory, taskStream, analysis) =>
          import taskStream.log
          // this runs after compile but before package-bin
          recursiveListFiles(targetDirectory, ".*javax.*".r) foreach { 
            file =>
              log.warn("deleting matched resource: " + file.getAbsolutePath())
              IO.delete(file)
          }
          analysis
        })) ++
        Seq(name := "MyProject", version := "1.0", exportJars := true)
    }
    
    def recursiveListFiles(f: File, r: Regex): Array[File] = {
      val these = f.listFiles
      val good = these.filter(f => r.findFirstIn(f.getName).isDefined)
      good ++ these.filter(_.isDirectory).flatMap(recursiveListFiles(_, r))
    }
    

    Its a little bit more complicated than what I had hoped but it allows me to do all sorts of modifications prior to packaging (in this case searching the target folder deleting all class files that matches a regular expression). Also it accomplished my second goal of sticking with the default SBT lifecycle.

提交回复
热议问题