java - Maven 安装成功,但 jar 无法从 bash (CLI) 正常工作

标签 java eclipse bash maven

目前我正在公司开展一个项目,该项目将 XML 数据导入到我们的数据库中。在执行此操作时,我依赖于一些已在其他项目中创建和使用的基本配置项目,即用于创建与 Oracle 数据库的连接的 EntityManagerBuilder 或其他实用程序类。在我看来,这些依赖关系给我带来了一些问题。

如果我在 Eclipse 中启动我的项目,它运行得非常好。当我使用 mvn clean install -DskipTests 创建项目时,它构建得很好。 但是,当我想从命令行运行它时,应用程序启动,几行代码后就停止,不会抛出任何错误或异常。

我认为它与某些依赖项有关的原因是通过日志记录我设法找到了应用程序停止的点。由于它停在我可以调查的地方,我就这么做了。我下载了源代码,只添加了一些日志记录,突然我的应用程序对该类完全没有任何问题,而是在下一个对其他类的静态调用时停止了。

我根本不知道在哪里寻找错误。由于这是一个必须作为每月任务自行运行的应用程序,因此不能从 eclipse 执行它。

希望有人能给我提示如何解决这个问题。

这是我的 POM:

<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>
<parent>
    <groupId>com.company.infrastructure.foo</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>foo-import</artifactId>
<packaging>jar</packaging>
<name>FooImport</name>

<properties>
    <company.consoleapp.main.class>com.company.infrastructure.foo.import.FooImporter</company.consoleapp.main.class>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.company.maven</groupId>
            <artifactId>company-standalone-dm</artifactId>
            <version>${company.parent.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <!-- Logging -->

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>

    <!-- Utilities -->

    <dependency>
        <groupId>args4j</groupId>
        <artifactId>args4j</artifactId>
    </dependency>

    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.11</version>
    </dependency>   

    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.2.11</version>
    </dependency>

    <!-- Using foo-dataaccess -->

    <dependency>
        <groupId>com.company.infrastructure.marken</groupId>
        <artifactId>foo-dataaccess</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>

</dependencies>

<build>
    <finalName>foo-import</finalName>
    <plugins>

        <!-- Consoleapp-Mixin -->

        <plugin>
            <groupId>com.github.odavid.maven.plugins</groupId>
            <artifactId>mixin-maven-plugin</artifactId>
            <configuration>
                <mixins>
                    <mixin>
                        <groupId>com.company.maven</groupId>
                        <artifactId>company-consoleapp-mixin</artifactId>
                        <version>${company.parent.version}</version>
                    </mixin>
                </mixins>
            </configuration>
        </plugin>

    </plugins>

</build>

最佳答案

我认为这可能与您在 Maven 中的依赖管理有关,因为您没有指定版本,而 Maven 会自动计算出这些版本。

但是,当您运行应用程序时,您需要将这些 jar 放在您的类路径中,否则您可能最终会收到 ClassNotFoundException,因为这些 jar 不可用。因此,除非您弄清楚您提到的依赖项并将它们添加到您的类路径中,否则您最终会看到错误。

可能值得在您的应用程序中启用更高级别的日志记录,以便为您提供错误所在的一些指示。您还可以尝试看看在失败时是否引用了外部库,该库在您的类路径中不可用。

您能否也分享一下您如何通过 CLI 运行您的应用程序。

关于java - Maven 安装成功,但 jar 无法从 bash (CLI) 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38186671/

相关文章:

eclipse - Intellij 团队同步视角?

java - 我尝试在 Ubuntu 20.04 中的 eclipse 中创建一个新项目,但它给了我这个错误 Errors generated during the build

Linux Shell 脚本 : if/else works on bash but not zsh or sh

Java赋值运算符

java - 如何将对象数组传递给方法

java - 如何在java中读取这个json数组

python - 为什么 lxc 在 lxc-webpanel 中可以按预期运行,而不是在 CLI 或 python 中运行?

linux - 如何在linux shell脚本输出中缩短文件路径

java - 操作两个流后无法将结果保存到 List<Long>

java - 我可以使用 fastxml 将日期写入 ISODate 以使用即将到期的 mongodb 索引吗?