JDK 22: Gatherer

If you take a look at JDK-22, you will find a very interesting thing called 461: Stream Gatherers (Preview). Those Gatherers are a way to enhance the Stream API, which has existed in the
JDK since 2014 (JDK 8). The collectors have been enhanced over time (you can look up the differences via Javaalmanac check for Collectors). The provided collectors in the JDK already cover a lot of things, but sometimes there are situations where it's not enough or not flexible enough or would produce code that is hard to read (more accurate, hard to understand). The first thought could be to request an enhancement of the Collectors in the JDK itself, but that would mean adding more methods on the collectors, which already have 44 methods (if I have counted correctly). Apart from having a problem that is so specific, it's worth adding that to the JDK itself. So, it might be a better solution to give the users a way to enhance the Stream API based on their own needs. That is also the summary of the JEP 461:

Enhance the Stream API to support custom intermediate operations.
This will allow stream pipelines to transform data in ways that are not easily achievable with the existing built-in intermediate operations. This is a preview API.

Using JDK21 Preview Features And/Or Incubator Classes

Sometimes you want to play around with those new fancy features of JDK21 (or even newer
JDK's) like preview features and maybe some classes from the incubator.

So how can you configure your Maven build to support such a play lesson? It's easier than you
think. Let's start the configuration. My assumption is that you would like to play around
with preview features of JDK21 for example String Templates (JEP430). I just selected this JEP for demonstration. You can select whatever JEP is in the preview.

The first thing is to know that you have to activate the preview features via:

XML
 
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <enablePreview>true</enablePreview>
  </configuration>
</plugin>


Maven Plugin Configuration: The (Unknown) Tiny Details

Introducing

If you are using Apache Maven, I suppose you have configured several plugins within your pom file already. As an example, we use the Maven Enforcer Plugin to prevent starting a given build with a Maven version that is less than 3.5.0, which looks like this:

XML
 




x
26


1
<project>
2
  <build>
3
    <plugins>
4
      <plugin>
5
        <groupId>org.apache.maven.plugins</groupId>
6
        <artifactId>maven-enforcer-plugin</artifactId>
7
        <version>3.0.0-M3</version>
8
        <executions>
9
          <execution>
10
            <id>id-enforce</id>
11
            <goals>
12
              <goal>enforce</goal>
13
            </goals>
14
            <configuration>
15
              <rules>
16
                <requireMavenVersion>
17
                  <version>3.5.0</version>
18
                </requireMavenVersion>
19
              </rules>
20
            </configuration>
21
          </execution>
22
        </executions>
23
      </plugin>
24
    </plugins>
25
  </build>
26
</project>



Maven Plugin Testing in a Modern Way Part V

In the previous part of the series - Maven Plugin Testing - In a Modern way - Part IV we have seen how to define goals to run Maven. In this part we will take a deeper look how we can define system properties for a Maven call to use. Let us take a look at a simple example taken from the previous part.

Java


This will run Maven with the goal verify. So what could you do to prevent running the unit tests or even any tests at all within that integration test case? What would you usually do on plain command line? You would have added -DskipTests to your Maven command line. How could you do the above integration test example? Very easy like this: