79761098

Date: 2025-09-10 16:49:29
Score: 0.5
Natty:
Report link

Adding to the answer by k_o_ I used the java-comment-preprocessor (jcp), and this is how my maven plugins looked:

      <!-- this plugin processes the source code and puts the 
           processed files into ${project.build.directory}/generated-test-sources/preprocessed -->
      <plugin>
        <groupId>com.igormaznitsa</groupId>
        <artifactId>jcp</artifactId>
        <!-- 7.2.0 is latest at this time, but 7.1.2 is latest that works 
             with jdk8. -->
        <version>7.1.2</version>
        <executions>
          <execution>
            <!-- Only my test source has conditionals.
                 Use generate-sources if using "main". --> 
            <phase>generate-test-sources</phase>
            <goals>
              <goal>preprocess</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <!-- not sure why , but I believe this is necessary when using
               generate-test-sources -->
          <useTestSources>true</useTestSources>
          <vars>
            <JDK11>true</JDK11>
          </vars>
          <sources>
            <source>${project.basedir}/src/test/java-unprocessed</source>
          </sources>
          <!-- I think I can use <targetTest> to specify where the processed files
               should be written. I just accepted the default. -->  
        </configuration>
      </plugin>

      <plugin>
        <!-- This plugin adds the generated (preprocessed) code from above, 
             into the build -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.5.0</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-test-sources</phase>
            <goals>
              <!-- Use add-source for "main". I think you need two different
                   execution entries if you need both phases.  -->
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <sources>
                <!-- This is the default output from the above jcp plugin -->
                <source>${project.build.directory}/generated-test-sources/preprocessed</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>

The result is, Java code files that are found in ${project.basedir}/src/test/java-unprocessed are processed by JCP and then dropped into ${project.build.directory}/generated-test-sources/preprocessed, and then the regular test compile includes those generated test sources.

My Java code has stuff like this in it:

//#ifdef JDK11
  code code code
//#endif
//#ifdef JDK8
  code code code
//#endif

And it does what you think it should do.

The jcp plugin is really handy, and confoundingly undocumented. There's literally no documentation, no public examples, no hints. The so-called "examples" on the wiki are not examples at all. They don't show how to do this ^^. Also I could not find a reference on all the expressions that are supported in comments. All I used was #ifdef. There's a bunch more. Good luck figuring out what's available!

For information on how to use it, I guess....read the source code?

Reasons:
  • Blacklisted phrase (1): this plugin
  • Blacklisted phrase (1): This plugin
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • High reputation (-2):
Posted by: Cheeso