我正在寻找有关放置图像文件 maven web 项目的建议。 src/main/java 下的一个类需要使用图像文件。我的问题是,如果我将图像文件放在 src/main/webapp/images 以及 src/main/resources/Images 下,那么应用程序服务器无法在运行时找到该路径(myeclipse 可以),因为 war archive 没有指定路径“src/main/webapp/images”。
我的问题是我应该把我的项目可以找到的图像文件放在哪里而不提示任何错误或异常。
我正在使用
- 具有成熟风格的 Java Web 应用程序
- MyEclipse 10
- 应用服务器:JBoss 6
目前我有以下目录结构
项目目录结构
-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 的目录结构看起来像
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 中,我正在尝试访问图像
第一次尝试
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);
作为oers说我应该使用
MyClass.class.getResource("/images/image.jpg")
添加建议的解决方案后(但仍然不起作用)
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);
提前致谢
最佳答案
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
关于java - 如何在 Maven 项目中打包资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8134179/