开发者社区> 问答> 正文

Maven运行web项目无法加载resource目录中配置文件-HTML报错

"

<span style=""color:#333333;font-family:Helvetica, Tahoma, Arial, sans-serif;font-size:14px;line-height:24px;background-color:#F5F5F5;"">Maven项目tomcat:run的方式运行取不到resource目录下配置文件

<build>
    <finalName>pointexchange</finalName>
	<plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>tomcat-maven-plugin</artifactId>
			<version>1.0-beta-1</version>
			<configuration>
				<url>http://localhost:8080/manager/html</url>
				<server>tomcat6</server>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.1</version>
			<configuration>
				<source>1.6</source>
				<target>1.6</target>
			</configuration>
		</plugin>
取配置文件的代码

String filePath = "/config.propites";
InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
运行命令
tomcat:run

web项目,访问controller中一个properties工具类取properties文件,得到的inputstream 总是为空。
把项目打war包时可以正常取到配置。

怎么解决?

"

展开
收起
montos 2020-05-31 11:04:35 874 0
1 条回答
写回答
取消 提交回答
  • "这个跟Maven 没关系。是你部署配置错了。######回复 <a href=""http://my.oschina.net/u/930383"" class=""referer"" target=""_blank"">@zhang1hang2 : eclipse。######回复 <a href=""http://my.oschina.net/yuer"" class=""referer"" target=""_blank"">@Minuzy : 你这是什么ide下的操作? eclipse项目右键properties里没看到 dep.. 这个选项啊?######回复 <a href=""http://my.oschina.net/u/930383"" class=""referer"" target=""_blank"">@zhang1hang2 : 项目右键 properties -> Deployment Assembly 然后 Add -> Folder 选择你要部署的目录。跟Maven是没有关系的。希望能帮到你 :)######我知道是部署有地方不对,所以贴出了pom的配置。######

    或者去项目根目录下找这个文件 /.settings/org.eclipse.wst.common.component  然后配置中添加

    <pre class=""brush:xml; toolbar: true; auto-links: false;""><wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/> <wb-resource deploy-path="/" source-path="/src/main/webapp"/>######回复 <a href=""http://my.oschina.net/u/930383"" class=""referer"" target=""_blank"">@zhang1hang2 : 路径问题。跟配置无关了######谢谢你的指导。我用你上一种方法,确实可以调试了。但是使用的eclipse启动tomcat的方式,而不是tomcat:run的方式,tomcat:run的话怎么让它可以取到resouce中配置文件?######

    请问楼主这个问题你是怎么解决的,我也是resources目录下的文件没编译和发布到classes的目录下,我都是手动拷贝这些文件的。我也把编译存放classes的目录修改成“test-prject/src/main/webapp/WEB-INF/classes” 没有用maven的target目录下了

    "

    2020-05-31 11:04:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
使用CNFS搭建弹性Web服务 立即下载
天猫 HTML5 互动技术实践 立即下载
天猫HTML5互动技术实践 立即下载