【问题标题】:Maven jar with dependencies?具有依赖项的Maven jar?
【发布时间】:2017-12-04 15:31:22
【问题描述】:

我想构建项目,以便最终 jar 将所有依赖项包含在单个 jar 文件中 (如果不可能,将依赖项中的类包含到 jar 文件中) 我关注了Including dependencies in a jar with Maven 的线程,但它包括在内 我什至没有在我的 pom.xml 中提到的依赖项。这是我的 POM,它只有两个依赖项。

我希望在构建 final 时,它包含 pom 中提到的特定依赖项(以类或 jar 形式)

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>com.myProject</groupId>
  <artifactId>utils</artifactId>
  <version>1</version>
</dependency>

【问题讨论】:

  • 你看到的其他依赖可能是你的依赖的依赖...你不想包含它们吗?
  • 是的,我不想包含它们
  • @suboptimal 你能给我一些例子吗,因为我不知道阴影如何帮助我
  • @user3198603 也许我在第一步误读了你的问题。您想获得一个 jar 文件,其中仅包含您在 pom.xml 中提到的依赖项,独立于范围?但是排除上述依赖项的依赖项可能会导致应用程序无法正常运行。

标签: java maven build


【解决方案1】:

在这里您可以找到一个示例 pom.xml 如何使用 maven-shade-plugin 解决它

当您运行mvn package 时,它会生成一个Jar 文件target/app-with-dependencies.jar。此 Jar 文件包含项目本身的已编译类以及依赖项 org.slf4j:slf4j-log4j12com.myProject:utils。它不包括 org.slf4j:slf4j-log4j12 所依赖的依赖项。

<?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>sub.optimal</groupId>
    <artifactId>shadedemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <maven.shade.version>2.3</maven.shade.version>
        <maven.antrun.version>1.7</maven.antrun.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>com.myProject</groupId>
            <artifactId>utils</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>${maven.shade.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-shade-plugin</artifactId>
                <version>${maven.shade.version}</version>
                <configuration>
                    <quiet>true</quiet>
                    <verbose>false</verbose>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>app-with-dependencies</finalName>
                            <artifactSet>
                                <includes>
                                    <!--
                                    here you define the dependencies which
                                    you want to be included
                                    -->
                                    <include>org.slf4j:slf4j-log4j12:*</include>
                                    <include>com.myProject:utils:*</include>
                                </includes>
                            </artifactSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>${maven.antrun.version}</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <delete>
                                    <fileset dir="${project.build.directory}" includes="${project.name}-*.jar" />
                                </delete>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

【讨论】:

  • 非常感谢。我想从最终的 jar 中排除特定的属性文件(MyProject.properties)。我尝试将以下元素 MyProject.properties 包含在您的示例中,但 MyProject.properties 仍然包含在最终 jar 中。知道为什么吗?
  • 我得到了stackoverflow.com/questions/17658831/…的答复。再次感谢。
  • 关于您的解决方案的一个问题。它创建了两个 jar 文件,即 a) shadedemo-1.0-SNAPSHOT,第二个是 app-with-dependencies。我只需要一个,即带有依赖关系的应用程序。有没有一种方法可以将输出作为单个 jar 文件而不是两个?
  • @user3198603 您可以在packagephase 的末尾删除它。但坦率地说,我会把它留在那里。无论如何都需要为阴影插件构建它。如果要清理target 目录,请执行mvn clean
  • 非常感谢。我接受答案有点晚了:(
【解决方案2】:

一种方法是使用maven组装插件,pom.xml配置如下:

<plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-assembly-plugin</artifactId>  
            <configuration>  
                <archive>  
                    <manifest>  
                        <mainClass>com.taobao.top.appstore.App</mainClass>  
                    </manifest>  
                </archive>  
                <descriptorRefs>  
                    <descriptorRef>jar-with-dependencies</descriptorRef>  
                </descriptorRefs>  
            </configuration>  
</plugin>  

【讨论】:

  • 这是stackoverflow.com/questions/1729054/…建议的解决方案。但正如我所说,它还包括我在 pom 中没有提到的所有依赖项
  • 导入依赖时,可能依赖其他jar,注意Maven依赖机制是maven的导入功能,:mkyong.com/maven/…
  • 但由于某种原因,我只想包含我的 pom 中提到的依赖项。有没有办法做到这一点?
猜你喜欢
  • 2021-05-25
  • 2014-09-02
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 2017-05-20
  • 2023-01-19
  • 2018-01-20
  • 2019-05-02
相关资源
最近更新 更多