我有两个使用Camel's Main
的独立模块。它们都包含:
main.setPropertyPlaceholderLocations( "classpath:application.properties" );
我也尝试过:
main.setPropertyPlaceholderLocations( "application.properties" );
如果我从Eclipse中运行它们,两者都可以正常工作(已将<project>/target
添加到 Run Configurations' Classpath )。
如果我从cmd行运行它们:
...\target> java -jar <module>.jar
如果target
同时包含application.properties
和<module>.jar
,则可以正常工作。另一个结果是:
Exception in thread "main" org.apache.camel.RuntimeCamelException: java.io.FileNotFoundException:
Properties file application.properties not found in classpath
我见过Q: Camel properties file not found in class path,但是我的application.properties
是在src/main/resources
中,并在target
期间复制到了mvn package
。 / p>
为澄清。在项目的POM中,我使用:
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
</includes>
<targetPath>..</targetPath> <!-- relative to target/classes -->
</resource>
<resources>
防止application.properties
驻留在<module>.jar
内部。
90 [main] INFO <module> - Starting Camel...
182 [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.22.0 (CamelContext: camel-1) is shutting down
195 [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.22.0 (CamelContext: camel-1) uptime
198 [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.22.0 (CamelContext: camel-1) is shutdown in 0.017 seconds
Exception in thread "main" org.apache.camel.RuntimeCamelException: java.io.FileNotFoundException: Properties file application.properties not found in classpath
at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1830)
at org.apache.camel.model.RouteDefinitionHelper.initRouteInputs(RouteDefinitionHelper.java:382)
at org.apache.camel.model.RouteDefinitionHelper.prepareRouteImp(RouteDefinitionHelper.java:298)
at org.apache.camel.model.RouteDefinitionHelper.prepareRoute(RouteDefinitionHelper.java:270)
at org.apache.camel.model.RoutesDefinition.route(RoutesDefinition.java:205)
at org.apache.camel.model.RoutesDefinition.from(RoutesDefinition.java:158)
at org.apache.camel.builder.RouteBuilder.from(RouteBuilder.java:169)
at <module>Route.configure(<module>Route.java:24)
at org.apache.camel.builder.RouteBuilder.checkInitialized(RouteBuilder.java:462)
at org.apache.camel.builder.RouteBuilder.configureRoutes(RouteBuilder.java:402)
at org.apache.camel.builder.RouteBuilder.addRoutesToCamelContext(RouteBuilder.java:383)
at org.apache.camel.impl.DefaultCamelContext$1.call(DefaultCamelContext.java:1029)
at org.apache.camel.impl.DefaultCamelContext$1.call(DefaultCamelContext.java:1026)
at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3272)
at org.apache.camel.impl.DefaultCamelContext.addRoutes(DefaultCamelContext.java:1026)
at org.apache.camel.main.MainSupport.postProcessCamelContext(MainSupport.java:612)
at org.apache.camel.main.MainSupport.postProcessContext(MainSupport.java:550)
at org.apache.camel.main.Main.doStart(Main.java:136)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.main.MainSupport.run(MainSupport.java:170)
at <module>.run(<module>.java:44)
at <module>.main(<module>.java:20)
Caused by: java.io.FileNotFoundException: Properties file application.properties not found in classpath
at org.apache.camel.component.properties.DefaultPropertiesResolver.loadPropertiesFromClasspath(DefaultPropertiesResolver.java:112)
at org.apache.camel.component.properties.DefaultPropertiesResolver.resolveProperties(DefaultPropertiesResolver.java:69)
at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:207)
at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:178)
at org.apache.camel.impl.DefaultCamelContext.resolvePropertyPlaceholders(DefaultCamelContext.java:2552)
at org.apache.camel.model.ProcessorDefinitionHelper.resolvePropertyPlaceholders(ProcessorDefinitionHelper.java:735)
at org.apache.camel.model.RouteDefinitionHelper.initRouteInputs(RouteDefinitionHelper.java:380)
... 20 more
204 [Camel Thread #0 - CamelHangupInterceptor] INFO org.apache.camel.main.MainSupport$HangupInterceptor - Received hang up - stopping the main instance.
答案 0 :(得分:1)
现在我知道为什么一个模块运行而另一个模块却没有运行。显然,骆驼在setPropertyPlaceholderLocations()
之后执行了延迟加载,并且由于我还没有使用application.properties
的任何属性(至今),它甚至都没有尝试读取文件。
现在,我正在使用它们,以前的工作模块也会失败。 (错误导致真相的罕见情况之一;;)
解决方案是使用:
String jarPath = new File(
this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath() )
.getParent();
main.setPropertyPlaceholderLocations("file:" + jarPath + "/application.properties" )
几个小时前克劳斯(Claus)的评论,而不是:
main.setPropertyPlaceholderLocations("classpath:...")