Maven汇编插件:不包括依赖jar,不包括jar的fat jar

9njqaruj  于 2023-05-16  发布在  Maven
关注(0)|答案(2)|浏览(265)

pom.xml

<?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>first.second</groupId>
    <artifactId>mytestapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>mytestapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <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>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
               
               <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>first.second.MyTest</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>assemble-all</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
               
        </plugins>
        
    </build>
</project>

mytestapp-0.0.1-SNAPSHOT-jar-with-dependencies.jar

因此,最终的jar依赖项不包括junit依赖项。
因此,当我尝试运行MyTest时:

java -cp mytestapp-0.0.1-SNAPSHOT-jar-with-dependencies.jar first.second.MyTest

我明白

MyTest main
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/Assert
        at first.second.MyTest.main(MyTest.java:16)

输出与异常

MyTest.java

package first.second;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class MyTest 
{
    public static void main( String[] args )
    {
        System.out.println("MyTest main");
        assertEquals(1, 1);
    }
}

如何让maven汇编插件includejunit jar在jar里面有依赖,这样

java -cp mytestapp-0.0.1-SNAPSHOT-jar-with-dependencies.jar first.second.MyTest

运行没有错误?
我知道关于这个主题还有其他问题,但是在尝试了许多建议的答案之后,依赖jar仍然不包括在内。

gudnpqoy

gudnpqoy1#

通常junit是一个典型的“测试”依赖项,但它不一定是...
要使其对“主”类“可用”:

  • 在编译时-我们必须删除(因为它是默认值)或设置<scope>compile</scope>
  • 在运行时-我们设置<scope>runtime</scope>

jar-with-dependencies(汇编插件内置描述符)也会更多地考虑这些。
更多详情:introduction-to-dependency-mechanism
生成的带有依赖项的jar现在看起来像这样:

myss37ts

myss37ts2#

告诉Maven通过使用依赖插件下载你的依赖,如下所示:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.2</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>

之后,您将jar文件放在target/lib目录中。现在可以将此目录添加到程序集。

相关问题