8

I am looking for suggestion in putting image file maven web project. One of classes under src/main/java need to use an image file. My problem is, if i put image file under src/main/webapp/images as well as under src/main/resources/Images then application server cannot find that path on runtime(where myeclipse can ) because war archive do not have specified path "src/main/webapp/images".

My question is where should i put the image file that my project can find it without prompting any error or exception.

I am using

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

Currently i do have 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 pom.xml's build snippet is as follows

<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 MyProject.war's dir structure 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 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);

as oers Said i should use

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

after adding suggested solution (but it still dont 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);  

Thanks in advance

oers
  • 18,436
  • 13
  • 66
  • 75
Rehman
  • 3,908
  • 6
  • 28
  • 29

3 Answers3

15

The default resource directory for all Maven projects is src/main/resources which will end up in target/classes and in WEB-INF/classes in the WAR. The directory structure will be preserved 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 put your resources is in the src/main/resources

Reference: Adding and Filtering External Web Resources

Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
1

It seems that your problem is the way you read the image. You can't (or shouldn't) read the image from a Path. The Path might change on every system.

You need to read the image from the classpath like:

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

or

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

For this to work, the image Folder needs to be on the classpath. Maven will put everything in the classpath, that is under main/resources. So thats where your image folder should be.

oers
  • 18,436
  • 13
  • 66
  • 75
  • i have changed code to MyClass.class.getResource("Images/myImage.png"); but still it is unable to find any path or URL. – Rehman Nov 15 '11 at 10:10
  • could you list how the war file looks directory/file structure)? You can edit your question to insert this information. – oers Nov 15 '11 at 10:22
  • i have tried both and in both cases application do not find image file and throws exception with message "ProjectPath/MyProject/Images/myImage.png" is not found – Rehman Nov 15 '11 at 10:52
  • please show the content of your war file and the exact source code for loading the image in the question, I think we misunderstand each other :) – oers Nov 15 '11 at 11:14
1

You can use maven-resources-plugin to copy things to desired location.

something like this

<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 what you need is a properties file where you maintain path to the resources folder. That path will be an absolute path. Depending on your deployment you change that path in the properties file.

Ravi Bhatt
  • 3,147
  • 19
  • 21
  • With this suggestion i can pack an image from src/main/resources/Images into war/Images. But can you please specify how can i access it if i want to run a MyClass through unit test. i mean how can i access that image if i like to run application in myeclipse instead of running on application server. Thanks – Rehman Nov 15 '11 at 11:47