views:

270

answers:

1

Can someone tell me how to configure maven for it also to compile sources from the target/hibernate3/generated-sources directory? I have already read this and other posts but they don't seem to solve my problem (which indeed seems trivial).

I have used the bottom-up approach hibernate configuration for cfg.xml, hbm.xml and POJO generation (i.e. auto-generated the complete hibernate configuration out of an existing database schema). I'm also only using standard maven and hibernate3-plugin directory layouts. Yet, when executing mvn compile in the command-line while my sources are in the src/main/java and the generated sources in /target/hibernate3/generated-sources only the ones from src/main/java get compiled and copied into target/classes. I wouldn't like to generate sources into src/main/java as I'd like mvn clean to clean them.

I'd like to solve the problem using command-line, plugins and pom.xml only. Is there a way to configure maven-compiler-plugin to do so? Or is there another way?

Regards and thanks for any help.

+2  A: 

The convention is to generate code in target/generated-sources/<generator> so in your case, I would generate sources in target/generated-sources/hibernate3 (it's sad the hibernate3 plugin doesn't follow this convention by default). Anyway, things should work and actually, the POM I posted in this previous answer does (I re-ran my sample project and it definitely works). Could you be a victim of MHIBERNATE-104? Honestly, I'm confused.

As a workaround, you could use the Build Helper Maven Plugin and its build-helper:add-source goal to add your directory as source directory. See the Usage page for a configuration example.


I'm using exactly the pom you proposed here, just not plugged into the default build lifecycle (without executions).

That must be the cause of the problem: if hibernate3:hbm2java isn't part the build lifecycle, it doesn't get the opportunity to register the output directory as source folder. So Maven isn't aware of it during the "normal build" and will just ignore it.

Pascal Thivent
+1 @Pascal Thivent can you use this while generating classes from xml schema?
c0mrade
@Pascal: thanks for your answer. What really confuses me, is that the default directory `target/hibernate3/generated-sources` is not the convention. I have just checked, and none of those two directories work out of the box, still only `src/main/java` get compiled. Maybe I screwed something else up, but I'll try the second solution anyway, this should work without problems.
mmm
@c0mrade: Yes, you can (see [this answer](http://stackoverflow.com/questions/2857081/how-can-i-tell-jaxb-maven-to-genereate-multiple-schema-packages/2857613#2857613), [this one](http://stackoverflow.com/questions/2556442/maven-jaxb2xjc-jaxwswsimport-to-be-added-in-eclipse) or [this one](http://stackoverflow.com/questions/2295025/best-ways-to-manage-generated-artifacts-for-web-service-xml-bindings-in-a-java-we/2295146#2295146)).
Pascal Thivent
@mmm That's extremely weird. The solution I posted in [this answer](http://stackoverflow.com/questions/2848567/how-to-configure-hbm2java-and-hbm2dao-to-add-packagename-to-generated-classes/2852173#2852173) works *as is*. I double checked with another project on my machine that uses this configuration and confirm it definitely works. Just in case, what version of the plugin do you use?
Pascal Thivent
@Pascal: thanks a lot again. [MHIBERNATE-104](http://jira.codehaus.org/browse/MHIBERNATE-104) can indeed be the case. With the `Build Helper Maven Plugin` everything seems ok, though. Actually, without it, even the `m2eclipse -> Update Project Configuration` option didn't work (??), though this can be unrelated. BTW.: I'm using exactly the pom you proposed [here](http://stackoverflow.com/questions/2848567/how-to-configure-hbm2java-and-hbm2dao-to-add-packagename-to-generated-classes/2852173#2852173), just not plugged into the default build lifecycle (without executions).
mmm
@Pascal: I'm using version `2.2` of `hibernate3-maven-plugin`
mmm
@mmm: BTW, I created an issue for the default outputDirectory (see [MHIBERNATE-106](http://jira.codehaus.org/browse/MHIBERNATE-106)) and it appears that it will be fixed in version 3.0.
Pascal Thivent
@Pascal: Regarding unplugging `hibernate3:hbm2java` from the build lifecycle and then maven not knowing about the generated sources in the normal build: thanks again... :).
mmm