brucke
brucke

Reputation: 76

Read property in EAR file from containing WAR file

I have a small problem with my property files. I generate a dependency-tree.txt file with maven and store it in my EAR file

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

Now I want to display the dependency-tree.txt file with an REST Service. I developed the REST Endpoint in the restservice.war.

Can I access the dependency-tree.txt file which is stored outside the war file, but within the ear file?

The reason for this REST endpoint is, we want to provide an interface for the test-team. With this approach, we can describe the deployed artifact without any manual steps.

Or have somebody a better solution for me?

Thanks

Upvotes: 0

Views: 152

Answers (2)

Vanja Lee
Vanja Lee

Reputation: 263

Why would you store it in EAR!? EAR should usually contain only WAR and if your server requires it, another deployment file (like jboss-deployment-structure.xml for instance). EAR is wrapper to package several modules together, nothing else.

If you have REST Endpoint and you want it to give back resources from project, you can achieve this by putting the resources into the project. Creating the file in the project root and reading it from there is easily configurable by using relative path when loading the resource. Better solution is to clearly define an external location for storing this file, to make this location configurable in some application config.properties file. In this case, all developers will just need to set this path in their individual configurations.

Upvotes: 0

question_maven_com
question_maven_com

Reputation: 2473

The idea is to run the plugin maven-dependency-plugin:tree at validate phase (1 phase in maven Lifecycle ) , and copy the file (tree.txt) to the right place for your WAR that after the generation of the war / ear, the file must be in the right place to be deploy a REST service.

<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>

Upvotes: 1

Related Questions