具有依赖性的Maven jar?

时间:2015-01-29 14:21:39

标签: java maven build

我想构建项目,以便最终jar包含单个jar文件中的所有依赖项 (如果不可能包括从jar文件中依赖的类) 我跟着线程Including dependencies in a jar with Maven但是包含了LT 依赖也是我甚至没有在我的pom中提到的。这是我的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>

2 个答案:

答案 0 :(得分:4)

在这里,您可以找到一个示例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>

答案 1 :(得分:3)

一种方法是使用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>