<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.vaadin</groupId>
    <artifactId>vaadin-swing-bridge</artifactId>
    <version>1.4-SNAPSHOT</version>
  </parent>
  <artifactId>swing-bridge-patch</artifactId>
  <name>Vaadin Swing Bridge Patch</name>
  <description>JDK internal patches for the Vaadin Swing Bridge</description>

  <licenses>
    <license>
      <name>GPLv2 + CPE</name>
      <url>https://openjdk.org/legal/gplv2+ce.html</url>
      <distribution>repo</distribution>
    </license>
  </licenses>

  <properties>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
    <spotless.license-header>${maven.multiModuleProjectDirectory}/eclipse/gpl-v2-cpe.txt</spotless.license-header>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>swing-bridge-graphics</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>com.diffplug.spotless</groupId>
        <artifactId>spotless-maven-plugin</artifactId>
        <configuration>
          <java>
            <!-- Keep the copied JDK source byte-identical to upstream except
                 for the marked "SwingBridge patch:" hunks, so re-syncing with
                 a newer JDK stays a reviewable, mechanical diff. -->
            <excludes>
              <exclude>src/main/java/java/awt/KeyboardFocusManager.java</exclude>
              <exclude>src/main/java/javax/swing/RepaintManager.java</exclude>
              <!-- Our own tests of the patched classes: the module's license
                   header is Oracle's, which they are not. -->
              <exclude>src/test/java/**</exclude>
            </excludes>
          </java>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.14.1</version>
        <configuration>
          <compilerArgs>
            <!-- Compile the patched sources as members of java.desktop so
                 they may access package-private members of java.awt (the
                 patched KeyboardFocusManager needs e.g. Component.appContext,
                 AWTEvent.isPosted, SequencedEvent). -->
            <compilerArg>--patch-module</compilerArg>
            <compilerArg>java.desktop=${project.basedir}/src/main/java</compilerArg>
            <!-- swing-bridge-graphics (VaadinGlobalFocusState etc.) is on the
                 classpath at compile time and on -Xbootclasspath/a at runtime,
                 i.e. in the unnamed module in both cases. -->
            <compilerArg>--add-reads=java.desktop=ALL-UNNAMED</compilerArg>
            <compilerArg>--add-exports=java.base/jdk.internal.util=java.desktop</compilerArg>
          </compilerArgs>

        </configuration>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <configuration>
              <!-- The tests use only public API of the patched classes, so
                   they compile against the stock java.desktop; the patch is
                   applied to the surefire fork at run time, see below. -->
              <compilerArgs combine.self="override">
                <compilerArg>--add-exports=java.desktop/sun.awt=ALL-UNNAMED</compilerArg>
              </compilerArgs>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.4.2</version>
        <configuration>
          <excludes>
            <exclude>sun/font/FontManager.class</exclude>
          </excludes>
        </configuration>
      </plugin>
      <!-- The test fork patches java.desktop with the same classes the jar
           ships, so the copy below leaves out what the jar leaves out. -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>patch-classes-for-tests</id>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <phase>process-test-classes</phase>
            <configuration>
              <outputDirectory>${project.build.directory}/patch-classes</outputDirectory>
              <resources>
                <resource>
                  <directory>${project.build.outputDirectory}</directory>
                  <excludes>
                    <exclude>sun/font/FontManager.class</exclude>
                  </excludes>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- Exposes ${com.vaadin:swing-bridge-graphics:jar}, the resolved
           graphics artifact, for the surefire argLine. -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.8.1</version>
        <executions>
          <execution>
            <id>dependency-paths</id>
            <goals>
              <goal>properties</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.5.4</version>
        <configuration>
          <!-- Run the tests the way the playground runs: java.desktop patched
               with this module's classes, swing-bridge-graphics on the boot
               classpath so the patched classes can reach it, and the same
               exports and opens the playground passes (the patched
               PlatformGraphicsInfo makes the bridge's toolkit and graphics
               environment the JVM's, so the fork is a bridge JVM). -->
          <argLine>--patch-module java.desktop=${project.build.directory}/patch-classes -Xbootclasspath/a:${com.vaadin:swing-bridge-graphics:jar} --add-reads=java.desktop=ALL-UNNAMED --add-exports=java.desktop/sun.font=ALL-UNNAMED --add-exports=java.desktop/sun.awt=ALL-UNNAMED --add-exports=java.desktop/sun.awt.dnd=ALL-UNNAMED --add-exports=java.desktop/sun.awt.dnd.peer=ALL-UNNAMED --add-exports=java.base/sun.nio.cs=ALL-UNNAMED --add-exports=java.desktop/sun.java2d=ALL-UNNAMED --add-exports=java.desktop/sun.java2d.pipe=ALL-UNNAMED --add-exports=java.desktop/sun.awt.datatransfer=ALL-UNNAMED --add-exports=java.desktop/sun.awt.image=ALL-UNNAMED --add-exports=java.desktop/java.awt.peer=ALL-UNNAMED --add-exports=java.desktop/java.awt.dnd=ALL-UNNAMED --add-exports=java.desktop/java.awt.dnd.peer=ALL-UNNAMED --add-exports=java.desktop/sun.print=ALL-UNNAMED --add-exports=java.desktop/sun.swing=ALL-UNNAMED --add-opens=java.desktop/java.awt.event=ALL-UNNAMED --add-opens=java.desktop/sun.awt=ALL-UNNAMED --add-opens=java.desktop/java.awt.dnd=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED</argLine>
          <systemPropertyVariables>
            <java.awt.headless>false</java.awt.headless>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>release</id>
      <build>
        <plugins>
          <!-- Override parent: GPL requires distributing real sources -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
              <execution>
                <id>license-sources-jar</id>
                <phase>none</phase>
              </execution>
              <execution>
                <id>license-javadoc-jar</id>
                <phase>none</phase>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <configuration>
              <!-- javadoc cannot document the patched java.desktop sources;
                   the sources jar still ships them (GPL). -->
              <sourceFileExcludes>
                <sourceFileExclude>java/awt/**</sourceFileExclude>
                <sourceFileExclude>javax/swing/**</sourceFileExclude>
              </sourceFileExcludes>
              <additionalJOptions>
                <additionalJOption>--add-exports=java.base/jdk.internal.util=ALL-UNNAMED</additionalJOption>
              </additionalJOptions>
            </configuration>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
