【问题标题】:Create an executable jar for a project that uses spring data jpa and maven为使用spring data jpa和maven的项目创建可执行jar
【发布时间】:2016-06-10 10:50:11
【问题描述】:

我正在尝试使用 java -jar <myJarName> 执行一个 jar

我尝试了多种方法来创建这个带有依赖项的 jar

  • 第一种方式

在 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>

这里发生的是包含所有类路径的清单,但没有复制任何依赖项

  • 第二种方式

试过“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>

创建了第二个以 jar-with-dependencies 结尾的 jar,其中包含我需要的一切。但是调用 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)

我已验证 META-INF/Persistence.xml 存在

  • 第三条路

尝试了 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>

这个也复制了所有的依赖,但是失败并报错

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

【问题讨论】:

  • 你是怎么解决这个问题的?我对第二个选项有同样的问题。

标签: java spring jpa jar maven-3


【解决方案1】:

只需使用 SpringBoot。它用于生成胖可执行jar。您可以使用https://start.spring.io/ 为 Maven 创建一个 SpringBoot 项目,您可以将代码导入其中。就冲突而言,像这样使用 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>

它将确保您的所有版本都是一致的。

【讨论】:

    【解决方案2】:

    我也选择了第二个选项并且遇到了类似的问题。您遇到的问题是您正在组合多个 spring 依赖项,它们为工厂、处理程序、命名空间等提供每个定义,这样您就会遇到冲突。

    解决这个问题的方法是检查所有 spring 依赖项以获取此类定义,并通过组合将它们添加到您的项目中。有关更多详细信息,您可以查看此 blog entrythis maven module 我在其中组合了配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-23
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 2021-09-12
      • 1970-01-01
      相关资源
      最近更新 更多