Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 234
Marceau Avatar asked Mar 15 '13 16:03

Marceau


People also ask

What is groupId and artifactId in Maven dependency?

The groupId is a parameter indicating the group or individual that created a project, which is often a reversed company domain name. The artifactId is the base package name used in the project, and we use the standard archetype.

What is groupId and artifactId while creating Maven project?

Maven uses a set of identifiers, also called coordinates, to uniquely identify a project and specify how the project artifact should be packaged: groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project.

What is groupId in pom xml?

Definition. The groupId is an XML element in the POM. XML file of a Maven project that specifies the id of the project group. In contrast, artifactId is an XML element in the POM. XML of a Maven project that specifies the id of the project (artifact).

What is the purpose of the persistence XML file?

The persistence. xml configuration file is used to configure a given JPA Persistence Unit. The Persistence Unit defines all the metadata required to bootstrap an EntityManagerFactory , like entity mappings, data source, and transaction settings, as well as JPA provider configuration properties.


1 Answers

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>
like image 86
unziberla Avatar answered Nov 07 '22 07:11

unziberla