如何在Maven项目中打包资源? [英] How to pack resources in a Maven Project?

查看:125
本文介绍了如何在Maven项目中打包资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找放置图像文件maven web项目的建议。 src / main / java下的一个类需要使用图像文件。我的问题是,如果我将图像文件放在src / main / webapp / images以及src / main / resources / Images下,那么应用服务器在运行时(myeclipse可以在哪里)找不到该路径,因为war archive没有指定路径src / main / webapp / images。

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.

我正在使用


  • 带有Mavenized falvour的Java Web应用程序

  • MyEclipse 10

  • 应用程序服务器:JBoss 6

目前我确实有以下目录结构

Currently i do have following dir 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

pom.xml的构建片段如下

<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>

和MyProject.war的dir结构看起来像

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

在MyClass中我试图访问图像

In MyClass i am trying to access image

第一次尝试

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);   

第二次尝试

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表示我应该使用

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);  

提前致谢

推荐答案


所有Maven项目的默认资源目录是
src / main / resources 最终将在WAR中的目标/类和
WEB-INF /类中结束。目录结构将在此过程中保留

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

建议的资源放置方式是 src / main / resources

The suggested way to put your resources is in the src/main/resources

参考:添加和过滤外部网络资源

这篇关于如何在Maven项目中打包资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆