繁体   English   中英

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

[英]Read property in EAR file from containing WAR file

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

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

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

我可以访问存储在war文件外部但在ear文件内部的dependency-tree.txt文件吗?

使用此REST端点的原因是,我们希望为测试团队提供一个接口。 通过这种方法,我们无需任何手动步骤即可描述已部署的工件。

还是有人为我提供更好的解决方案?

谢谢

这个想法是在验证阶段(maven Lifecycle中的1阶段)运行插件maven-dependency-plugin:tree,并将文件(tree.txt)复制到WAR生成后的WAR的正确位置,文件必须在正确的位置才能部署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是将几个模块打包在一起的包装器,仅此而已。

如果您拥有REST Endpoint,并且希望它从项目中退还资源,则可以通过将资源放入项目中来实现。 在加载资源时,可以使用相对路径轻松地在项目根目录中创建文件并从中读取文件。 更好的解决方案是明确定义用于存储此文件的外部位置,以使此位置在某些应用程序config.properties文件中可配置。 在这种情况下,所有开发人员只需在各自的配置中设置此路径即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM