使用 IntelliJ 制作 OrientDB 可执行 jar

Making OrientDB executable jar with IntelliJ

我用 ide intellij 创建了一个 java 应用程序,其中包括使用 Maven 的几个依赖项(特别是 OrientDb 和 Bouncy Castle)。

该应用程序在 ide 上运行良好,但我需要在另一台计算机上使用 ssh 运行 它。 因此,我使用 Maven Assembly Plugin ( http://maven.apache.org/plugins/maven-assembly-plugin/usage.html ) 制作了一个可执行 jar。 但是当我 运行 我得到这个错误:

$ java -jar 1-1.0-SNAPSHOT-jar-with-dependencies.jar 
apr 13, 2016 5:55:28 PM com.orientechnologies.common.log.OLogManager log
INFORMAZIONI: OrientDB auto-config DISKCACHE=4.075MB (heap=1.751MB os=7.874MB disk=39.975MB)
Exception in thread "main" com.orientechnologies.common.exception.OException: Error on creation of shared resource
    at com.orientechnologies.common.concur.resource.OSharedContainerImpl.getResource(OSharedContainerImpl.java:66)
    at com.orientechnologies.orient.core.storage.OStorageAbstract.getResource(OStorageAbstract.java:143)
    at com.orientechnologies.orient.core.metadata.OMetadataDefault.init(OMetadataDefault.java:196)
    at com.orientechnologies.orient.core.metadata.OMetadataDefault.load(OMetadataDefault.java:76)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.initAtFirstOpen(ODatabaseDocumentTx.java:2901)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.open(ODatabaseDocumentTx.java:259)
    at bcvis.Main.main(Main.java:26)
Caused by: com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException: Cannot find a command executor for the command request: sql.select from OFunction order by name
    at com.orientechnologies.orient.core.sql.OCommandExecutorSQLDelegate.parse(OCommandExecutorSQLDelegate.java:72)
    at com.orientechnologies.orient.core.sql.OCommandExecutorSQLDelegate.parse(OCommandExecutorSQLDelegate.java:42)
    at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.command(OAbstractPaginatedStorage.java:1400)
    at com.orientechnologies.orient.core.sql.query.OSQLQuery.run(OSQLQuery.java:72)
    at com.orientechnologies.orient.core.sql.query.OSQLSynchQuery.run(OSQLSynchQuery.java:85)
    at com.orientechnologies.orient.core.query.OQueryAbstract.execute(OQueryAbstract.java:33)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.query(ODatabaseDocumentTx.java:714)
    at com.orientechnologies.orient.core.metadata.function.OFunctionLibraryImpl.load(OFunctionLibraryImpl.java:65)
    at com.orientechnologies.orient.core.metadata.OMetadataDefault.call(OMetadataDefault.java:201)
    at com.orientechnologies.orient.core.metadata.OMetadataDefault.call(OMetadataDefault.java:197)
    at com.orientechnologies.common.concur.resource.OSharedContainerImpl.getResource(OSharedContainerImpl.java:64)
    ... 6 more

我该怎么做才能解决这个问题?我也对其他构建解决方案持开放态度,但我对 Java / Maven 不是很有经验。

这是我的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>1</groupId>
    <artifactId>1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.orientechnologies</groupId>
            <artifactId>orientdb-graphdb</artifactId>
            <version>2.1.15</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk16</artifactId>
            <version>1.45</version>
        </dependency>
    </dependencies>
    <build>
    <plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
                <manifest>
                    <mainClass>bcvis.Main</mainClass>
                </manifest>
            </archive>
        </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>
</project>

编辑:

就像@wolf4ood 建议的那样,我尝试在这个 POM 中使用 maven shade 插件

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>1</groupId>
    <artifactId>1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.orientechnologies</groupId>
            <artifactId>orientdb-graphdb</artifactId>
            <version>2.1.15</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk16</artifactId>
            <version>1.45</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>

                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer"/>

                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>bcvis.Main</mainClass>
                                </transformer>


                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project> 

我放置了那个过滤器来修复这个错误:

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

但我得到的错误与以前完全相同:

$ java -jar 1-1.0-SNAPSHOT.jar 
apr 13, 2016 9:59:16 PM com.orientechnologies.common.log.OLogManager log
INFORMAZIONI: OrientDB auto-config DISKCACHE=4.075MB (heap=1.751MB os=7.874MB disk=40.019MB)
Exception in thread "main" com.orientechnologies.common.exception.OException: Error on creation of shared resource
    at com.orientechnologies.common.concur.resource.OSharedContainerImpl.getResource(OSharedContainerImpl.java:66)
    at com.orientechnologies.orient.core.storage.OStorageAbstract.getResource(OStorageAbstract.java:143)
    at com.orientechnologies.orient.core.metadata.OMetadataDefault.init(OMetadataDefault.java:196)
    at com.orientechnologies.orient.core.metadata.OMetadataDefault.load(OMetadataDefault.java:76)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.initAtFirstOpen(ODatabaseDocumentTx.java:2901)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.open(ODatabaseDocumentTx.java:259)
    at bcvis.Main.main(Main.java:26)
Caused by: com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException: Cannot find a command executor for the command request: sql.select from OFunction order by name
    at com.orientechnologies.orient.core.sql.OCommandExecutorSQLDelegate.parse(OCommandExecutorSQLDelegate.java:72)
    at com.orientechnologies.orient.core.sql.OCommandExecutorSQLDelegate.parse(OCommandExecutorSQLDelegate.java:42)
    at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.command(OAbstractPaginatedStorage.java:1400)
    at com.orientechnologies.orient.core.sql.query.OSQLQuery.run(OSQLQuery.java:72)
    at com.orientechnologies.orient.core.sql.query.OSQLSynchQuery.run(OSQLSynchQuery.java:85)
    at com.orientechnologies.orient.core.query.OQueryAbstract.execute(OQueryAbstract.java:33)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.query(ODatabaseDocumentTx.java:714)
    at com.orientechnologies.orient.core.metadata.function.OFunctionLibraryImpl.load(OFunctionLibraryImpl.java:65)
    at com.orientechnologies.orient.core.metadata.OMetadataDefault.call(OMetadataDefault.java:201)
    at com.orientechnologies.orient.core.metadata.OMetadataDefault.call(OMetadataDefault.java:197)
    at com.orientechnologies.common.concur.resource.OSharedContainerImpl.getResource(OSharedContainerImpl.java:64)
    ... 6 more

问题是 META-INF/services 中的 JAVA 服务。它们不会合并到您的可执行文件中。所以有些执行者没有正确注册。您应该使用 maven shade 插件来构建 jar 并使用资源转换器

https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html

我找到了问题的解决方案。

事实是我只需要 orientdb-core 包而不是所有 orientdb-graph。将 Maven 依赖项更改为

<dependency>
            <groupId>com.orientechnologies</groupId>
            <artifactId>orientdb-core</artifactId>
            <version>2.1.15</version>
</dependency>

让奇怪的异常消失了!