How to pack resources in a Maven project?

I am looking for a proposal to create a web project for a maven image file. One of the classes under src / main / java should use an image file. My problem is that if I put the image file in src / main / webapp / images, as well as in src / main / resources / Images, then the application server cannot find this path at runtime (where myeclipse can be used), because the military archive does not have the specified path "src / main / webapp / image".

My question is where should I put the image file that my project can find without causing any errors or exceptions.

I use

  • Java web application with Mavenized falvour
  • MyEclipse 10
  • Application Server: JBoss 6

I currently have the following dir structure

Project Directory Structure

-src/main/java -- classes -src/main/resources -- applicationContext.xml -src/main/resources/Images -- myImage.png (target image) -src/test/java -- test classes -src/test/resources (nothing) -src -- main --- webapp --WEB-INF ---faces-config.xml ---web.xml --Images --OtherImages --myImage.png( second place for image) --Css --Meta-INF --login.jspx --jsfPageType1 --page1.jspx --jsfPageType2 --page2.jspx -target -pom.xml 

And the pom.xml assembly fragment looks like this

 <build> <sourceDirectory>${basedir}/src/main/java</sourceDirectory> <outputDirectory>${basedir}/target/${project.artifactId}/classes</outputDirectory> <resources> <resource> <directory>${basedir}/src/main/resources</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <testResources> <testResource> <directory>${basedir}/src/test/resources</directory> </testResource> </testResources> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> <optimize>true</optimize> <useProjectReferences>true</useProjectReferences> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory> <webResources> <resource> <directory>${basedir}/src/main/resources</directory> <directory>${basedir}/src/main/resources/Images</directory> </resource> </webResources> </configuration> </plugin> </plugins> </build> 

and the structure MyProject.war look like

  MyProject.war --Images ---OtherImages --Css --Meta-INF --login.jspx --jsfPageType1 --page1.jspx --jsfPageType2 --page2.jspx --WEB-INF --classes ---applicationContext.xml ---com(classes package) ---Images ----myImage.png --lib --web.xml --faces-config.xml 

In MyClass, I am trying to access an image

1st Try

 private static String PATH_TITLE_IMAGE = "src/main/resources/Images/myImage.png"; com.itextpdf.text.Image bckGrndImage = com.itextpdf.text.Image.getInstance(PATH_TITLE_PAGE_IMAGE); 

2nd Try

 private static String PATH_TITLE_IMAGE = "src/main/webapp/Images/myImage.png"; com.itextpdf.text.Image bckGrndImage = com.itextpdf.text.Image.getInstance(PATH_TITLE_PAGE_IMAGE); 

asers. I have to use

 MyClass.class.getResource("/images/image.jpg") 

after adding the proposed solution (but it still does not work)

  private static String PATH_TITLE_IMAGE = "Images/myImage.png"; URL url = MyClass.class.getResource(PATH_TITLE_IMAGE); com.itextpdf.text.Image bckGrndImage = com.itextpdf.text.Image.getInstance(url); 

Thank you in advance

+4
source share
3 answers

The default resource directory for all Maven src/main/resources projects, which will appear in the target / class and in WEB-INF / classes in the WAR. Directory structure will be saved. in the process.

 . |-- pom.xml `-- src `-- main |-- java | `-- com | `-- example | `-- projects | `-- SampleAction.java |-- resources | `-- images | `-- sampleimage.jpg `-- webapp |-- WEB-INF | `-- web.xml |-- index.jsp `-- jsp `-- websource.jsp 

The suggested way to post your resources is in src/main/resources

Link: Adding and filtering external web resources

+13
source

It seems your problem is how you read the image. You cannot (or should not) read an image from the Path. The path may change in every system.

You need to read the image from the classpath, for example:

 MyClass.class.getResource("/images/image.jpg") 

or

 MyClass.class.getResourceAsStream("/images/image.jpg") 

For this to work, the image folder must be in the classpath. Maven will put everything in the classpath, i.e. in main / resources. So this is where your image folder should be.

+1
source

You can use the maven-resources-plugin to copy things to the right place.

something like that

 <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>copy-resources</id> <!-- here the phase you need --> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory><!--your path here--></outputDirectory> <resources> <resource> <directory><!--path-here--></directory> </resource> </resources> </configuration> </execution> </executions> </plugin> 

EDIT: based on your comment below, I think you need a properties file in which you maintain the path to the resource folder. This path will be the absolute path. Depending on your deployment, you change this path in the properties file.

0
source

Source: https://habr.com/ru/post/1381267/


All Articles