Maven resource files, put java class files and property files in same directory

后端 未结 3 395
Happy的楠姐
Happy的楠姐 2021-01-12 15:56

I have java class files and property files in the same directory.

src/main/java/com/berlin/Test.class

src/main/java/com/berlin/Test.properties

With t

相关标签:
3条回答
  • 2021-01-12 16:44

    First you should place your resources into src/main/resources/com/berlin/Test.properties and your java source files into src/main/java/com/berlin/Test.java...Never put compiled classes into src/ folder... furthermore you should remove the configuration:

      <finalName>${project.artifactId}</finalName>
      <sourceDirectory>src</sourceDirectory>
      <testSourceDirectory>test</testSourceDirectory>
      <resources>
            <resource>
                <directory>src</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
    

    from your pom, cause you don't need it and furthermore it's wrong (Convention over configuration paradigm!). Take a look at the default folder layout in maven.

    The Maven Way is to put the source files (*.java) into src/main/java, the resources src/main/resources. The unit test classes src/test/java and src/test/resources after compiling it will be put into target/classes or for the tests target/test-classes.

    0 讨论(0)
  • 2021-01-12 16:46

    For the Maven reasons, you should place properties file into src/main/resources, not in src/main/java directory (keeping the same subfolder structure).

    That being said, to get what you want you need to replace (in your pom.xml) src with src/main/java. Not tested, but if you have problems please share.

    0 讨论(0)
  • 2021-01-12 17:00
    <build>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
            <testResource>
                <directory>src/test/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
        </testResources>
    </build>
    
    0 讨论(0)
提交回复
热议问题