Why does maven not copy the properties files during the build process?

北战南征 提交于 2019-12-03 10:22:33

What is your project's build path configured to be in Netbeans? You might try changing it to src/main/webapp/WEB-INF/classes. This way class files compiled from your src/main/java folder and any resources you have under src/main/resources should get included in the generated WAR. You would then be able to access your config.properties file if you place it under the src/main/resources folder.

You might also review any includes sections in your pom.xml and ensure you're not accidentally excluding something (if you explicitly include some things, you're likely implicitly excluding everything else).

Maven doesn't copy resources from the java source tree by default, but you can get it do that by adding this to your pom.xml:

<build>
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <excludes><exclude>**/*.java</exclude></excludes>
    </resource>
  </resources>
</build>

Make sure you exclude the java source files.

From http://www.ninthavenue.com.au/how-to-change-mavens-default-resource-folder

Try putting your config.properties under src\main\resources\com\myapp. I was able to test this on a local project. I'm running Maven 3.0.2.

Created a mvn sample project with the webapp archetype:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

Created a directory at src/main/resources/com/foo and put a foo.properties file under it.

Ran a build:

mvn clean install

Then, when looking in the resulting target directory, the foo.properties file appears:

ls -al target/my-webapp/WEB-INF/classes/com/foo/
-rw-r--r--  1 sblaes  staff    4 Apr  2 22:09 foo.properties

You might try those steps on your machine. If that works, then start trying to simplify your POM above by removing things from it to see if it starts working. Trial and error is no fun, but I just don't see anything above that should be breaking it.

By default maven will include all files under resources folder. If your properties files are not in the resource folder, then you need to include the following in the pom.xml file under the build section.

<build>
/* other tags like <plugins> goes here */
<sourceDirectory>src/main/java</sourceDirectory>  
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
/* other tags like <plugins> goes here */
</build>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!