4

While installing my project to local repository with mvn install command, hibernate mapping files are excluded from generated JAR.

I have the *.hbm.xml files under src/main/resources/traffic_domain/mapping/, so it should be ok as it is following standard directory layout for maven.

my POM file looks like that:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>masters.traffic</groupId>
  <artifactId>traffic_domain</artifactId>
  <packaging>jar</packaging>
  <name>traffic_domain</name>  
  <version>0.1.0</version>   
  <build>
        <sourceDirectory>src</sourceDirectory>       
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>                    
                </configuration>
            </plugin>
        </plugins>
  </build>
...
</project>

Directory structure is following:

traffic_domain
|-- pom.xml
|-- src
    |-- main
        |-- java
            |-- ...
        |-- resources
            |-- traffic_domain
                |-- mapping
                    |-- Access.hbm.xml
                    |-- *.hbm.xml

traffic_domain.jar is one of JARs needed for my web application to run. While starting tomcat, I'm getting such exception:

Caused by: org.hibernate.MappingNotFoundException: resource: main/resources/traffic_domain/mapping/Access.hbm.xml not found at...

How to fix that ?


Update: Ok (ad. Pascal Thivent comment), that is the content of generated JAR after invoking mvn clean package:

c:\Users\jwa\Desktop\tets\traffic_domain\target>jar xvf traffic_domain-0.1.0.jar
  created: META-INF/
 inflated: META-INF/MANIFEST.MF
  created: main/
  created: main/java/
  created: main/java/traffic_domain/
  created: main/java/traffic_domain/bean/
  created: main/java/traffic_domain/logic/
  created: main/java/traffic_domain/tools/
 inflated: Access.hbm.xml
 inflated: District.hbm.xml
 inflated: main/java/traffic_domain/bean/Access.class
 inflated: main/java/traffic_domain/bean/District.class
 inflated: main/java/traffic_domain/bean/PostalCode.class
 inflated: main/java/traffic_domain/bean/Street.class
 inflated: main/java/traffic_domain/bean/TrafficCondition.class
 inflated: main/java/traffic_domain/logic/AccessFacade.class
 inflated: main/java/traffic_domain/logic/LocationFacade.class
 inflated: main/java/traffic_domain/logic/TrafficConditionFacade.class
 inflated: main/java/traffic_domain/tools/HibernateUtil.class
 inflated: PostalCode.hbm.xml
 inflated: Street.hbm.xml
 inflated: TrafficCondition.hbm.xml
  created: META-INF/maven/
  created: META-INF/maven/masters.traffic/
  created: META-INF/maven/masters.traffic/traffic_domain/
 inflated: META-INF/maven/masters.traffic/traffic_domain/pom.xml
 inflated: META-INF/maven/masters.traffic/traffic_domain/pom.properties

For comparison, here is the content of that JAR created by Eclipce, which is working:

C:\Users\jwa\Desktop\correct>jar xvf traffic_domain.jar
 inflated: main/java/traffic_domain/bean/PostalCode.class
 inflated: traffic_domain/mapping/Access.hbm.xml
  created: main/resources/
  created: main/resources/traffic_domain/
 inflated: traffic_domain/mapping/Street.hbm.xml
 inflated: main/java/traffic_domain/logic/AccessFacade.class
 inflated: main/resources/traffic_domain/mapping/TrafficCondition.hbm.xml
 inflated: traffic_domain/mapping/PostalCode.hbm.xml
  created: main/java/traffic_domain/bean/
 inflated: main/java/traffic_domain/tools/HibernateUtil.class
  created: main/
 inflated: main/java/traffic_domain/bean/TrafficCondition.class
 inflated: mapping/Street.hbm.xml
 inflated: PostalCode.hbm.xml
 inflated: main/java/traffic_domain/bean/Access.class
  created: traffic_domain/mapping/
 inflated: District.hbm.xml
  created: traffic_domain/
 inflated: traffic_domain/mapping/TrafficCondition.hbm.xml
  created: main/java/traffic_domain/tools/
 inflated: Access.hbm.xml
 inflated: traffic_domain/mapping/District.hbm.xml
  created: main/java/traffic_domain/logic/
  created: mapping/
  created: main/resources/traffic_domain/mapping/
 inflated: mapping/TrafficCondition.hbm.xml
 inflated: main/resources/traffic_domain/mapping/Access.hbm.xml
 inflated: mapping/Access.hbm.xml
 inflated: main/java/traffic_domain/bean/Street.class
  created: main/java/
 inflated: main/java/traffic_domain/logic/TrafficConditionFacade.class
 inflated: main/resources/traffic_domain/mapping/PostalCode.hbm.xml
  created: main/java/traffic_domain/
 inflated: TrafficCondition.hbm.xml
 inflated: main/resources/traffic_domain/mapping/District.hbm.xml
 inflated: mapping/PostalCode.hbm.xml
 inflated: Street.hbm.xml
 inflated: main/resources/traffic_domain/mapping/Street.hbm.xml
 inflated: main/java/traffic_domain/logic/LocationFacade.class
 inflated: main/java/traffic_domain/bean/District.class
 inflated: mapping/District.hbm.xml

Here is the part of hibernate.cfg.xml, which is loading the mappings (changed after axtavt advice):

<mapping resource="traffic_domain/mapping/Access.hbm.xml"/>
<mapping resource="traffic_domain/mapping/Street.hbm.xml"/>
<mapping resource="traffic_domain/mapping/District.hbm.xml"/>
<mapping resource="traffic_domain/mapping/PostalCode.hbm.xml"/>
<mapping resource="traffic_domain/mapping/TrafficCondition.hbm.xml"/>
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
jwaliszko
  • 16,942
  • 22
  • 92
  • 158
  • "I have the *.hbm.xml files under src/main/resources/traffic_domain/mapping/, so it should be ok as it is following standard directory layout for maven." If you know you are using the standard maven directory structure, why did you explicitly specify your source directory in your pom.xml? Removing the element will definitely solve the problem as Pascal has already mentioned. – Jesse Webb Jun 29 '10 at 19:02
  • Hello, finally I have changed everything in my 3 projects to standard maven directory layout. When it comes to m2eclipse, actually I have been using m2eclipse earlier, but I was not using it as properly as it should be used. Now I have learnt how to use that. It is ok now, I have no junks in *.jar files, directory structures are ok, and I think everything is correct. Thanks again. – jwaliszko Jun 30 '10 at 23:22

3 Answers3

3

By default, resources from src/main/resources are supposed to be copied during the build process into target/classes. But as I mentioned in your previous question, the problem here is the following line:

<sourceDirectory>src</sourceDirectory> 

Because of this line, Maven is considering everything under src as sources and main/resources gets copied to target/classes. So, while you are using the "default layout", you are still not using Maven's default configuration and, instead of having the content of src/main/resources copied to target/classes, main/resources is included.

So, as I recommended in my answer, use Maven's defaults (default layout, default configuration), especially if you are a Maven beginner:

  • move your Java sources into src/main/java (and remove the sourceDirectory element)

I have no idea how things are working under Eclipse... but your POM is clearly incorrect.


Follow-up: There is still something very wrong with your project: main/java is not supposed to be part of the package name and why do resources end up at the root of the jar? Could you show the latest version of your POM?

Regarding the jar produced by Eclipse, it may be working but the only thing I see when I look at it is a huge mess (duplicate files, wrong Java packaging, etc). This may result from differences between the Maven project setup and the Eclipse project setup though.

I don't know if you are using m2eclipse, but that would be my suggestion here. The project setup under Eclipse needs to be aligned with the Maven project setup and it currently isn't. m2eclipse can do that for you by deriving the settings from the POM.

Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Hello, as You said, I've deleted the sourceDirectory entry from POM file, because I have standard layout now. Indeed, the *.hbm.xml files are created under target/classes after invoking mvn compile command. But, in the JAR file, after invoking mvn package command, they are still NOT included, and I'm getting still that creepy exception.. – jwaliszko Jun 29 '10 at 19:23
  • @Jarek This is weird because `mvn package` does nothing more than jaring `target/classes`. Could you run `mvn clean package` and update the question with the content of the generated jar (`jar xvf target/traffic_domain-0.1.0.jar`). I suspect another issue now (how do you tell hibernate to load mappings?) – Pascal Thivent Jun 29 '10 at 19:32
1

If you need include *.hbm.xml on the phase compile; edit your pom.xml and add the next code:

<build>
                <resources>
            <resource>
                <directory>source/com/qfund/orm/</directory>
                <targetPath>com/qfund/orm/</targetPath>
                <includes>
                    <include>*.hbm.xml</include>
                </includes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/java/</directory>
                <includes>
                    <include>*.xml</include>
                    <include>*.xsd</include>
                    <include>*.xslt</include>
                    <include>*.properties</include>
                </includes>
            </testResource>
        </testResources>
</build>
dmotta
  • 1,843
  • 2
  • 21
  • 32
0

Look at the exception:

Caused by: org.hibernate.MappingNotFoundException: resource: main/resources/traffic_domain/mapping/Access.hbm.xml not found at...

Hibernate tries to find a mapping file at main/resources/traffic_domain/mapping/Access.hbm.xml, when it should be accessed as traffic_domain/mapping/Access.hbm.xml. Probably you specified the wrong path to your mapping file in .cfg.xml.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • No it's not the problem, because while building traffic_domain project in Eclipse, everything is ok, there is no problem in finding Access.hbm.xml. The error occurs only when traffic_domain project is build with Maven. – jwaliszko Jun 29 '10 at 10:36
  • @Jarek It is the problem. If it works in Eclipse as you say, you are using maven directory layout improperly. Maven directory layout expects `main/resources` to be a part of classpath, so your file can be accessed as a classpath resource named `traffic_domain/mapping/Access.hbm.xml`. If you use m2eclipse, it is done automatically. Otherwise, you can add `main/resources` to classpath in project properties. – axtavt Jun 29 '10 at 11:00
  • Hello, I've changed that paths in hibernate.cfg.xml file (to traffic_domain/mapping/Access.hbm.xml), but I'm getting still the same problem. – jwaliszko Jun 29 '10 at 19:29
  • @Jarek You situation seems to be more complex than a simple maven layout, so see Pascal's answer. – axtavt Jun 29 '10 at 21:22