如何使用Lein-Ring uberwar包含属性

时间:2013-02-05 22:04:29

标签: clojure leiningen ring

我们正尝试使用lein ring uberwar命令在JBoss上对Leiningen项目进行测试部署。在我尝试阅读属性文件之前,我取得了巨大的成功。

我有一个将属性文件读入地图的函数:

(defn load-properties
  [file-name]
  (with-open [^java.io.Reader reader (io/reader file-name)]
    (let [properties (java.util.Properties.)]
      (.load properties reader)
      (into {} (for [[k v] properties] [(keyword k) (read-string v)])))))

我这样使用它:

(def settings (load-properties "application.properties"))

“application.properties”是项目目录中文件的名称。使用lein run一切都很好,但是当我创建独立war并将它转换为JBoss时,这就是堆栈跟踪:

16:51:08,140 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/kente].[kente.core/application servlet]] (http-/127.0.0.1:8080-1) Allocate exception for ser
vlet kente.core/application servlet: java.io.FileNotFoundException: application.properties (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method) [rt.jar:1.6.0_37]
        at java.io.FileInputStream.<init>(Unknown Source) [rt.jar:1.6.0_37]
        at clojure.java.io$fn__8326.invoke(io.clj:233) [clojure-1.4.0.jar:]
        at clojure.java.io$fn__8265$G__8230__8272.invoke(io.clj:73) [clojure-1.4.0.jar:]
        at clojure.java.io$fn__8338.invoke(io.clj:262) [clojure-1.4.0.jar:]
        at clojure.java.io$fn__8265$G__8230__8272.invoke(io.clj:73) [clojure-1.4.0.jar:]
        at clojure.java.io$fn__8300.invoke(io.clj:169) [clojure-1.4.0.jar:]
        at clojure.java.io$fn__8239$G__8234__8246.invoke(io.clj:73) [clojure-1.4.0.jar:]
        at clojure.java.io$reader.doInvoke(io.clj:106) [clojure-1.4.0.jar:]
        at clojure.lang.RestFn.invoke(RestFn.java:410) [clojure-1.4.0.jar:]
        at kente.utils$load_properties.invoke(utils.clj:7) [classes:]
        at clojure.lang.AFn.applyToHelper(AFn.java:161) [clojure-1.4.0.jar:]
        at clojure.lang.AFn.applyTo(AFn.java:151) [clojure-1.4.0.jar:]
        at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:3382) [clojure-1.4.0.jar:]
        at clojure.lang.Compiler$DefExpr.eval(Compiler.java:398) [clojure-1.4.0.jar:]
        at clojure.lang.Compiler.eval(Compiler.java:6516) [clojure-1.4.0.jar:]
        at clojure.lang.Compiler.load(Compiler.java:6952) [clojure-1.4.0.jar:]
        at clojure.lang.RT.loadResourceScript(RT.java:359) [clojure-1.4.0.jar:]
        at clojure.lang.RT.loadResourceScript(RT.java:350) [clojure-1.4.0.jar:]
        at clojure.lang.RT.load(RT.java:429) [clojure-1.4.0.jar:]
        at clojure.lang.RT.load(RT.java:400) [clojure-1.4.0.jar:]
        at clojure.core$load$fn__4890.invoke(core.clj:5415) [clojure-1.4.0.jar:]
        at clojure.core$load.doInvoke(core.clj:5414) [clojure-1.4.0.jar:]
        at clojure.lang.RestFn.invoke(RestFn.java:408) [clojure-1.4.0.jar:]
        at clojure.core$load_one.invoke(core.clj:5227) [clojure-1.4.0.jar:]
        at clojure.core$load_lib.doInvoke(core.clj:5264) [clojure-1.4.0.jar:]
        at clojure.lang.RestFn.applyTo(RestFn.java:142) [clojure-1.4.0.jar:]
        at clojure.core$apply.invoke(core.clj:603) [clojure-1.4.0.jar:]
        at clojure.core$load_libs.doInvoke(core.clj:5298) [clojure-1.4.0.jar:]
        at clojure.lang.RestFn.applyTo(RestFn.java:137) [clojure-1.4.0.jar:]
        at clojure.core$apply.invoke(core.clj:605) [clojure-1.4.0.jar:]
        at clojure.core$use.doInvoke(core.clj:5392) [clojure-1.4.0.jar:]
        at clojure.lang.RestFn.invoke(RestFn.java:421) [clojure-1.4.0.jar:]
        at kente.core$eval9$loading__4784__auto____10.invoke(core.clj:1)
        at kente.core$eval9.invoke(core.clj:1)
        at clojure.lang.Compiler.eval(Compiler.java:6511) [clojure-1.4.0.jar:]
        at clojure.lang.Compiler.eval(Compiler.java:6501) [clojure-1.4.0.jar:]
        at clojure.lang.Compiler.load(Compiler.java:6952) [clojure-1.4.0.jar:]
        at clojure.lang.RT.loadResourceScript(RT.java:359) [clojure-1.4.0.jar:]
        at clojure.lang.RT.loadResourceScript(RT.java:350) [clojure-1.4.0.jar:]
        at clojure.lang.RT.load(RT.java:429) [clojure-1.4.0.jar:]
        at clojure.lang.RT.load(RT.java:400) [clojure-1.4.0.jar:]
        at clojure.core$load$fn__4890.invoke(core.clj:5415) [clojure-1.4.0.jar:]
        at clojure.core$load.doInvoke(core.clj:5414) [clojure-1.4.0.jar:]
        at clojure.lang.RestFn.invoke(RestFn.java:408) [clojure-1.4.0.jar:]
        at clojure.core$load_one.invoke(core.clj:5227) [clojure-1.4.0.jar:]
        at clojure.core$load_lib.doInvoke(core.clj:5264) [clojure-1.4.0.jar:]
        at clojure.lang.RestFn.applyTo(RestFn.java:142) [clojure-1.4.0.jar:]
        at clojure.core$apply.invoke(core.clj:603) [clojure-1.4.0.jar:]
        at clojure.core$load_libs.doInvoke(core.clj:5298) [clojure-1.4.0.jar:]
        at clojure.lang.RestFn.applyTo(RestFn.java:137) [clojure-1.4.0.jar:]
        at clojure.core$apply.invoke(core.clj:603) [clojure-1.4.0.jar:]
        at clojure.core$require.doInvoke(core.clj:5381) [clojure-1.4.0.jar:]
        at clojure.lang.RestFn.invoke(RestFn.java:421) [clojure-1.4.0.jar:]
        at kente.servlet$eval3$loading__4784__auto____4.invoke(servlet.clj:1)
        at kente.servlet$eval3.invoke(servlet.clj:1)
        at clojure.lang.Compiler.eval(Compiler.java:6511) [clojure-1.4.0.jar:]
        at clojure.lang.Compiler.eval(Compiler.java:6501) [clojure-1.4.0.jar:]
        at clojure.lang.Compiler.eval(Compiler.java:6500) [clojure-1.4.0.jar:]
        at clojure.lang.Compiler.load(Compiler.java:6952) [clojure-1.4.0.jar:]
        at clojure.lang.RT.loadResourceScript(RT.java:359) [clojure-1.4.0.jar:]
        at clojure.lang.RT.loadResourceScript(RT.java:350) [clojure-1.4.0.jar:]
        at clojure.lang.RT.load(RT.java:429) [clojure-1.4.0.jar:]
        at clojure.lang.RT.load(RT.java:400) [clojure-1.4.0.jar:]
        at clojure.core$load$fn__4890.invoke(core.clj:5415) [clojure-1.4.0.jar:]
        at clojure.core$load.doInvoke(core.clj:5414) [clojure-1.4.0.jar:]
        at clojure.lang.RestFn.invoke(RestFn.java:408) [clojure-1.4.0.jar:]
        at clojure.lang.Var.invoke(Var.java:415) [clojure-1.4.0.jar:]
        at kente.servlet.<clinit>(Unknown Source) [classes:]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.6.0_37]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) [rt.jar:1.6.0_37]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) [rt.jar:1.6.0_37]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) [rt.jar:1.6.0_37]
        at org.jboss.msc.value.ConstructedValue.getValue(ConstructedValue.java:61)
        at org.jboss.as.naming.ValueManagedReferenceFactory.getReference(ValueManagedReferenceFactory.java:49)
        at org.jboss.as.ee.component.ManagedReferenceInterceptorFactory$ManagedReferenceInterceptor.processInvocation(ManagedReferenceInterceptorFactory.java:90) [jboss-as-ee-7.1.3
.Final-redhat-4.jar:7.1.3.Final-redhat-4]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final-redhat-2.jar:1.1.1.Final-redhat-2]
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.1.Final-redhat-2.jar:1.1.1.Final-redhat-2]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final-redhat-2.jar:1.1.1.Final-redhat-2]
        at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45) [jboss-as-ee-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final-redhat-2.jar:1.1.1.Final-redhat-2]
        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.1.Final-redhat-2.jar:1.1.1.Final-redhat-2]
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:161) [jboss-as-ee-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
        at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:85) [jboss-as-ee-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
        at org.jboss.as.web.deployment.component.WebComponentInstantiator$1.<init>(WebComponentInstantiator.java:57) [jboss-as-web-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
        at org.jboss.as.web.deployment.component.WebComponentInstantiator.getReference(WebComponentInstantiator.java:55) [jboss-as-web-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4
]
        at org.jboss.as.web.deployment.WebInjectionContainer.instantiate(WebInjectionContainer.java:102) [jboss-as-web-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
        at org.jboss.as.web.deployment.WebInjectionContainer.newInstance(WebInjectionContainer.java:81) [jboss-as-web-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
        at org.jboss.as.web.deployment.WebInjectionContainer.newInstance(WebInjectionContainer.java:75) [jboss-as-web-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1156) [jbossweb-7.0.17.Final-redhat-1.jar:]
        at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:952) [jbossweb-7.0.17.Final-redhat-1.jar:]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:188) [jbossweb-7.0.17.Final-redhat-1.jar:]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.17.Final-redhat-1.jar:]
        at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.17.Final-redhat-1.jar:]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.17.Final-redhat-1.jar:]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.17.Final-redhat-1.jar:]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:372) [jbossweb-7.0.17.Final-redhat-1.jar:]
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.17.Final-redhat-1.jar:]
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:679) [jbossweb-7.0.17.Final-redhat-1.jar:]
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931) [jbossweb-7.0.17.Final-redhat-1.jar:]

我注意到你可以设置一个名为:war-resources-path的defproject键,所以我将我的属性文件放入$ PROJECT_ROOT / war-resources目录(the default),我还没有好运。

是否有人知道使用lein ring uberwar从属性文件中读取的最佳方式?

1 个答案:

答案 0 :(得分:1)

jar / war中的条目不是文件。您应该使用clojure.java.io/resource而不是clojure.java.io/file或裸路径字符串来引用属性(以及战争中的任何其他类型的资源)。这将适用于文件系统上的本地资源以及战争中的资源。

您可以将资源放在项目根目录中的资源目录中,或者放在由资源路径指定的其他目录中,然后它们在类路径中可用,并且当您使用lein环构建一个时也将包含在战争中