簡體   English   中英

運行具有外部依賴項的可執行JAR

[英]Running a executable JAR with external dependencies

我正在建立一個具有兩個分布的Java服務。 每個發行版都必須構建不同的版本(一個版本內部裝有spring-boot嵌入式碼頭服務器,另一個版本則沒有)。 在這兩種方法中,我都創建了一個具有依賴關系的發行版,除了一個(已經實現)。 沒有碼頭的罐子是使用maven-assembly-plugin構建的(類似於此處的運行具有依賴項的可執行jar的問題 ),另一個使用spring-boot-maven-plugin (請參閱http://docs.spring.io/spring-boot/ docs / current / maven-plugin / usage.html )。 我的問題是外部依賴關系。

我想在運行時添加其他依賴項,但我不想使用OSGi

在這兩個構建過程中,結果都是一個“可執行” jar,我可以像這樣運行:

java -jar my.jar

但我不知道該如何解決其他依賴性。 如果我運行上面的命令時沒有my.jar內的外部依賴項(例如external.jar),即使位於同一文件夾中也會失敗。 我可以這樣使它在沒有碼頭的情況下正常工作:

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

但這對於my-with-jetty.jar不起作用。 我也嘗試運行:

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

這根本行不通。

所以我的問題是:

兩種情況下幾乎都可以打包帶有所有依賴項的jar嗎?

是否可以將罐子包裝為可運行罐子,從而不必提供主類?

當然可以,怎么辦? 我想以相同的方式運行兩個發行版。

我想要類似的行為:

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

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

謝謝。

您至少需要3個maven項目都在同一父項下,以便它們一起構建並具有相同的版本。 您所有的代碼都將在共享項目中,這將同時包含在兩個可運行的jar項目中。 每個可運行的jar項目將以不同的方式構建。

您的父pom將類似於:

<?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項目將具有所有共享代碼

您的spring-boot-jar如下所示:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM