简体   繁体   English

Maven 可执行文件 JAR 与依赖项

[英]Maven Executable JAR with dependencies

I've got a mvn project that contains a dependency to apache commons-lang3.我有一个 mvn 项目,其中包含对 apache commons-lang3 的依赖项。 I managed to generate a manifest.mf for this jar file and it starts via java -jar.jar on my remote server.我设法为这个 jar 文件生成了 manifest.mf,它通过我的远程服务器上的 java -jar.jar 启动。 However, it does not include any dependencies and I can't figure out why.但是,它不包含任何依赖项,我不知道为什么。

This is the pom.xml located in /workspace/:这是位于 /workspace/ 中的 pom.xml:

<project>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.hohmannit.phdev.midi</groupId>
 <artifactId>releng</artifactId>
 <version>1.0.0-SNAPSHOT</version>
 <packaging>pom</packaging>

 <properties>
  <tycho.version>2.2.0</tycho.version>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <build>
  <pluginManagement>
     <plugins>
       <plugin>
         <groupId>org.eclipse.tycho</groupId>
         <artifactId>tycho-p2-director-plugin</artifactId>
         <version>${tycho.version}</version>
       </plugin>
     </plugins>
   </pluginManagement>
  <plugins>
   <plugin>
    <groupId>org.eclipse.tycho</groupId>
    <artifactId>tycho-maven-plugin</artifactId>
    <version>${tycho.version}</version>
    <extensions>true</extensions>
   </plugin>
   <!--Enable the replacement of the SNAPSHOT version in the final product configuration-->
   <plugin>
        <groupId>org.eclipse.tycho</groupId>
            <artifactId>tycho-packaging-plugin</artifactId>
             <version>${tycho.version}</version>
             <executions>
              <execution>
                <phase>package</phase>
                <id>package-feature</id>
                    <configuration>
                        <finalName>${project.artifactId}_${unqualifiedVersion}.${buildQualifier}</finalName>
                    </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
    <groupId>org.eclipse.tycho</groupId>
    <artifactId>target-platform-configuration</artifactId>
    <version>${tycho.version}</version>
    <configuration>
     <!-- Optional set the Java version your are using-->
     <executionEnvironment>JavaSE-11</executionEnvironment>
     <environments>
      <environment>
       <os>linux</os>
       <ws>gtk</ws>
       <arch>x86_64</arch>
      </environment>
      <environment>
       <os>win32</os>
       <ws>win32</ws>
       <arch>x86_64</arch>
      </environment>
      <environment>
       <os>macosx</os>
       <ws>cocoa</ws>
       <arch>x86_64</arch>
      </environment>
     </environments>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <modules>
  <module>midi</module>
 </modules>
</project>

And this is the pom.xml located in /workspace/midi:这是位于 /workspace/midi 中的 pom.xml:

<?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.hohmannit.phdev</groupId>
    <artifactId>midi</artifactId>
    <version>0.0.2-SNAPSHOT</version>

    <name>midi</name>
    <url>http://example.com</url>
    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven 
                defaults (may be moved to parent pom) -->
            <plugins>
                <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>
                                <outputDirectory>
                                    ${project.build.directory}/midi/libs
                                </outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>libs/</classpathPrefix>
                                <mainClass>
                                    com.hohmannit.phdev.midi.App
                                </mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <release>11</release>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M5</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

The gitlab CI file looks like this: gitlab CI 文件如下所示:

image: maven:3-jdk-11
variables:
  # This will suppress any download for dependencies and plugins or upload messages which would clutter the console log.
  # `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
  MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  # As of Maven 3.3.0 instead of this you may define these options in `.mvn/maven.config` so the same config is used
  # when running from the command line.
  # `installAtEnd` and `deployAtEnd` are only effective with recent version of the corresponding plugins.
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"

cache:
  paths:
    - .m2/repository


default-job:
  script:
    - mvn test
  except:
    - tags

release-job:
  script:
    - mvn clean package
  artifacts:
    paths:
      - /builds/phdev/midi-sequencer/midi/target/*.jar
  only:
    - tags

When this job runs through, I get a jar file with the following MANIFEST.MF:当这项工作完成时,我得到一个 jar 文件,其中包含以下 MANIFEST.MF:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.6.3
Built-By: root
Build-Jdk: 11.0.10
Class-Path: libs/commons-lang3-3.11.jar
Main-Class: com.hohmannit.phdev.midi.App

You can see, that it correctly resolves the dependency to commons-lang and puts it into the manifest.您可以看到,它正确地解析了对 commons-lang 的依赖并将其放入清单中。 But the actual files are missing.但是实际文件丢失了。

Anyone knows why?有谁知道为什么?

I think your maven-jar-plugin adding entries to the manifest.我认为您的 maven-jar-plugin 将条目添加到清单中。 however, it is not bundled with the dependencies.但是,它没有与依赖项捆绑在一起。

try adding the following plugin尝试添加以下插件

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>fully.qualified.MainClass</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Maven:生成具有所有依赖项的可执行jar文件 - Maven: Generating an executable jar file with all the dependencies 如何使用 maven 和系统依赖项创建可执行的 jar? - How to create executable jar with maven and system dependencies? 可执行 jar 具有依赖关系和 dll 依赖关系使用 maven - Executable jar with dependencies and dll dependency using maven 使用Maven将Java代码和依赖项组合到一个jar(可执行jar)中 - Combine java code and dependencies into one jar (executable jar) using maven Maven插件创建可执行jar,依赖项未解包(带jar的jar) - Maven plugin to create executable jar with dependencies not unpacked (jar with jars) 如何使用Maven创建一个没有依赖关系的可执行jar? - How can I create an executable jar without dependencies using Maven? 如何生成具有所有 maven 依赖项的可执行 jar 文件? - How to produce an executable jar file with all maven dependencies? 如何使用maven生成包含依赖项子集的可执行jar? - How to produce a executable jar with maven that contains a subset of dependencies? 如何使用 Maven 创建具有依赖关系的可执行 JAR? - How can I create an executable JAR with dependencies using Maven? 用Maven构建可执行jar? - Building executable jar with maven?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM