java - 将 JDK 参数正确传递到 Maven pom 文件中

标签 java maven spring-boot command-line-arguments pom.xml

因为我需要在 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/

相关文章:

java - 在 Java 中使用 .equals()、.isEmpty() 和 .length() 检查空字符串

java - 如何仅使用关联对象的外键来保存 Spring 实体

spring-boot - 使用 Spring Boot 2 和 Spring Security 5 进行多因素身份验证

java - org.springframework.dao.InvalidDataAccessApiUsageException : Write operations are not allowed in read-only mode (FlushMode. 手册)

java - GridView项目合并android

java - 在 Maven/Intellij Idea 项目中排除日志文件夹

maven - 如何构建一个多模块 Maven 项目以一次编译它?

java - Hibernate 使 Weld 不在 Java SE 中初始化

authentication - 使用Spring Security时,请求之间是否共享SecurityContext?

java - 通过计时器或游戏循环创建新敌人?