Maven项目防止配置文件读取不到

1. 配置方法

我们使用IDEA创建一个基本的Maven项目时,默认情况下是从src/main/resources中读取xml或properties配置文件的。但是我们有时会把一些xml或properties配置文件放在代码目录下(即src/main/java下)。因此我们在pom.xml中进行如下配置(增加末尾的build部分),这样可以防止src/main/java中的配置文件读取不到。


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>untitled18artifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>11maven.compiler.source>
        <maven.compiler.target>11maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
    	
    dependencies>

    
    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
                <filtering>falsefiltering>
            resource>

            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>
    build>
    
project>

其中

 
    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
                <filtering>falsefiltering>
            resource>

            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>
    build>

这个部分就能保证既从src/main/resources中读取配置文件,也从src/main/java中读取配置文件。

你可能感兴趣的:(Java基础,maven,java)