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仍然不包括在内。
2条答案
按热度按时间gudnpqoy1#
通常junit是一个典型的“测试”依赖项,但它不一定是...
要使其对“主”类“可用”:
<scope>compile</scope>
<scope>runtime</scope>
jar-with-dependencies
(汇编插件内置描述符)也会更多地考虑这些。更多详情:introduction-to-dependency-mechanism
生成的带有依赖项的
jar
现在看起来像这样:myss37ts2#
告诉Maven通过使用依赖插件下载你的依赖,如下所示:
之后,您将jar文件放在target/lib目录中。现在可以将此目录添加到程序集。