简体   繁体   English

为使用spring数据jpa和maven的项目创建一个可执行jar

[英]Create an executable jar for a project that uses spring data jpa and maven

I am trying to execute a jar using java -jar <myJarName>我正在尝试使用java -jar <myJarName>执行一个 jar

I tried multiple ways of creating this jar with dependencies我尝试了多种方法来创建这个带有依赖项的 jar

  • First way第一种方式

Added the "maven dependency" and the "maven jar" plugins to pom file在 pom 文件中添加了“maven 依赖”和“maven jar”插件

<build>
    <plugins>

        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <useDefaultManifestFile>true</useDefaultManifestFile>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>fully.qualified.MainClass</mainClass>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>


    </plugins>
</build>

What was happening here is the the manifest contained all the class paths, but no dependencies got copied over这里发生的是清单包含所有类路径,但没有复制任何依赖项

  • Second way第二种方式

Tried the "maven assembly plugin"尝试了“Maven 程序集插件”

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>fully.qualified.MainClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

A second jar ending in jar-with-dependencies got created that had everything I needed.创建了第二个以 jar-with-dependencies 结尾的 jar,其中包含我需要的一切。 But calling the jar fails with the error但是调用 jar 失败并出现错误

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)

I verified that META-INF/Persistence.xml exists我验证了 META-INF/Persistence.xml 存在

  • Third way第三种方式

Tried the maven-shade-plugin尝试了 maven-shade-plugin

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>shade</goal></goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>fully.qualified.MainClass</mainClass>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

This one too copied all the dependencies, but it fails with an error这个也复制了所有的依赖项,但失败并出现错误

[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}

Just use SpringBoot.只需使用 SpringBoot。 It's meant for generating a fat executable jar.它用于生成一个胖可执行 jar。 You can use https://start.spring.io/ to create a SpringBoot project for Maven you can import your code into.您可以使用https://start.spring.io/为 Maven 创建一个 SpringBoot 项目,您可以将代码导入其中。 As far as the conflicts go use the Spring BOM for Maven like this: https://www.baeldung.com/spring-maven-bom就冲突而言,使用 Spring BOM for Maven,如下所示: https : //www.baeldung.com/spring-maven-bom

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.3.8.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

It will ensure all your versions are consistent.它将确保您的所有版本都是一致的。

I went with the second option as well and had a similar problem.我也选择了第二个选项,并遇到了类似的问题。 The issue you are running into is that you are combining multiple spring dependencies, which provide each definitions for factories, handlers, namespaces and such an you run into a conflict.您遇到的问题是您正在组合多个 spring 依赖项,这些依赖项提供了工厂、处理程序、命名空间的每个定义,并且您遇到了冲突。

The way around this is to check all the spring dependencies for such definition and add them in your project by combining them.解决此问题的方法是检查此类定义的所有 spring 依赖项,并通过组合它们将它们添加到您的项目中。 For more details you can take a look at this blog entry or at this maven module where I combined the configuration.有关更多详细信息,您可以查看此博客条目或我组合配置的此 maven 模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM