賞金將在 4 天后到期。此問題的答案有資格獲得 100聲望賞金。 Sam Orozco想引起對這個問題的更多關注。
我有一個簡單的目標:我希望能夠使用 maven-failsafe-plugin 或任何可行的替代方案來針對我使用 maven-shade-plugin 構建的 jar 運行測驗。具體來說,我想在 shade 運行后運行測驗,因為我想要一個驗證 shade 重定位的集成測驗不會破壞我試圖重定位的東西,因為它經常可以。
當我試圖專門重新定位杰克遜時,確保杰克遜仍然能夠找到注釋/等很重要。在某些 POJO 上,以便它們正確(反)序列化。顯然,這在搬遷前有效。我們遇到了陰影 jar 沒有正確(反)序列化事物的問題,因此對我們來說,進行某種可以驗證此行為的測驗對我們來說很重要部署前。
我遇到的問題似乎是 maven-failsafe-plugin 正在以某種能力運行陰影 jar,但正在測驗原始源。意思是,即使 maven-shade-plugin 中的重定位程序應該(并且在實時工件中)重定位了對該類的參考,它也無法加載我重定位的類。
我的期望:maven-failsafe-plugin 應該完全脫離陰影源。如果沒有,其他東西應該允許我在構建/CI 時使用陰影/重定位代碼運行類似的測驗。例如,就好像我從命令列運行它一樣。順便說一句,以下是我這樣做的輸出:
>java -jar shade-integration-tests-1.0-SNAPSHOT.jar
{key=123}
test.shaded.com.fasterxml.jackson.databind.ObjectMapper
我從測驗中得到的例外。注意,這不是斷言失敗,而是類加載失敗(沒有其他例外發生):
Running test.shade.integration.tests.JsonIT
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.057 s <<< FAILURE! - in test.shade.integration.tests.JsonIT
test.shade.integration.tests.JsonIT.testClassName Time elapsed: 0.032 s <<< ERROR!
java.lang.NoSuchFieldError: MAPPER
at test.shade.integration.tests.JsonIT.testClassName(JsonIT.java:9)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:217)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
...
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>test</groupId>
<artifactId>shade-integration-tests</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>test.shade.integration.tests.MainClass</Main-Class>
</manifestEntries>
</transformer>
</transformers>
<relocations>
<relocation>
<pattern>com.fasterxml.jackson</pattern>
<shadedPattern>test.shaded.com.fasterxml.jackson</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.12.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
MainClass.java (可以忽略這個):
package test.shade.integration.tests;
import java.util.Map;
public class MainClass {
public static void main(String[] args) throws Exception {
Map<String, Object> result = Json.MAPPER.readValue("{\"key\":123}", Map.class);
System.out.println(result);
System.out.println(Json.MAPPER.getClass().getCanonicalName());
}
}
json.java
package test.shade.integration.tests;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Json {
public static final ObjectMapper MAPPER = new ObjectMapper(); // This is part of the problem
private Json() {
}
}
JsonTest.java(maven-surefire-plugin,運行良好的junit測驗):
package test.shade.integration.tests;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class JsonTest {
@Test
void testClassName() {
String result = Json.MAPPER.getClass().getCanonicalName();
assertEquals("com.fasterxml.jackson.databind.ObjectMapper", result);
}
}
JsonIT.java(maven-failsafe-plugin,失敗的 IT):
package test.shade.integration.tests;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class JsonIT {
@Test
void testClassName() {
String result = Json.MAPPER.getClass().getCanonicalName();
assertEquals("test.shaded.com.fasterxml.jackson.databind.ObjectMapper", result);
}
}
uj5u.com熱心網友回復:
問題是,如果您在同一個模塊中運行測驗,甚至在多模塊 Maven 反應器中的依賴模塊中運行測驗,那么您的模塊對要著色的類的依賴關系當然也總是在依賴關系串列中。因此,您希望獨立運行您的 IT,即不在同一個反應器內運行。最簡單的方法是Maven Invoker Plugin,許多 OSS 專案中都使用它來實作這一點。例如,看看它是如何在我參與的專案AspectJ Maven Plugin中完成的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/527021.html
標籤:爪哇行家maven-shade-插件maven-failsafe-插件
