运行具有外部依赖项的可执行JAR [英] Running a executable JAR with external dependencies

查看:140
本文介绍了运行具有外部依赖项的可执行JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个有两个发行版的java服务。每个发行版必须构建不同(一个内部有一个spring-boot嵌入式jetty服务器,另一个没有)。在两者中,我创建了一个具有依赖关系的分布,除了一个(已经实现)。没有jetty的jar使用 maven-assembly-plugin 构建(类似于此处运行带有依赖项的可执行jar的问题)另一个使用 spring-boot-maven-plugin (参见 http://docs.spring.io/spring-boot/docs/电流/行家-插件/ usage.html中)。我的问题是外部依赖。

I'm building a java service which has two distribution. Each distribution must be build different (one has a spring-boot embedded jetty server inside, the other not). In both, I created a distribution with dependencies, except one (already achieved). The jar without jetty is build using maven-assembly-plugin (similarly as here Problems running executable jar with dependencies) the other one uses spring-boot-maven-plugin (see http://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html). My issue is the external dependencies.

我想在运行时上添加额外的依赖,但我不想要美国OSGi

I want add an additional dependency on runtime, but I DON'T WANT TO US OSGi

在两个构建过程中,结果都是一个可执行jar,我可以这样运行:

In both building process the result is a 'executable' jar, which I can run like this:

java -jar my.jar

但是我不知道如何解决其他依赖项。如果我在没有外部依赖项(比如external.jar)的情况下运行上面的命令,my.jar中的内容将会失败,即使它位于同一个文件夹中。我可以让它适用于没有码头的分发:

But I don't know how to address the additional dependencies. If I run the command above without a the external dependency (lets say external.jar) inside my.jar will fail, even if is in the same folder. I can make it work for the distribution without jetty like this:

java -classpath "./*" my.main.App

但这对my-with-jetty.jar不起作用。我也尝试运行:

But this doesn't work for my-with-jetty.jar. I also try to run:

java -classpath "./*" -jar my-with-jetty.jar

这根本不起作用。

所以我的问题是:

是否可以打包几乎所有依赖项的jar?

is it possible to pack a jar with almost all the dependencies for both cases?

是有可能将jar打包为runnable jar,这样就不必提供主类吗?

is it possible to pack the jar as runnable jar such that it not necessary to provide the main class?

当然,如果是,怎么样?我想以同样的方式运行两个发行版。

of course if yes, how? I would like to run both distribution in the same manner.

我想要类似的行为:

java -cp "./*" java -jar my.jar conf.cfg

java -cp "./*" java -jar my-rest.jar conf.cfg

谢谢。

推荐答案

您需要至少3个同一父项下的maven项目,以便它们一起构建并具有相同的版本。您的所有代码都将在共享项目中,这将包含在两个可运行的jar项目中。每个可运行的jar项目将以不同的方式构建。

You need at least 3 maven projects all under the same parent so they get built together and have the same version. All your code will be in the shared project, this will be included in both runnable jar projects. Each runnable jar project will be build differently.

您的父pom将类似于:

Your parent pom will be something like :

<?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>foo.bar</groupId>
  <artifactId>parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>shared-jar</module>
    <module>spring-boot-jar</module>
    <module>jetty-jar</module>
  </modules>

</project>

您的共享jar项目将拥有所有共享代码

Your shared-jar project will have all the shared code

你的spring-boot-jar看起来像:

Your spring-boot-jar will look like :

<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>foo.bar</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>spring-boot-jar</artifactId>
    <packaging>jar</packaging>

    <dependencies>
       <dependency>
          <groupId>foo.bar</groupId>
          <artifactId>shared-jar</artifactId>
          <version>${project.version}</version>
       </dependency>
        .... you will need to add all the spring boot dependencies with versions
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

您的码头将使用

    <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>foo.bar</groupId>
            <artifactId>parent</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <artifactId>jetty-jar</artifactId>
        <packaging>jar</packaging>

        <dependencies>
           <dependency>
              <groupId>foo.bar</groupId>
              <artifactId>shared-jar</artifactId>
              <version>${project.version}</version>
           </dependency>
            .... other dependencies
        </dependencies>

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <!-- <Main-Class>foo.bar.Application</Main-Class> -->
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    </project>

这篇关于运行具有外部依赖项的可执行JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆