maven shade illegalstateexception-缺少必需的类信息

pgccezyw  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(356)

我刚刚使用javafx和hibernate构建了一个应用程序,它在intellij上构建和运行得非常完美,所以我选择使用maven shade plugin生成一个“胖jar”,因为我使用了一些带有自动模块名的库,所以我不能使用jlink。
jar创建得很好,它正确地加载了初始屏幕,但是当它试图连接到我的数据库时,特别是这一行是有问题的(jpautil)。java:19);

factory = Persistence.createEntityManagerFactory(persistenceUnit, properties);

我得到以下例外情况;

java.lang.IllegalStateException: Required class information is missing
    at org.jboss.jandex.Indexer.rebuildNestedType(Indexer.java:926)
    at org.jboss.jandex.Indexer.resolveTypePath(Indexer.java:786)
    at org.jboss.jandex.Indexer.resolveTypeAnnotation(Indexer.java:705)
    at org.jboss.jandex.Indexer.resolveTypeAnnotations(Indexer.java:613)
    at org.jboss.jandex.Indexer.index(Indexer.java:1602)
    at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.toClassDescriptor(ClassFileArchiveEntryHandler.java:64)
    at org.hibernate.boot.archive.scan.spi.ClassFileArchiveEntryHandler.handleEntry(ClassFileArchiveEntryHandler.java:52)
    at org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor.visitArchive(JarFileBasedArchiveDescriptor.java:147)
    at org.hibernate.boot.archive.scan.spi.AbstractScannerImpl.scan(AbstractScannerImpl.java:48)
    at org.hibernate.boot.model.process.internal.ScanningCoordinator.coordinateScan(ScanningCoordinator.java:76)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.prepare(MetadataBuildingProcess.java:98)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:254)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:175)
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:76)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:171)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:119)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:61)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:50)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at com.it.util.JPAUtil.getEntityManagerFactory(JPAUtil.java:19)
    at com.it.controller.HomeController.initializeSettings(HomeController.java:1571)
    at com.it.controller.HomeController.initialize(HomeController.java:1407)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at com.it.Home.start(Home.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)

我知道语法是正确的,正如我之前提到的,它在ide上正确运行,所以我怀疑shade生成的jar文件中缺少了一些东西,但是我已经花了将近3天的时间在这上面了,我根本找不到原因。stacktrace上显示的信息没有多大帮助,至少我没有看到任何东西-我甚至查看了stacktrace上提到的所有类,它们似乎都在生成的jar上。希望这里的任何人都有类似的东西,或者在maven shade过程中可能比我更熟悉。任何帮助或点看将不胜感激。
有助于确定问题的其他文件如下:;
persistence.xml—其他属性是从代码上的xml文件加载的。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">

    <persistence-unit name="Project-Postgre">
        <description>Hibernate JPA Configuration</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>

            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value = "false" />
        </properties>
    </persistence-unit>
</persistence>

pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.it</groupId>
    <artifactId>Project</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.release>11</maven.compiler.release>
        <javafx.version>14</javafx.version>
    </properties>

    <name>Project</name>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/de.jensd/fontawesomefx -->
        <dependency>
            <groupId>de.jensd</groupId>
            <artifactId>fontawesomefx</artifactId>
            <version>8.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.20.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc10 -->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc10</artifactId>
            <version>19.7.0.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>${maven.compiler.release}</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.3</version>
                <configuration>
                    <mainClass>com.it.HomeFX</mainClass>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>project-classifier</shadedClassifierName>
                            <outputFile>target\shade\${project.artifactId}.jar</outputFile>
                            <transformers>
                                <transformer implementation=
                                                     "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.it.HomeFX</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

jpautil公司

public static EntityManagerFactory getEntityManagerFactory(String persistenceUnit, String connectionURL, String user, String password) {
        if (factory == null) {
            Map<String, String> properties = new HashMap<>();
            properties.put("javax.persistence.jdbc.url", connectionURL);
            properties.put("javax.persistence.jdbc.user", user);
            properties.put("javax.persistence.jdbc.password", password);
            factory = Persistence.createEntityManagerFactory(persistenceUnit, properties);
        }
        return factory;
}
nkoocmlb

nkoocmlb1#

我也有同样的问题。我试图将postgresqljdbc驱动程序版本降级到42.0.0。例外情况消失,一切正常。

zdwk9cvp

zdwk9cvp2#

我在使用spring时也遇到了同样的错误。version:5.2.7.release spring.boot.version:2.3.1.发布postgresql-version:42.2.16
从42.2.15开始的所有版本的postgres都会发生这种情况。版本42.2.14(或更早)对我来说很好。

czfnxgou

czfnxgou3#

通过从hibernate切换到openjpa,我成功地克服了这个问题。hibernate没有提供足够的信息来调试这个问题,当切换到openjpa时,我不得不将这个添加到pom.xml中;

<dependency>
     <groupId>org.apache.openjpa</groupId>
     <artifactId>openjpa-all</artifactId>
     <version>3.1.2</version>
</dependency>

shade创建了包含所有必需库的jar,并成功连接到数据库。

相关问题