Referring to the artifactId of a maven dependency jar from persistence.xml

hibernate, jpa, maven, pom.xml

Solution

I have solved it this way:

in `persistence.xml` substitute the whole `<jar-file>` tag, i.e.

<persistence-unit name="PersistenceUnit">
    ...
    <!-- here is where the jar file is supposed to go -->
    ${importjarfile}
    ...
</persistence-unit>

Finally, set your properties like this:

<properties>
    <x-version>4.0</x-version>
    <importjarfile><![CDATA[<jar-file>lib/X-v5-${x-version}.jar</jar-file>]]></importjarfile>
</properties>

In this way Eclipse won't see the `jar-file` tag and won't complain.

My solution was actually a little bit cleaner since I was using a `.properties` file so I didn't have to use CDATA, I hope it still works.

My solution:

importjarfile = <jar-file>lib/X-v5-${x-version}.jar</jar-file>

Problem

We're working with JPA and are trying to stick to standard spec (and avoiding Hibernate-specific features). We use one project (let's call it X) inside another project (A) as a Maven dependency. We need JPA to scan project X for Entities as well as scanning project A. To that end, we've added a line ``` <jar-file>lib/X-v5-4.0.jar</jar-file> ``` inside ``` <persistence-unit> ``` in persistence.xml. This works fine. The issue we still have is that we now need to specify the version of project X in not only pom.xml but also in persistence.xml. This is a recipe for problems with deploys in the future. We've come up with a system using Maven resource filtering: ``` <jar-file>lib/X-v5-${x-version}.jar</jar-file> ``` in persistence.xml and ``` <properties> <x-version>4.0</x-version> </properties> ``` and ${x-version} in pom.xml. This works but is still not perfect, as we'll have to remember to update the version number in a non-standard location each time project X gets a new version. Ideally, we'd like to have a situation where we can adjust version information in the dependency part of pom.xml and changes would propagate to persistence.xml automatically. We'd reduce a lot of possible errors in future deploys this way. Is this possible? EDIT (OUR SOLUTION): we have added a file named jpa.xml. We define an entityManagerFactory, a persistenceAnnotationBeanPostProcessor and a transactionManager in it. The important part here is the entityManagerFactory bean. It has a property "packagesToScan" that allows you to indicate specific packages to scan for entities to put in the persistence context. A code snippet: ``` <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="jpaDataSource" /> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> </property> <property name="packagesToScan"> <list> <value>org.com.our.external.library.package1</value> <value>org.com.our.external.library.package2</value> <value>org.com.our.external.library.package3</value> </list> </property> </bean> ``` I'm sure you see the advantage: as we are referring to these libraries by package signature, we no longer have to worry about jar version numbers.

Original source