因为我需要在 HTTP 请求中自定义 Host header ,所以我需要使用以下参数启动我的 Spring Boot Java 应用程序(自 JDK 12 起可用):
java -jar -Djdk.httpclient.allowRestrictedHeaders=主机应用程序.jar
但是如何将其传递到 maven pom.xml 文件中,以便能够在由于缺少此标志而失败的测试期间使用此参数?
我尝试通过以下方式使用 maven-compiler-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-Djdk.httpclient.allowRestrictedHeaders=host</arg>
</compilerArgs>
</configuration>
</plugin>
但这是错误的:
error: invalid flag: -Djdk.httpclient.allowRestrictedHeaders=host
以下示例也不起作用:
-jdk.httpclient.allowRestrictedHeaders=host
jdk.httpclient.allowRestrictedHeaders=host
所以我尝试使用 spring-boot-maven-plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Djdk.httpclient.allowRestrictedHeaders=host</jvmArguments>
</configuration>
</plugin>
但它也不起作用,因为在这种情况下该标志被忽略,并且当我运行 mvn test 时出现限制错误。当我使用此标志运行 java 时,不会发生这种情况。
最佳答案
您似乎配置了错误的插件。您说您需要“能够在测试期间使用此参数”,这意味着您应该配置 Maven Surefire Plugin 。
看看the example they have provided 。也许你可以使用systemProperties
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<systemProperties>
<property>
<name>propertyName</name>
<value>propertyValue</value>
</property>
[...]
</systemProperties>
</configuration>
</plugin>
或argLine
方法:
<argLine>-Djava.endorsed.dirs=...</argLine>
关于java - 将 JDK 参数正确传递到 Maven pom 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57908560/