title: WildFly and resource exclusion
body: <p>I have a wildfly war application at hands, which depends on a jar which starts its own JPA persistence container with Spring. This jar has its own <code>persistence.xml</code>, with  only one persistence unit.
  In the embedded jar, since there is only one persistence unit, it is injected using a <code>@PersistenceContext</code> without a unit name. Furthermore, the objects having such fields are not EJBs, they are simple Spring beans...</p>
  
  <p>On the other hand, the application has also a <code>persistence.xml</code> with two persistence units.
  The code of the application has <code>@PersistenceContext</code> fields with a proper <code>unitName</code>, which is mandatory since there are two of them.</p>
  
  <p>Now, when I start my application, Wildfly (specifically the jpa module) scans the code from the jar and chokes on the <code>@PersistenceContext</code> without unitNames of my Spring beans:</p>
  
  <pre><code>DEBUG [org.jboss.as.jpa.messages] (MSC service thread 1-1) persistence unit search for unitName=null referenced from class=the.spring.Bean (annotation=@javax.persistence.PersistenceContext() on void the.spring.Bean.setEntityManager(javax.persistence.EntityManager))
  </code></pre>
  
  <p>To cope with this, I would like to exclude the jar: In TomEE there is a mean to exclude a jar explicitly from being scanned, but I cannot find a similar functionnality in WildFly.
  I have tried the following configurations without success:</p>
  
  <ul>
  <li><p><code>jboss-deployment-structure.xml</code> which is read but without noticeable effect:</p>
  
  <pre><code>&lt;jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2"&gt;
    &lt;deployment&gt;
      &lt;exclusions&gt;
        &lt;module name="deployment.jee-app.war.spring-app.jar" /&gt;
        &lt;!-- I have also tried the following --&gt;
        &lt;!-- &lt;module name="jee-app.war.spring-app.jar" /&gt; --&gt;
        &lt;!-- &lt;module name="spring-app.jar" /&gt; --&gt;
      &lt;/exclusions&gt;
    &lt;/deployment&gt;
  &lt;/jboss-deployment-structure&gt; 
  </code></pre></li>
  <li><p><code>jboss-scanning.xml</code> which seems to be ignored</p>
  
  <pre><code>&lt;scanning xmlns="urn:jboss:scanning:1.0"&gt;
    &lt;path name="jee-app.war/WEB-INF/lib"&gt;
      &lt;exclude name="spring-app.jar" /&gt;
    &lt;/path&gt;
  &lt;/scanning&gt;
  </code></pre></li>
  </ul>
  
  <p>Both of these files are in the WEB-INF folder of my war application.</p>
  
  <pre><code>jee-app.war
    ├── WEB-INF
    │   ├── classes
    |   │   ├── META-INF
    |   │   |    └── persistence.xml _____________ # 2 PUs here
    |   |   └── &lt; lots of packages &gt;
    |   ├── lib
    |   |   ├── spring-app.jar ___________________ # to EXCLUDE from scanning !!!
    |   |   |   ├── META-INF
    |   |   |   |   └── persistence
    |   |   |   |       └── persistence.xml ______ # 1 PU here, loaded by Spring
    |   |   |   ├── application.xml ______________ # Spring definition
    |   |   |   └── the
    |   |   |       └── spring
    |   |   |           └── Bean.class
    |   |   └── &lt; lots of other jars &gt; 
    |   ├── jboss-deployment-structure.xml
    |   ├── jboss-scanning.xml
    |   └── web.xml
    └── &lt; some JSPs &gt;
  </code></pre>
  
  <p>Of course, I cannot modify the spring-app.jar (would be too easy...)</p>
  
  <p>Needless to say, I have spend a considerable amount of time in the WildFly doc and on Google without finding anything else than exluding subsystems, or overriding locally modules of WildFly...</p>
  
  <p>Thanks for your help!</p>
  
  <p>Juan Manuel</p>
  
  <p><strong>EDIT: As per request, here are the persistence.xml files</strong></p>
  
  <p>The one in the jee-app.war:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
  
    &lt;persistence-unit name="PU1" transaction-type="JTA"&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;jta-data-source&gt;java:jboss/datasources/DS1&lt;/jta-data-source&gt;
      &lt;class&gt;...&lt;/class&gt; &lt;!-- a lot of classes included by name !--&gt;
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
        &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /&gt;
        &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
        &lt;property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" /&gt;
        &lt;property name="hibernate.show_sql" value="true" /&gt;
        &lt;property name="hibernate.format_sql" value="false" /&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  
    &lt;persistence-unit name="PU2" transaction-type="JTA"&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;jta-data-source&gt;java:jboss/datasources/DS2&lt;/jta-data-source&gt;
      &lt;class&gt;...&lt;/class&gt; &lt;!-- a lot of classes included by name !--&gt;
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
        &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /&gt;
        &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
        &lt;property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" /&gt;
        &lt;property name="hibernate.show_sql" value="true" /&gt;
        &lt;property name="hibernate.format_sql" value="false" /&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>The one in the spring-app.jar</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;
    &lt;persistence-unit name="xa-rm"&gt;
  
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;mapping-file&gt;META-INF/persistence/orm.xml&lt;/mapping-file&gt;
  
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
  
    &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>