title: Create an executable jar for a project that uses spring data jpa and maven
body: <p>I am trying to execute a jar using <code>java -jar &lt;myJarName&gt;</code></p>
  
  <p>I tried multiple ways of creating this jar with dependencies</p>
  
  <ul>
  <li>First way</li>
  </ul>
  
  <p>Added the "maven dependency" and the "maven jar" plugins to pom file </p>
  
  <pre><code>&lt;build&gt;
      &lt;plugins&gt;
  
          &lt;plugin&gt;
              &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;phase&gt;install&lt;/phase&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;copy-dependencies&lt;/goal&gt;
                      &lt;/goals&gt;
                      &lt;configuration&gt;
                          &lt;outputDirectory&gt;${project.build.directory}/lib&lt;/outputDirectory&gt;
                      &lt;/configuration&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
  
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
              &lt;configuration&gt;
                  &lt;useDefaultManifestFile&gt;true&lt;/useDefaultManifestFile&gt;
                  &lt;archive&gt;
                      &lt;manifest&gt;
                          &lt;addClasspath&gt;true&lt;/addClasspath&gt;
                          &lt;mainClass&gt;fully.qualified.MainClass&lt;/mainClass&gt;
                          &lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
                      &lt;/manifest&gt;
                  &lt;/archive&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
  
  
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p>What was happening here is the the manifest contained all the class paths, but no dependencies got copied over</p>
  
  <ul>
  <li>Second way</li>
  </ul>
  
  <p>Tried the "maven assembly plugin"</p>
  
  <pre><code>&lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
              &lt;configuration&gt;
                  &lt;archive&gt;
                      &lt;manifest&gt;
                          &lt;mainClass&gt;fully.qualified.MainClass&lt;/mainClass&gt;
                      &lt;/manifest&gt;
                  &lt;/archive&gt;
                  &lt;descriptorRefs&gt;
                      &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                  &lt;/descriptorRefs&gt;
              &lt;/configuration&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;id&gt;make-assembly&lt;/id&gt; &lt;!-- this is used for inheritance merges --&gt;
                      &lt;phase&gt;package&lt;/phase&gt; &lt;!-- bind to the packaging phase --&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;single&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p>A second jar ending in jar-with-dependencies got created that had everything I needed. But calling the jar fails with the error</p>
  
  <pre><code>Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa]
  Offending resource: class path resource [META-INF/spring/app-context.xml]
  
          at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
          at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
  </code></pre>
  
  <p>I verified that META-INF/Persistence.xml exists</p>
  
  <ul>
  <li>Third way</li>
  </ul>
  
  <p>Tried the maven-shade-plugin</p>
  
  <pre><code>&lt;build&gt;
      &lt;plugins&gt;
  
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.1&lt;/version&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;phase&gt;package&lt;/phase&gt;
                      &lt;goals&gt;&lt;goal&gt;shade&lt;/goal&gt;&lt;/goals&gt;
                      &lt;configuration&gt;
                          &lt;transformers&gt;
                              &lt;transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&gt;
                                  &lt;mainClass&gt;fully.qualified.MainClass&lt;/mainClass&gt;
                              &lt;/transformer&gt;
                              &lt;transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt;
                                  &lt;resource&gt;META-INF/spring.handlers&lt;/resource&gt;
                              &lt;/transformer&gt;
                              &lt;transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt;
                                  &lt;resource&gt;META-INF/spring.schemas&lt;/resource&gt;
                              &lt;/transformer&gt;
                          &lt;/transformers&gt;
                      &lt;/configuration&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p>This one too copied all the dependencies, but it fails with an error</p>
  
  <pre><code>[main] INFO org.springframework.jdbc.datasource.DriverManagerDataSource - Loaded JDBC driver: org.h2.Driver
  1022 [main] WARN org.springframework.context.support.ClassPathXmlApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.vmware.vra.performance.loganalyzer.Repository.JpaConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
  Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.vmware.vra.performance.loganalyzer.Repository.JpaConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
  </code></pre>