从包含WAR文件的EAR文件中读取属性



我的属性文件有一个小问题。我用maven生成了一个dependency-tree.txt文件,并将其存储在我的EAR文件中

EAR
  /META-INF
     dependency-tree.txt
  /lib
     some libs
  restservice.war

现在我想用REST服务显示dependency-tree.txt文件。我在restservice.war.中开发了REST端点

我可以访问dependency-tree.txt文件吗?该文件存储在war文件之外,但存储在ear文件中?

这个REST端点的原因是,我们希望为测试团队提供一个接口。使用这种方法,我们可以在没有任何手动步骤的情况下描述已部署的工件。

或者有人能为我找到更好的解决方案?

感谢

这个想法是在验证时运行插件maven依赖插件plugin:tree阶段(maven生命周期中的1个阶段),并将文件(tree.txt)复制到在战争一代人之后/ear,文件必须位于正确的位置才能部署REST服务。

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <!-- because we will use the plugin org.apache.maven.plugins at validate 
                    phase, for Eclipse is happy , we'll ignore here -->
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-dependency-plugin</artifactId>
                                    <versionRange>[2.10,)</versionRange>
                                    <goals>
                                        <goal>tree</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution> <!-- -->
                    <id>generateTree</id>
                    <phase>validate</phase> <!-- You can change it , if you put 'test' , you can delete <pluginManagement> -->
                    <goals>
                        <goal>tree</goal>
                    </goals>
                    <configuration>
                        <outputFile>src/main/resources/tree_rest_access.txt</outputFile>
                        <!-- change location, exemple : you can use => restservice/src/main/webapp/tree/rest 
                            , and expose from controller "tree/rest/tree_rest_access.txt" -->
                    </configuration>
                </execution>
            </executions>
        </plugin>

为什么要将其存储在EAR!?EAR通常只应包含WAR,如果您的服务器需要,则应包含另一个部署文件(例如jboss-deployment-structure.xml)。EAR是将几个模块打包在一起的包装器,而不是其他模块。

若您有RESTEndpoint,并且希望它从项目中返回资源,则可以通过将资源放入项目中来实现这一点。通过在加载资源时使用相对路径,可以很容易地在项目根目录中创建文件并从中读取。更好的解决方案是明确定义存储该文件的外部位置,使该位置在一些应用程序config.properties文件中可配置。在这种情况下,所有开发人员只需要在各自的配置中设置此路径。

最新更新