maven-ear-plugin has a fileNameMapping property, with which you can specify file name mapping for all dependencies included in the EAR file. The following values โโare available: standard , no-version , full , no-version-for-ejb . standard means the file name is an artifact, including. version of the artifact. no-version means that files are only artifactId without version. full means that filename is an artifact of groupId + artifactId +. no-version-for-ejb means that filename is artifactId with no version in case of type EJB.
So, define the ear plugin as follows:
<plugin> <artifactId>maven-ear-plugin</artifactId> <version>2.10.1</version> <configuration> <fileNameMapping>no-version</fileNameMapping> </configuration> </plugin>
can do the trick for you.
Alternative 2: Externalize persistence.xml
An alternative approach might be to externalize persistence.xml . The easiest approach is to create a jar containing persistence.xml and place it in the ear lib folder:
EAR + |- lib + | |- core-module.jar | \- persistence-module.jar + | \- META-INF + | \- persistence.xml |- ejb1-module.jar \- ejb2-module.jar
EJB modules can be either jar archives or exploded directories. In the setup, as stated above, persistence.xml will look like this:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="my-persistence-unit"> <jar-file>../ejb1-module.jar</jar-file> <jar-file>../ejb2-module.jar</jar-file> .... </persistence-unit> </persistence>
A storage unit that is defined at the EAR level is usually visible for all application components. However, if a storage unit with the same name is defined by a JAR, WAR or application file in the EAR, the storage unit of this name defined at the EAR level will not be visible to the components defined by this EJB-JAR, WAR or application file, if the link per save unit does not use syntax syntax syntax to specify the path name to disambiguate the link. When the # syntax is used, the path name refers to the jar file of the referenced application file. For example, the syntax ../lib/persistenceUnitRoot.jar#myPersistenceUnit refers to a save unit whose name, as indicated in the name element of the persistence.xml file, is myPersistenceUnit and for which the relative path to the root of the save block is saved ../lib/persistenceUnitRoot.jar .
A save unit can be specified by annotating the entityManager with @PersistenceContext(unitName = "../lib/persistenceUnitRoot.jar#myPersistenceUnit") .