Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Thursday, July 19, 2012

Seamlessly Static Meta Model generation

I would like to introduce a simple, quick and straight forward way to create Static Meta Model classes.

First, I would like to correct a perception I had in my previous post regarding the place such files are created.
Since the Static Meta Model files are generated classes and should automatically changed for each @Entity modification they should placed in the target folder and not comitted to the repository.

Moreover, creation of static meta model files via Eclipse works correctly if you use the appropriate generation project version and te right plugin.

First step is to get the class generation jar. Put in pom.xml :
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>1.1.1.Final</version>
        </dependency>

Then in eclipse add the annotation generation plugin via Help ->Eclipse Market place -> search for "Apt M2E" and install it.

After the installation right click on your project -> properties -> Java compiler-> Annotation processing -> mark "enable project specific settings" (and actually all the checkboxes on that screen), in the Generated Source Directory put "target\generated-sources" (This will generate the classes in your target folder).

Inside the Annotation processing item there is a Factory Path item, enable this part as well and set the jar we import via maven to generate the classes. You can do it by clicking Add Variable -> M2_REPO -> Extend -> and choose the following: path : /org/hibernate/hibernate-jpamodelgen/1.2.0.Final/hibernate-jpamodelgen-1.2.0.Final.jar

Make sure only that path is checked.

As a final step, please make sure the target\generated-sources folder is on your classpath (right click-> build path -> ad as source folder).

That's it. Every change should trigger automatic static meta model generation.

Friday, June 1, 2012

Eclipse metamodel generation (JPA 2.0) issues

Update:
Please see updated post in that manner which simplifies the process a bit better.

Static Metamodel classes are uses for type safe queries in JPA2.0 standard.

There are few ways to create these classes:
1. Define the IDE (e.g. eclipse) to generate the files automatically (like the IDE automatic build which generates the class files on each Java file change).
2. Define maven to generate the files. The generation will be done when running the maven script.

 I would recommend to avoid the generation via the Eclipse IDE, since I've noticed that the generated static meta model are inaccurate (e.g. some members were missing).

 I'm not sure what the reason is since the missing member has no special attribute that differentiate it form the other class members that are generated, however, the fact is that it is constantly not generated.

Static meta model files generation via maven overcome this issue.


Good luck !

Wednesday, February 1, 2012

JPA 2.0 Metamodel generation using Eclipse Maven build

Update:
Please see this updated post in that manner which simplifies the process a bit better.


In order to write JPA2.0 queries using the criteria API in a type safe manner, one can use the corresponding @Entity generated metamodel.

This post is for developers who are familiar with this concept, and feel their current metamodel files generation process need improvement.

Over the web there are some guidelines regarding the best way to generate these static metamodel classes.
However, maybe it's just me, but I could not run them in a proper way.
Some methods generated the metamodel classes in the target directory and some simply did not work.
When running the metamodel generation tool, I had to set the pom file with the proc parameter manually with the "only" value and revert it when I wanted to generate the other java files.
(Very tedious when rapid model changes  are needed)

I would like to suggest a way to create the static metamodel files in the source directory (right beside the @Entities they refer)  AND run the build without modifying the pom/maven file.

The process is set from two steps:
1) Configure the POM.xml
2) Configure eclipse to run the build

So, first thing first - lets configure the pom file:




            org.hibernate

            hibernate-jpamodelgen

            1.1.1.Final

        

    

    

        

            

                maven-clean-plugin

                2.4.1

                

                  

                       

                     ../your_project_name/src/main/java/com/your_path/model/                     

                      

                        **/*_.java                       

                                           

                      false

                    

                  

                

              

           

            

                org.apache.maven.plugins

                maven-compiler-plugin

                2.3.2

                

                    1.7

                    1.7                                           

                    

                        org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor

                    

                    

                        ../your_project_name/src/main/java/

                    

                

            

        

    


The artifact is needed for the @Entity classes analysis and static Metamodel generation.
The maven-clean section is to clean the previous metamodel files (pattern: *_.java ) before generation (useful since the metamodel generation raise an error if any previous file already exist).
The maven-compileer section refer to the class with the metamodel generation class and set the target directory as the source directory (not the target!).

Next step is to define the eclipse maven build.
You can follow the below screenshots (sample build for project called "management"):


Configure the metamodel build:
Configure the project build:

  • For every "regular" build simply run the project build.
  • For any change in the @Entity files, run the compile metamodel build.

That's it..

Important note:
If you know how to run the two builds automatically one after the other, kindly leave a comment.

Thursday, December 29, 2011

Unit Test on Spring via Maven - configuration tips



 This post is for newbies who set their project to run Spring powered Unit Tests via Maven.

There are many blogs who mention how to do it.
I would recommend to install STS IDE and create new Spring Template project.
It usually comes with built in Test package. The test package can be executed via JUnit.

The tricky part comes when you want to run the tests via Maven and set the Spring configuration xml file in your custom folder.

Using default setting, Maven might raise an exception complaining the Spring xml configuration file could not be found or Tests could not be found.

So, my two cents are:
cent 1: Make sure your test classes ends with Test (and not ending with Tests, for example, as it comes with the Spring default project)
cent 2: configuration can be resolved while defining the spring configuration path.
 The trick is to use the relative location as it is in the source folder, and not in the target compilation folder. Also note to use backslash as folder separation and not dots.

Configuration using classpath relative path:

@ContextConfiguration (value = "classpath:/com/company/app/OrderPersistenceTests-context.xml")

 @RunWith(SpringJUnit4ClassRunner.class)

public class OrderPersistenceTest {

...

}

or

Configuration using project relative path:

@ContextConfiguration (value = "file:src/test/resources/com/company/app/OrderPersistenceTests-context.xml ") 
@RunWith(SpringJUnit4ClassRunner.class)
public class OrderPersistenceTest {



...

} 


Run tests using maven:
If pom.xml has the skipTest tag with true as value, e.g.:

       

            

                org.apache.maven.plugins

                maven-surefire-plugin

                2.6

                 

                1.5

                1.7

               true

            

            

...


...

Then no test will be executed, even when explicitly executing maven with test goal


Good luck !