【问题标题】:Jetty Maven can not find dev.properties from parent projectJetty Maven 无法从父项目中找到 dev.properties
【发布时间】:2014-08-23 13:27:36
【问题描述】:

我们有一个父项目,下面有多个子项目。他们中的一些人正在制造一场战争,而另一些人正在制造一个罐子。

在父项目中,有一个 dev.properties 文件。它在目录中

/src/main/resources/config/environments/dev.properties

这个文件被多个子项目正确使用,没有问题。

导致错误的项目在pom.xml中对jetty有如下配置

<profiles>
    <profile>
        <id>jetty</id>
        <build>
            <finalName>${project.artifactId}</finalName>
            <plugins>
                <plugin>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>maven-jetty-plugin</artifactId>
                    <version>6.1.16</version>
                    <configuration>
                        <scanIntervalSeconds>10</scanIntervalSeconds>
                        <stopPort>8005</stopPort>
                        <stopKey>STOP</stopKey>
                        <contextPath>/</contextPath>
                        <useTestClasspath>true</useTestClasspath>
                    </configuration>
                    <executions>
                        <execution>
                            <id>start-jetty</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <scanIntervalSeconds>0</scanIntervalSeconds>
                                <daemon>true</daemon>
                            </configuration>
                        </execution>
                        <execution>
                            <id>stop-jetty</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>stop</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

</profiles>

这个项目引用我们的管理项目,如下所示:

<dependency>
            <groupId>sample.project</groupId>
            <artifactId>sample-project-admin-service</artifactId>
            <version>0.1.0-SNAPSHOT</version>
        </dependency>

在示例项目和文件 sample-project-admin-service-context.xml 中有这样的配置:

<bean id="cacheMan"
        class="org.infinispan.spring.provider.SpringRemoteCacheManagerFactoryBean"
        p:configurationPropertiesFileLocation="/WEB-INF/classes/config/environments/${SERVER_ENV}.properties" />

这应该读取属性文件并初始化 cacheMan。 sample-project-admin-service-context.xml 位于路径中:

/sample-project-admin-service/src/main/resources/META-INF/spring/flash-air-admin-service-context.xml

当我们制作一个war文件并将其部署到tomcat时。应用程序正确部署,没有任何问题。但是当我尝试用码头加载它时,我收到以下错误:

设置 bean 时无法解析对 bean 'cacheManager' 的引用 属性“缓存管理器”;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建在类路径资源中定义的名称为“cacheManager”的bean [META-INF/spring/sample-project-admin-service-context.xml]:调用 初始化方法失败;嵌套异常是 java.io.FileNotFoundException: 无法打开 ServletContext 资源 [/WEB-INF/classes/config/environments/dev.properties]:

Jetty maven 使用命令运行:

mvn install -Pjetty

任何帮助将不胜感激。

提前致谢

【问题讨论】:

    标签: java spring maven spring-mvc maven-jetty-plugin


    【解决方案1】:

    在部署时,所有适合 src/main/resources 的文件都打包在类路径中。然后,当 Ioc 容器尝试实例化 cacheMan bean 时,它无法找到它。问题是您无法在定义时恢复父级中的文件,因为它有一个不透明的路径Get reosurce in a modular project,唯一要做的就是:

    • 在子模块项目中从父项目中移动文件

    并以这种方式更改 bean 定义。

    如果路径是

    /src/main/resources/config/environments/dev.properties

    您的 bean 定义将是:

    <bean id="cacheMan"
        class="org.infinispan.spring.provider.SpringRemoteCacheManagerFactoryBean"
        p:configurationPropertiesFileLocation="/config/environments/dev.properties" />
    

    通过这种方式 Ioc 容器能够定位文件

    【讨论】:

    • 您好,感谢您的回复。问题是文件 dev.properties 被多个项目使用,我认为在每个子项目中保留文件副本不是一个好习惯。有没有办法在编译时用 maven 移动文件?
    • @giannisapi 尝试仅更改路径 p:configurationPropertiesFileLocation 将文件留在父项目中,它应该可以工作
    • 嗨,Skizzo,我已经使用了 p:configurationPropertiesFileLocation,你能解释一下你的意思吗?
    • @giannisapi 你能否将值从 /WEB-INF/classes/config/environments/${SERVER_ENV}.properties 更改为 p:configurationPropertiesFileLocation 到 /config/environments/ dev.properties 并尝试安装您的应用程序,因为所有资源文件都将打包在类路径中
    • 再次感谢,但我已经尝试过了,但它似乎不起作用,我仍然收到 sam 错误
    【解决方案2】:

    好的,我似乎已经解决了这个问题。我遵循的步骤是:

    这是码头插件的最终配置文件:

    <profile>
                <id>jetty</id>
                <build>
                    <finalName>${project.artifactId}</finalName>
                    <plugins>
                        <plugin>
                            <groupId>org.eclipse.jetty</groupId>
                            <artifactId>jetty-maven-plugin</artifactId>
                            <version>9.2.1.v20140609</version>
                            <configuration>
                                <scanIntervalSeconds>10</scanIntervalSeconds>
                                <stopPort>8005</stopPort>
                                <stopKey>STOP</stopKey>
                                <contextPath>/</contextPath>
                                <useTestClasspath>true</useTestClasspath>
                            </configuration>
                            <executions>
                                <execution>
                                    <id>start-jetty</id>
                                    <phase>pre-integration-test</phase>
                                    <goals>
                                        <goal>run</goal>
                                    </goals>
                                    <configuration>
                                        <scanIntervalSeconds>0</scanIntervalSeconds>
                                        <daemon>true</daemon>
                                    </configuration>
                                </execution>
                                <execution>
                                    <id>stop-jetty</id>
                                    <phase>post-integration-test</phase>
                                    <goals>
                                        <goal>stop</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
    

    如您所见,我已将版本更改为 9.2.1.v20140609。这似乎解决了问题。

    谢谢

    【讨论】:

      猜你喜欢
      • 2011-06-12
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多