I'm trying to reference some files in a Java application that will be copied into a WAR.
I'm new to Maven, but basically the files are
- Main
- Java
- package
- Class ---- FROM HERE
- webapp
- resources
- folder ---- TO HERE
I've tried a relative paths such as
File source = new File("../../../webapp/resources/folder");
But can never access that folder from the class.
Is there an alternative approach to this?
2条答案
按热度按时间gpfsuwkq1#
Always remember that Maven's policy is
convention over configuration
.That being said, in your case, while using Maven, you need to follow the Maven Standard Directory Structure .
Create a directory structure like
src/main/java
and put your package(s) in thejava
folder.For any resources, create a folder structure like
src/main/resources
and put your resources in theresources
folder.After you do this, lets say that there is a file named
readme.txt
insidesrc/main/resources
directory, then you can simply access this file using:File file = new File("readme.txt");
Maven will always treat
src/main/resources
as the root for any resources.Another example is lets say you have a file named
hello.txt
insrc/main/resources/somefolder/hello.txt
then you can access it using:File file = new File("somefolder/readme.txt");
waxmsbnn2#
i have done the same thing you are looking for. same i needed to have latest generated class to be replaced from the classes in war file. so that in
pom.xml
file you will get the path for the class files getting generated. like:so in my case it was
<directory>${project.build.directory}\classes</directory>
here${project.build.directory}
is the variable initialized at application context level and were returning the project directory path.so you can try this out.
this program is for
JBOSS 1.7
which used to replace all latest created.class,.js
and.jps
files in war so that after running this program client will get latest updated files in your dev environment. it will reduce the effort of deploying your war files to server again and again because for heavy applicationMaven clean
andinstall
is very time taking task. edit the program as per your convenience.