gpt4 book ai didi

jpa - 在Play JPA项目中使用sbt-native-packager将自定义文件夹添加到Docker

转载 作者:行者123 更新时间:2023-12-02 19:10:09 36 4
gpt4 key购买 nike

我正在开发一个JPA + Playframework 2.5.x项目,并将其打包为Docker镜像。

我想将conf文件夹下的一个文件(META-INF / persistence.xml)以外的所有文件强行添加到docker输出中

目前,我已经执行了以下操作:

mappings in Docker += file("conf/base/application.conf") -> "opt/docker/conf/base/application.conf"
mappings in Docker += file("conf/base/default-client.conf") -> "opt/docker/conf/base/default-client.conf"
mappings in Docker += file("conf/prod/application.conf") -> "opt/docker/conf/prod/application.conf"
mappings in Docker += file("conf/prod/logback.xml") -> "opt/docker/conf/prod/logback.xml"
mappings in Docker += file("conf/stage/application.conf") -> "opt/docker/conf/stage/application.conf"
mappings in Docker += file("conf/stage/logback.xml") -> "opt/docker/conf/stage/logback.xml"
mappings in Docker += file("conf/local/application.conf") -> "opt/docker/conf/local/application.conf"
mappings in Docker += file("conf/local/logback.xml") -> "opt/docker/conf/local/logback.xml"
mappings in Docker += file("conf/routes") -> "opt/docker/conf/routes"
mappings in Docker += file("conf/ValidationMessages.properties") -> "opt/docker/conf/ValidationMessages.properties"

我确信这不是实现此目标的最佳方法。有人可以提出更好的选择来按我需要的方式自定义Docker输出吗?

我的build.sbt文件:
PlayKeys.externalizeResources := false
name := """wp-pw-ng"""

version := "1.0.0-SNAPSHOT"

lazy val `wp-pw-ng` = (project in file(".")).enablePlugins(PlayJava, JavaAppPackaging)

val playVer = "2.5.9"

scalaVersion := "2.11.8"

libraryDependencies ++= Seq(
javaJpa,
"org.hibernate" % "hibernate-core" % "5.2.3.Final" exclude("dom4j", "dom4j"),
"mysql" % "mysql-connector-java" % "6.0.5",
cache,
javaWs,
filters,
"dom4j" % "dom4j" % "1.6",
"com.google.guava" % "guava" % "20.0-rc1",
"com.amazonaws" % "aws-java-sdk" % "1.11.34"
)

val docDeps = Seq(
"io.swagger" %% "swagger-play2" % "1.5.3.2"
)

libraryDependencies ++= docDeps

fork in Test := true
javaOptions in Test ++= Seq(
"-Xms512M",
"-Xmx1536M",
"-Xss1M",
"-XX:MaxPermSize=384M"
)

fork in run := false

unmanagedResourceDirectories in Compile <+= (sourceDirectory in Compile) (_ / "resources")

mappings in Docker += file("conf/base/application.conf") -> "opt/docker/conf/base/application.conf"
mappings in Docker += file("conf/base/default-client.conf") -> "opt/docker/conf/base/default-client.conf"
mappings in Docker += file("conf/base/pw.conf") -> "opt/docker/conf/base/paywall.conf"
mappings in Docker += file("conf/prod/application.conf") -> "opt/docker/conf/prod/application.conf"
mappings in Docker += file("conf/prod/logback.xml") -> "opt/docker/conf/prod/logback.xml"
mappings in Docker += file("conf/stage/application.conf") -> "opt/docker/conf/stage/application.conf"
mappings in Docker += file("conf/stage/logback.xml") -> "opt/docker/conf/stage/logback.xml"
mappings in Docker += file("conf/local/application.conf") -> "opt/docker/conf/local/application.conf"
mappings in Docker += file("conf/local/logback.xml") -> "opt/docker/conf/local/logback.xml"
mappings in Docker += file("conf/routes") -> "opt/docker/conf/routes"
mappings in Docker += file("conf/ValidationMessages.properties") -> "opt/docker/conf/ValidationMessages.properties"

//************************************************
// Custom Docker Build,
// use command 'activator docker:publishLocal'
// to publish image locally to computer.
//************************************************
import com.typesafe.sbt.packager.docker._

val playUser = "play"
val grp = "idud"
dockerRepository := Some("quay.io/we")

version in Docker := "latest"

val buildEnv: String = System.getProperty("build.env")

dockerCommands := Seq(
Cmd("FROM", "anapsix/alpine-java:8_jdk_unlimited"),
Cmd("RUN", "apk", "-Uuv add", "--no-cache", "su-exec", "groff", "less", "python", "py-pip", "&& pip install awscli ", "&& apk --purge -v del py-pip ", " && rm /var/cache/apk/* "),
Cmd("RUN", s"addgroup $grp"),
Cmd("RUN", s"adduser -s /bin/bash -D -G $grp $playUser"),
Cmd("RUN", "echo", s"'$playUser ALL=(ALL) NOPASSWD:ALL'", ">> /etc/sudoers"),
Cmd("WORKDIR", "/opt/docker"),
Cmd("ADD", "opt /opt"),
Cmd("RUN", "chown", "-R", s"$playUser:$grp", "."),
Cmd("USER", s"$playUser"),
Cmd("ENTRYPOINT", "[\"bin/wp-pw-ng\", \"-Dconfig.file=conf/" + buildEnv + "/application.conf\", \"-Dhttp.port=7000\" , \"-Dpidfile.path=/dev/null\" ,\"-Dlogger.file=conf/" + buildEnv + "/logback.xml\"]"),
Cmd("EXPOSE", "9877")
)

附加信息
由于Playframework, Issue 4590的构建 stage模式问题,我不得不求助于这种解决方法,并且我不得不在 PlayKeys.externalizeResources := false文件中使用 build.sbt标志。这将从Docker中的 conf文件夹中删除所有文件。

最佳答案

playExternalizeResources:= false设置just prevents sbtfrom addingplayExternalizedResources到您的mappings in Universal,这是可用于所有目标程序包格式(包括docker)的映射。

您可以使用MappingsHelper(ScalaDocs)减轻构建sbt的负担。

import NativePackagerHelper._
mappings in Universal ++= contentOf("conf")

您还可以过滤映射以删除不需要的文件,例如
import NativePackagerHelper._
mappings in Universal ++= contentOf("conf").filter(_.2.contains("persistence.xml")

希望能有所帮助,
木木

关于jpa - 在Play JPA项目中使用sbt-native-packager将自定义文件夹添加到Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40849457/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com