简体   繁体   English

如何在Maven项目中打包资源?

[英]How to pack resources in a Maven Project?

I am looking for suggestion in putting image file maven web project. 我正在寻找建立图像文件maven web项目的建议。 One of classes under src/main/java need to use an image file. src / main / java下的一个类需要使用图像文件。 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". 我的问题是,如果我将图像文件放在src / main / webapp / images以及src / main / resources / Images下,那么应用程序服务器在运行时(myeclipse可以在哪里)找不到该路径,因为war存档没有指定的路径“SRC /主/ web应用/图像”。

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 带有Mavenized falvour的Java Web应用程序
  • MyEclipse 10 MyEclipse 10
  • Application Server: JBoss 6 应用服务器:JBoss 6

Currently i do have following dir structure 目前我确实有以下dir结构

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

and MyProject.war's dir structure look like 和MyProject.war的dir结构看起来像

     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 在MyClass中,我正在尝试访问图像

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 作为oers说我应该使用

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 提前致谢

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. 所有Maven项目的默认资源目录是src/main/resources ,它们最终将出现在目标/类和WAR中的WEB-INF / classes中。 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 建议的资源放置方式是在src/main/resources

Reference: Adding and Filtering External Web Resources 参考: 添加和过滤外部Web资源

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. Maven会将所有内容放在classpath中,即main / resources下。 So thats where your image folder should be. 那就是你的图像文件夹所在的位置。

You can use maven-resources-plugin to copy things to desired location. 您可以使用maven-resources-plugin将内容复制到所需位置。

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. 根据您的部署,您可以在属性文件中更改该路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM