Не слишком ли поздно Maven внедряет системное свойство java.io.tmpdir в JVM для JDK 17?JAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Не слишком ли поздно Maven внедряет системное свойство java.io.tmpdir в JVM для JDK 17?

Сообщение Anonymous »

The issue
I have a Maven project I'm trying to migrate it from JDK 11 to JDK 17.

In this project, I have a test using @TempDir from the library JUnit 5. Internally, this library uses java.nio.file.Files.createTempDirectory("junit"). This directory should reflect the value of the system property java.io.tmpdir if it is set.

java.nio.file.Files.createTempDirectory("junit") has parent directory set to System.getProperty("java.io.tmpdir") for JDK 11.

However, when I use Maven and JDK 17, java.nio.file.Files.createTempDirectory("junit") parent directory is "/tmp" instead of System.getProperty("java.io.tmpdir") :

mvn verify -Djava.io.tmpdir=/tmp/custom-tmpdir/ # will use /tmp anyway If I use java directly (version 17), it works :

java -ea -Djava.io.tmpdir=/tmp/custom-tmpdir/ -javaagent:[redacted] -classpath [redacted] -junit5 groupId.artifctId.ClassTest,testMethod(java.nio.file.Path) If I set the property using the environment variable _JAVA_OPTIONS, it works for Maven and JDK 17 too.

export _JAVA_OPTIONS='-Djava.io.tmpdir=/tmp/custom-tmpdir/' mvn verify Using MAVEN_OPTS or .mvn/jvm.config doesn't work.

This lets me to believe that there's a problematic interaction between Maven and JDK 17, that causes Maven to inject the system property too late.

Am I missing something ? Should I use _JAVA_OPTIONS and call it a day ?

I'm using Maven 3.6.3, it's not the latest version, but I've read the release notes of next versions and I didn't find anything implying an upgrade might help me.

I'm using OpenJDK from Red Hat (OpenJDK 64-Bit Server VM 21.9 (build 17.0.3+7-LTS, mixed mode, sharing).
Sample application to demonstrate the behavior :
pom.xml

4.0.0 org.example demotmpdir 1.0-SNAPSHOT 11 11 UTF-8 org.junit junit-bom 5.8.2 pom import org.junit.jupiter junit-jupiter test org.junit.platform junit-platform-launcher test org.apache.maven.plugins maven-compiler-plugin 3.10.1 org.apache.maven.plugins maven-surefire-plugin 2.22.2 ${java.io.tmpdir} src/main/java/org/example/Main.java

package org.example; import java.io.IOException; import java.nio.file.Files; public class Main { public static void main(String[] args) throws IOException { System.out.println("createTempDirectory : " + Files.createTempDirectory("prefix").getParent()); System.out.println("java.io.tmpdir : " + System.getProperty("java.io.tmpdir")); } } src/test/java/org/example/TempDirTest.java

package org.example; import java.nio.file.Path; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.Assertions; class TempDirTest { @Test void testTempDir(@TempDir Path tmpdir) { System.out.println("@TempDir : " + tmpdir.toAbsolutePath()); System.out.println("java.io.tmpdir : " + System.getProperty("java.io.tmpdir")); final var actual = tmpdir.getParent().toAbsolutePath().toString(); final var expected = System.getProperty("java.io.tmpdir"); Assertions.assertEquals(expected, actual); } } Sample output with JDK 11 Run the command :

mvn clean compile exec:java -Dexec.mainClass=org.example.Main verify -Djava.io.tmpdir=/tmp/custom And get the following output :

[INFO] --- exec-maven-plugin:3.1.0:java (default-cli) @ demotmpdir --- createTempDirectory : /tmp/custom java.io.tmpdir : /tmp/custom [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demotmpdir --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running org.example.TempDirTest @TempDir : /tmp/custom/junit11934696726407367076 java.io.tmpdir : /tmp/custom [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.045 s - in org.example.TempDirTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 Sample output with JDK 17 Run the command :

mvn clean compile exec:java -Dexec.mainClass=org.example.Main verify -Djava.io.tmpdir=/tmp/custom And get the following output :
[INFO] --- exec-maven-plugin:3.1.0:java (default-cli) @ demotmpdir --- createTempDirectory : /tmp java.io.tmpdir : /tmp/custom [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demotmpdir --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running org.example.TempDirTest @TempDir : /tmp/junit444674993039453082 java.io.tmpdir : /tmp/custom [ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.061 s

Источник: https://stackoverflow.com/questions/731 ... te-for-jdk
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»