Joginder Pawan Kumar
Joginder Pawan Kumar

Reputation: 67

Unable to read pom.xml inside META-INF folder of Spring war deployed on Tomcat

I am trying to read pom.xml file as I need get list of dependencies.

When I run SpringBoot application using STS or Eclipse, I am able to read file using below line as POM file is present inside project folder.

Model model = mavenXpp3Reader.read(new FileReader("pom.xml"));

When I run SpringBoot application as jar e.g., java -jar -Dspring.profiles.active=local my-app-1.0.0.war, I am able to read file using below line code snippet

Model model = mavenXpp3Reader.read(new InputStreamReader(Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("META-INF/maven/{groupId}/{artifactId}/pom.xml")));

and also using below snippet

Model model = mavenXpp3Reader.read(new InputStreamReader(
                    this.getClass().getClassLoader().getResourceAsStream("META-INF/maven/{groupId}/{artifactId}/pom.xml")));

However, if I deploy this application WAR on Tomcat, none of these are working.

I tried with using \ before path and using / also in path. But none of these are working.

I also tried with /webapps/xyz#services/META-INF/maven/{groupId}/{artifactId}/pom.xmlas I found this is the full path of pom.xml on Tomcat

I think the approaches I used will read only inside classpath e.g,. classes not inside webapps folder.

Right now I have no clue what is wrong with this code. Any clue or help is highly appreciated. Thanks.

Upvotes: 0

Views: 80

Answers (1)

Corneil du Plessis
Corneil du Plessis

Reputation: 1043

Try using the Spring Framework FileSystemResource to use a file given it's path:

Resource resource = new FileSystemResource("/extact-path-to-file");
Model model = mavenXpp3Reader.read(resource.getInputStream());

Upvotes: 0

Related Questions