Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is maven not packaging my hibernate-mapping file?

I'm using Maven 2 and Hibernate for a Java web app.

I have a Hibernate-mapping file, Domain.hbm.xml in the same source folder with several domain classes.

If I run mvn clean package I find that this file is not copied into the war file in the target folder.

However, if I do only mvn clean and then go to eclipse and select Clean from the Project menu, the file is sent to the appropriate place in the target. I can then do mvn package to sucessfully package a war file that has access to the database.

Is a pom setting for this? What's going on?

like image 698
Eric Wilson Avatar asked Aug 27 '10 20:08

Eric Wilson


2 Answers

If I run mvn clean package I find that this file is not copied into the war file in the target folder.

Then the mapping files are probably not in the default resource directory (src/main/resources) but in the source directory (src/main/java) and Maven won't copy them to the build output directory by default.

However, if I do only mvn clean and then go to eclipse and select Clean from the Project menu, the file is sent to the appropriate place in the target. I can then do mvn package to sucessfully package a war file that has access to the database.

Under Eclipse, my guess is that files from sources directories get copied to the build output directory, including application resource files. So this somehow "trick" Maven when you run package after the Eclipse clean step. But this doesn't really matter, the master is Maven, everything else is irrelevant.

To fix it, either move your mapping files to src/main/resources. Or keep them in src/main/java and declare the sources directory as an "extra" resource directory:

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

Some people do this, I prefer to use separate directories.

like image 69
Pascal Thivent Avatar answered Nov 15 '22 20:11

Pascal Thivent


Resources other than Java sources you want to end up on the classpath directly (in your case inside /WEB-INF/classes) must go in your src/main/resources directory, not src/main/java.

like image 41
Ramon Avatar answered Nov 15 '22 20:11

Ramon