这是一个 POM 片段,它将主 JAR 拆分为普通工件和“JUnit 支持 JAR”:
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<excludes>
<exclude>**/junit/**</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>junit-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>junit</classifier>
<archive>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<includes>
<include>**/junit/**</include>
</includes>
</configuration>
</execution>
<execution>
<id>test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
简而言之,它会查找junit
名称中包含的任何包并将其放入 JUnit JAR 中,将其排除在正常工件之外。
如果普通罐子的坐标是
<groupId>com.group</groupId>
<artifactId>foo</artifactId>
那么您只需添加以下内容即可获得 JUnit 支持代码<classifier>junit</classifier>
:
<groupId>com.group</groupId>
<artifactId>foo</artifactId>
<classifier>junit</classifier>
因此,要使用它,所依赖的 POMcom.group:foo
将如下所示:
<dependency>
<groupId>com.group</groupId>
<artifactId>foo</artifactId>
<version>...</version>
</dependency>
<dependency>
<groupId>com.group</groupId>
<artifactId>foo</artifactId>
<version>...</version>
<classifier>junit</classifier>
<scope>test</scope>
</dependency>
有时,您将需要 JUnit 来编译您的“JUnit support JAR”。如果是这种情况,请使用
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
将 JUnit 包含到构建中。这将使依赖项在编译时可用,但不会泄漏给其他任何人。