Jboss部署war以及獲取Resource的真實路徑
最近在將一個SpringBoot
項目打成war
包部署到Jboss
中,中途遇到一些問題記錄。
Jboss上部署war
普通的SpringBoot
項目目錄結構如下
.
├── src
└── main
├── java
└── resources
當我們打出war
包后,想在Jboss
中部署時需要添加jboss-deployment-structure.xml
文件
關於此文件的配置可參考Jboss as 7 Developer Guide
加入后目錄結構如下
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ ├── resources
│ │ │ ├── application.yml
│ │ │ └── META-INF
│ │ └── webapp
│ │ └── WEB-INF
│ │ ├── jboss-deployment-structure.xml
│ │ └── jboss-web.xml
jboss-deployment-structure.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<!-- 需要排除的 -->
<exclusions>
<module name="javax.validation.api" />
<module name="org.hibernate.validator" />
<!--Log4j exclude-->
<module name="org.slf4j" />
<module name="org.slf4j.impl" />
</exclusions>
<!-- 需要依賴的模塊 -->
<dependencies>
<!-- This one always goes last. -->
<module name="javax.api" export="true"/>
</dependencies>
</deployment>
</jboss-deployment-structure>
jboss-web.xml
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd">
<jboss-web>
<context-root>app</context-root>
</jboss-web>
Jboss中獲取Resource的真實路徑
在使用過程中,因為在Resouce
中放了一些文件,需要去獲取文件內容
最開始使用如下方法去獲取
// vfs:/content/app.war/WEB-INF/classes/data/data.yaml
new ClassPathResource("data/data.yaml")).getURI()
當我嘗試創建一個File
時報錯找不到
因此借助JBoss VFS
去獲取當前資源的真實路徑
MAVEN
中添加
<!-- JBoss is using Virtual File System (VFS) -->
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-vfs</artifactId>
<version>3.2.14.Final</version>
</dependency>
具體使用:
VirtualFile content = (VirtualFile) this.getClass().getClassLoader().getResource("data/data.yaml").getContent();
// $JBOSS_HOME/tmp/vfs/temp/tempc755413fe36e407c/app.war-64dfd9c1b9e1463e/WEB-INF/classes/data/data.yaml
String realPath = content.getPhysicalFile().getPath()