簡體   English   中英

如何使用 maven 和系統依賴項創建可執行的 jar?

[英]How to create executable jar with maven and system dependencies?

我的 Maven 項目中幾乎沒有系統依賴項,我已將其包含如下:

<dependency>
    <groupId>Com.myCompany</groupId>
    <artifactId>myArtifact</artifactId>
    <scope>system</scope>
    <version>1.0</version>
    <systemPath>${project.basedir}\lib\myArtifact-1.0.jar</systemPath>
</dependency>

我正在嘗試創建一個包含所有必需依賴項的可執行 jar [fat jar],因此該 jar 可以獨立運行。

我正在使用“maven-assembly-plugin”以這種方式進行打包:

<plugin>
    <artifactId>maven-assembly-plugin<artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true<addClasspath>
                <mainClass>com.mycompany.Application</mainClass>
            </manifest>
         </archive>
         <descriptorRefs>
             <descriptorRef>jar-with-dependencies</descriptorRef>
         </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

問題是,當創建包時,系統依賴項不包含在 jar 中,我在運行它時遇到 ClassNotFound 異常。

有人可以指導我正確的配置方式嗎?

更新:

很少有人提到我可以將依賴項安裝到本地存儲庫。 這里的問題是我們有一個自動構建服務器,每當我們提交到 repo 時就會觸發它。 我真的不想將它們安裝在遠程構建服務器上。

深入挖掘,發現這個hacky配置(不推薦!!)

在 Maven 程序集插件中引用此 xml:

<configuration>   
    <appendAssemblyId>false</appendAssemblyId>   
    <descriptors>     
        <descriptor>${basedir}/assembly.xml</descriptor>   
    </descriptors> 
</configuration>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>jar-with-all-dependencies</id>
<formats>
    <format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <useProjectArtifact>true</useProjectArtifact>
        <unpack>true</unpack>
        <scope>runtime</scope>
    </dependencySet>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <unpack>true</unpack>
        <scope>system</scope>
    </dependencySet>
</dependencySets>

 </assembly>

暫無
暫無

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

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