Properly pass JDK argument into Maven pom file

Arkadiusz Migała :

Because I need to customize Host header in HTTP request, I need to start my Spring Boot Java app with following argument (available since JDK 12):

java -jar -Djdk.httpclient.allowRestrictedHeaders=host application.jar

but how to pass it into maven pom.xml file to be able to use this argument durring tests which are failing because of missing this flag?

I tried to use maven-compiler-plugin in following way:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <compilerArgs>
            <arg>-Djdk.httpclient.allowRestrictedHeaders=host</arg>
        </compilerArgs>
    </configuration>
</plugin>

but it's wrong:

error: invalid flag: -Djdk.httpclient.allowRestrictedHeaders=host

Following examples are not working either:

-jdk.httpclient.allowRestrictedHeaders=host

jdk.httpclient.allowRestrictedHeaders=host

So i tried even with 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>

but it's also not working because in that case this flag is ignored and I got restriction error when I run mvn test. Which is not happening when I run java with this flag.

Milen Dyankov :

You seem to be configuring the wrong plugin. You said you need to "be able to use this argument during tests" which means you should be configuring Maven Surefire Plugin.

Have a look at the example they have provided. May be you can use 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>

or the argLine approach:

<argLine>-Djava.endorsed.dirs=...</argLine>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165617&siteId=1