Maven在jar中嵌入依赖项工件代码 [英] maven embed dependency artifact code in jar

查看:94
本文介绍了Maven在jar中嵌入依赖项工件代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多模块Maven项目.我有一个主要的基本代码"模块,该模块创建了我项目中所有已编译源代码的jar.

I have a multi-module maven project. I have a main "base-code" module which creates a jar of all the compiled source code in my project.

我还有另一个模块"executable",该模块从相同的源代码创建一个可执行jar.为了避免重复,我想从基本代码"模块中提取类.

I have another module, "executable", which creates an executable jar from the same source code. To avoid duplication I want to pull the classes in from the "base-code" module.

我以为我要做的就是使基本代码"模块成为可执行"模块的依赖项.但我只是得到一个空罐子.我究竟做错了什么? (我的可执行" pom在下面)

I thought that all I had to do was make the "base-code" module a dependency of the "executable" module to do this. But I just get an empty jar. What am I doing wrong? (my "executable" pom is below)

 <project>
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.myproject/groupId>
    <artifactId>myproject</artifactId>
    <version>1</version>
</parent>
<artifactId>executable</artifactId>
<packaging>jar</packaging>

<dependencies>
    <dependency>
    <groupId>com.myproject/groupId>
        <artifactId>code-base</artifactId>
        <version>1</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <finalName>runnable</finalName>
                <archive>
                    <manifest>
                        <mainClass>com.myproject.Main</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

推荐答案

您正在寻找的可能是uber-jar:具有所有嵌入式jar依赖项的单个jar文件,新版本的maven-assembly-plugin支持此功能,如下所示: 4个预定义描述符之一,请检出

What you are looking for is probably uber-jar: a single jar file with all embedded jar dependencies, the newly version of maven-assembly-plugin support this as one of the 4 pre-defined descriptor, check out here.

尝试使用maven-assembly-plugin替换您的maven-jar-plugin,如下所示:

Try using maven-assembly-plugin replace your maven-jar-plugin like this:

<!-- Create single executable jar with all dependencies unpacked and embedded -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>com.myproject.Main</mainClass>
          <addClasspath>true</addClasspath>
          <classpathPrefix>lib/</classpathPrefix>
        </manifest>
      </archive>
      <descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals><goal>single</goal></goals>
      </execution>
    </executions>
  </plugin>

或者,您也可以使用 maven-shade-plugin 这个.

Alternatively, you can also use maven-shade-plugin to do this.

希望有帮助.

这篇关于Maven在jar中嵌入依赖项工件代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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