【问题标题】:mvn output jar file is executable only inside target foldermaven 输出 jar 文件只能在目标文件夹内执行
【发布时间】:2018-08-15 01:22:51
【问题描述】:

我有一个 mvn 构建的 jar,它在目标文件夹中运行良好。

但是如果我将 jar 复制到不同的文件夹/机器。我会得到一个找不到类的异常。我了解 jar 无法访问依赖项。我怎样才能解决这个问题? 我的 pom 包含,

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
            <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
            <goal>copy-dependencies</goal>
            </goals>
            <configuration>
            <configuration>
            <!-- exclude junit, we need runtime dependency only -->
            <includeScope>runtime</includeScope>
            <outputDirectory>${project.build.outputDirectory}/dependency/
            </outputDirectory>
            </configuration>
            </configuration>
            </execution>
            </executions>
            </plugin>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>

            <configuration>

            <archive>
            <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>com.Main</mainClass>

            <classpathPrefix>dependency/</classpathPrefix>

            </manifest>
            </archive>
            </configuration>
            </plugin>

提前致谢, 萨兰亚

【问题讨论】:

  • 我添加了一个可能的解决方案。请看看,如果有帮助。
  • You can check an example here 跳过 JavaFX 员工和其他人会工作。

标签: maven


【解决方案1】:

使用 maven-assembly-plugin 制作一个带有依赖项的胖 jar:

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <!-- get all project dependencies -->
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <!-- bind to the packaging phase -->
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
              </execution>
            </executions>
        </plugin>

目标文件夹会有额外的jar-with-dependencies.jar;你应该可以在任何地方使用它。

【讨论】:

  • 但我的输出 jar 必须是预定义的名称,并且不应包含 jar-with-dependencies.jar。此外,当我包含此插件时,它说没有主清单
  • 将descriptorRef 更改为您想要的任何内容。关键是 maven 将创建两个 jar - 一个不包含依赖项,另一个包含依赖项,并且您希望有一个单独的名称。无论如何,我不明白,从执行的角度来看,jar 名称的重要性。
  • 所以在我看到的任何地方,复制依赖插件也可以用来拥有一个可执行的 jar。但对我来说,只有当我指向目标文件夹时才能执行 jar。
  • 我尝试了 Maven 程序集插件,对您的示例稍作更改。但是有什么方法可以摆脱没有依赖关系的 jar 或更新其默认名称。具有依赖关系的 jar 在目标文件夹内外工作。所以从我的角度来看,没有依赖关系的 jar 是没有用的。
  • 依赖Jar只是某些情况下的一种方式,但不应该是制作jar的方式。想想一个有 1000 多个依赖 jar 的大型项目,任何俱乐部都在一起。您的 Jar 会变得很大,并失去其可移植性和 Java 平台的许多优点。理想情况下,您可以通过在运行时设置类路径来学习如何使用普通 jar。同时,我认为删除文件和重命名 jar 是不可能的,除非您使用其他插件。
【解决方案2】:

我从我的 pom.xml 中删除了 maven jar 和依赖插件。已添加

 <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.importer.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <finalName>xyz</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                </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>

这创建了一个可以在任何地方执行的具有依赖项的 jar。还带有特定的 jar 名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-25
    • 2012-02-18
    • 2012-09-04
    • 1970-01-01
    • 2015-08-14
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多