【问题标题】:Customise Freedesktop file with sbt-native-packager and JDKPackager plugin使用 sbt-native-packager 和 JDKPackager 插件自定义 Freedesktop 文件
【发布时间】:2015-10-17 23:50:48
【问题描述】:

我想自定义由 javapackager 创建的 .desktop 文件,作为 sbt-native-packager 的 JDKPackager 插件的一部分。它显然使用了一个模板:

[info]   Using default package resource [Menu shortcut descriptor]
         (add package/linux/Foo.desktop to the class path to customize)

特别是,我想添加StartupWMClass 条目,Gnome 将使用它来统一我的应用程序打开的所有窗口。

【问题讨论】:

    标签: scala sbt-native-packager


    【解决方案1】:

    javapackager 指的是插件的目标目录,即target/jdkpackager。例如,这是在编写 javafx-ant 构建文件时创建的。所以我们可以在这里捎带:

    // rewrite the task so that after the ant build is created,
    // we add package/linux/MyApp.desktop
    writeAntBuild in JDKPackager := {
      val res  = (writeAntBuild in JDKPackager).value
      val main = (mainClass     in JDKPackager).value
        .getOrElse(sys.error("No main class specified"))
      val tgt  = (target        in JDKPackager).value
      val n    = (name          in JDKPackager).value
      val wm   = main.replace('.', '-')
      val desktop = 
        s"""[Desktop Entry]
           |Name=APPLICATION_NAME
           |Comment=APPLICATION_SUMMARY
           |Exec=/opt/APPLICATION_FS_NAME/APPLICATION_LAUNCHER_FILENAME
           |Icon=/opt/APPLICATION_FS_NAME/APPLICATION_LAUNCHER_FILENAME.png
           |Terminal=false
           |Type=Application
           |Categories=DEPLOY_BUNDLE_CATEGORY
           |DESKTOP_MIMES
           |StartupWMClass=$wm
           |""".stripMargin
      IO.write(tgt / "package" / "linux" / s"$n.desktop", desktop)
      res
    }
    

    【讨论】:

    • 这个工作的原因(从 Oracle javapackager 文档中并不完全明显)是 build.xml 文件将 tgt 目录放在 Ant 任务类路径中,然后查找覆盖其类路径的package/linux 分支中的资源。
    【解决方案2】:

    只要Ant task XML中有对应的设置,还有另一种解决方案:只需重写插件生成的XML,通过antBuildDefn即可。

    这是一个通过添加category attribute.desktop 文件指定菜单类别的示例:

    antBuildDefn in JDKPackager := {
      val origTask = (antBuildDefn in JDKPackager).value
    
      val InfoLabel = "info"
      val KeyRegex  = s"$InfoLabel\\.(.+)".r
    
      import scala.xml._
      import scala.xml.transform._
      val infoRewrite = new RewriteRule {
        override def transform(n: Node) = n match {
          case e: Elem if e.prefix == "fx" && e.label == InfoLabel =>
            e % Attribute("", "category", "Office", Null)
          case other => other
        }
      }
    
      new RuleTransformer(infoRewrite)(origTask)
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 2015-10-17
      • 2017-03-25
      • 2016-10-12
      • 2017-04-12
      • 2014-12-05
      • 2016-12-14
      • 2015-09-01
      • 2014-07-06
      相关资源
      最近更新 更多