【问题标题】:java executing maven project outside IDE [duplicate]java在IDE之外执行maven项目[重复]
【发布时间】:2016-11-03 19:58:22
【问题描述】:

我可能完全误解了 Maven 的工作原理,但我有以下问题。 我在 netbeans 中使用 maven 创建了一个应用程序。它在 netbeans 中运行良好,但是当我进行 Clean and Build 时,它只是编译我的源文件,并且 maven 依赖项不在 jar 文件中,因此无法在命令窗口中运行。

这是我的 pom 文件。它有构建部分,依赖项在 mainfest.mf 的类路径中

<?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>com.ibh</groupId>
<artifactId>SafePassword</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.ibh.safepassword.TestMain</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.192</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>4.2.0.Final</version>
        <type>jar</type>
    </dependency>
</dependencies>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

我来自 c# 世界,认为 maven 与仅在 DEV 阶段玩游戏的 NuGet 相似,但在编译时一切都在那里......

缺少什么?如何执行 jar 文件以使其他依赖项到位,以便应用程序可以运行?

感谢您的提示!

编辑

我不同意这个问题与您所指的问题完全重复。 因为我可以阅读这些许多解决方案,所以 maven 非常复杂,每个问题都没有一个解决方案

【问题讨论】:

  • 解压 jar。依赖项应该在里面。您可以使用 java -jar &lt;jarname&gt; 运行 jar
  • Sean Patrick Floyd:我检查了 jar 的内容,那里没有依赖项。在命令窗口中执行 jar 时也会得到 NoClassDefFoundError

标签: java maven netbeans


【解决方案1】:

默认情况下,Maven 构建一个基本上是一个库的 jar - 它不包含其依赖项。如果您想包含依赖项,制作可运行的应用程序,您有两个主要选择:

  1. 使用允许的插件将所有内容捆绑在一个 jar 中,例如 maven-shade-plugin。然后你可以运行这个 jar。
  2. 使用您的 jar 和所有依赖项创建分发包:
    1. 使用设计为分发的包装,例如 warear - 如果其中一个符合您的意图
    2. 否则使用通用捆绑插件,例如标准maven-assembly-plugin或更高级的appassembler-maven-plugin

【讨论】:

  • 当然,你仍然需要有主类和类路径的清单文件,但你已经正确地做到了。
猜你喜欢
  • 1970-01-01
  • 2020-03-04
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
相关资源
最近更新 更多