title: How to pack resources in a Maven Project?
body: <p>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".</p>
  
  <p>My question is where should i put the image file that my project can find it without prompting any error or exception.</p>
  
  <p>I am using </p>
  
  <ul>
  <li>Java Web Application with Mavenized falvour</li>
  <li>MyEclipse 10</li>
  <li>Application Server: JBoss 6</li>
  </ul>
  
  <p>Currently i do have following dir structure</p>
  
  <p><em>Project Directory Structure</em></p>
  
  <pre><code>-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
  </code></pre>
  
  <p>And <em>pom.xml's build snippet</em> is as follows</p>
  
  <pre><code>&lt;build&gt;
      &lt;sourceDirectory&gt;${basedir}/src/main/java&lt;/sourceDirectory&gt;
      &lt;outputDirectory&gt;${basedir}/target/${project.artifactId}/classes&lt;/outputDirectory&gt;
      &lt;resources&gt;
        &lt;resource&gt;
          &lt;directory&gt;${basedir}/src/main/resources&lt;/directory&gt;
          &lt;excludes&gt;
            &lt;exclude&gt;**/*.java&lt;/exclude&gt;
          &lt;/excludes&gt;
        &lt;/resource&gt;
      &lt;/resources&gt;
      &lt;testResources&gt;
        &lt;testResource&gt;
          &lt;directory&gt;${basedir}/src/test/resources&lt;/directory&gt;
        &lt;/testResource&gt;
      &lt;/testResources&gt;
      &lt;finalName&gt;${project.artifactId}&lt;/finalName&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
          &lt;configuration&gt;
            &lt;source&gt;${jdk.version}&lt;/source&gt;
            &lt;target&gt;${jdk.version}&lt;/target&gt;
            &lt;optimize&gt;true&lt;/optimize&gt;
            &lt;useProjectReferences&gt;true&lt;/useProjectReferences&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.1.1&lt;/version&gt;
          &lt;configuration&gt;
            &lt;warSourceDirectory&gt;${basedir}/src/main/webapp&lt;/warSourceDirectory&gt;
            &lt;webResources&gt;
              &lt;resource&gt;
                &lt;directory&gt;${basedir}/src/main/resources&lt;/directory&gt;
                &lt;directory&gt;${basedir}/src/main/resources/Images&lt;/directory&gt;
              &lt;/resource&gt;
            &lt;/webResources&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
    &lt;/build&gt;
  </code></pre>
  
  <p>and MyProject.war's dir structure look like</p>
  
  <pre><code>     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
  </code></pre>
  
  <p>In MyClass i am trying to access image</p>
  
  <p><em>1st Try</em></p>
  
  <pre><code>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);   
  </code></pre>
  
  <p><em>2nd Try</em></p>
  
  <pre><code>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);
  </code></pre>
  
  <p>as oers Said i should use </p>
  
  <pre><code>MyClass.class.getResource("/images/image.jpg")
  </code></pre>
  
  <p>after adding suggested solution (but it still dont work)</p>
  
  <pre><code>    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);  
  </code></pre>
  
  <p>Thanks in advance </p>