java - 具有依赖关系的 Maven jar?

标签 java maven build

我想构建项目,以便最终 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>

最佳答案

在这里你可以找到一个例子 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>

关于java - 具有依赖关系的 Maven jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28217001/

相关文章:

java - 用于在 iOS/android 上为塔防等 2D 游戏制作动画的方法

java - 加载 csv 文件抛出 NullPointerException

java - 无法在 Java 中删除文件,因为它是在 Java Platform SE 二进制文件中打开的

java - Maven jar :jar not gathering dependencies

java - 发布一个简单的 Java 库到 Maven

c++ - 构建项目时遇到了 Eclipse C++ 问题。

java - 寻找基于AOP和注解的方法参数验证框架

maven - 试图运行一个 Maven 包。找不到资源 'org.eclipse.m2e:lifecycle-mapping:pom:1.0.0'

mysql - 由于数据库访问凭据错误,Spring Boot War打包失败

android - Android项目:gradle配置失败