0

pom.xml snippet:

<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>xyzservice</artifactId>
<version>2.2.2-SNAPSHOT</version>
<packaging>war</packaging>

result file: xyzservice-2.2.2-SNAPSHOT.war

I copied the war file to webapps directory, renamed it to: xyzservice.war. Then after extraction by Tomcat, this code:

AImpl.class.getResource("/abc/d.pdf").getFile();

gave me: /usr/local/tomcat-8.0.36-8082/webapps/xyzservice-2.2.2-SNAPSHOT/WEB-INF/classes/abc/d.pdf but I am expecting /usr/local/tomcat-8.0.36-8082/webapps/xyzservice/WEB-INF/classes/abc/d.pdf without version information. Can anyone explain to me what went wrong & how to fix? Thanks in advance.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Charlie
  • 639
  • 9
  • 19
  • "I copied the war file to webapps directory, renamed it to: xyzservice.war" - and presumably it's then been extracted as /usr/local/tomcat-8.0.36-8082/webapps/xyzservice on the local file system, hence the fil you're getting. That seems entirely reasonable to me... – Jon Skeet Jun 29 '17 at 06:26
  • The extraction is as expected, but AImpl.class.getResource() did not reflect this, it is still looking for the original name directory structure. Any fix? – Charlie Jun 29 '17 at 07:02
  • Ah, I see - sorry, misunderstood which way round it was. Just to check, are you certain that it *has* actually been extracted as the file you're expecting? – Jon Skeet Jun 29 '17 at 07:05
  • yes. It was extracted as the expected: war file name as the root directory name. The real question here is how maven packaging & AImpl.class.getResource(...).getFile() & Tomcat work together. – Charlie Jun 29 '17 at 07:24

2 Answers2

1

It's obvious from your post that your web application is extracted with name xyzservice-2.2.2-SNAPSHOT inside tomcat webapps directory. May be you had renamed the war file, but not the directory inside it.

Please ensure to rename the directory without version i.e xyzservice and try again.

If you would like the version to be removed at build time, please refer to the below post and include warName tag without version inside configuration section of your maven-war-plugin in build tag.

how to Build project with maven without version

JSN
  • 2,035
  • 13
  • 27
  • does it mean that maven packaged the war file with original war file's name for its directory structure, not the renamed war file? If I have to change the directory name inside war before deployment, that is err-prone. Any better idea? – Charlie Jun 29 '17 at 06:57
  • Someone voted down my question, so that I could not up vote your answer. – Charlie Jun 29 '17 at 07:26
  • @Charlie - Your question should read something like **Change WAR name for deployment**. It's possibly a duplicate of the question I referred in my answer. – JSN Jun 29 '17 at 07:28
  • The real question here is how maven packaging & AImpl.class.getResource(...).getFile() & Tomcat work together. Very interesting, I just up voted your answer successfully. Thanks – Charlie Jun 29 '17 at 08:09
1

Look at the initial war file - it is named as artifactID-version.war so just renaming the .war file won't help. You'd have to adjust the version in pom.xml.

Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
  • The real question here is how maven packaging & AImpl.class.getResource(...).getFile() & Tomcat work together – Charlie Jun 29 '17 at 07:27
  • Maybe it's what it is apparent. Did you try what I mentioned? – Aakash Verma Jun 29 '17 at 07:29
  • I didn't quite get "adjust the version in pom.xml", I read the answers/comments/links, the version number can be removed by @beginner 's answer. do you have any comment on "The real question..."? – Charlie Jun 29 '17 at 07:57
  • Yea that's what I had meant by adjustment. And about your "real question", it is quite clear that `getResource()` contacts Tomcat to get the hosted path and so that means Tomcat has interpreted it using the artifactId and version number. – Aakash Verma Jun 29 '17 at 08:03
  • Make sense. Thanks alot, @Aakash Verma – Charlie Jun 29 '17 at 08:19