Oops sent this to the wrong email....

Sent from my iPhone

Begin forwarded message:

From: Claudio Corsi <clco...@yahoo.com>
Date: December 15, 2020 at 20:38:30 EST
To: dev-h...@maven.apache.org
Subject: [maven-compiler-plugin]  Issue with compiling test source using modules using a multi-version maven project.


Hi All,

I am using maven to develop a java agent that uses a multi-version jar file.  This is being developed within a single project module instead of using multiple modules.  The issue is that my initial jar file will default compilation target is jdk 7 and it includes classes for jdk 9.  The issue is that I am creating a test that is using modules and when I try to build the module specific test code.  I get the UnsupportedOperationException that includes the message:

    Can't compile test sources when main sources are missing a module descriptor

This is generated within the TestCompilerMojo#preparePaths method.  I tried to get around this issue by adding compiler specific attributes in the pom file but alas I was not able to get around this issue.  I then forked the maven-compiler-plugin code and updated the code.  I was able to get around this issue by adding the following code to the mentioned method.


        // It is possible that we are building a multi-version jar file in which the main classes are not module based.
        if ( ! mainModuleDescriptorClassFile.exists() && release != null )
        {
            // @todo does it really matter that we check the version?
            int version = Integer.valueOf( release );
            if ( version >= 9 )
            {
                String releaseOutputDirectory = String.format( "%s%sMETA-INF%sversions%s%d",
                        getProject().getBuild().getOutputDirectory(), File.separator, File.separator,
                        File.separator, version );

                // @todo how to add the releaseOutputDirectory automatically?
                // testPath.add( releaseOutputDirectory );
                mainModuleDescriptorClassFile =  new File ( releaseOutputDirectory, "module-info.class" );
                if ( getLog().isDebugEnabled() )
                {
                    getLog().debug( "Updated main module " + mainModuleDescriptorClassFile );
                }
            }
        }

This was add after the initial for loop for the master branch (3.9.0-SNAPSHOT).  I was able to get around the initial exception but alas I was still having issues building the test source.  I then was able to get around the issue by adding the following compilerArgs to my pom file:

              <compilerArgs>
                <compilerArg>--module-path</compilerArg>
                <compilerArg>${project.basedir}/target/classes/META-INF/versions/9</compilerArg>
              </compilerArgs>

This allowed me to build the test source for that specific test module.

I then was wondering if there is a better solution to this issue and I noticed that if I added the releaseOutputDirectory to the testPath collection then the above compilerArgs would not be required to be able to build the test sources.  My question then is, "Is their a better way then adding the directory to the testPath?".   I worry that the above solution will not work in all cases and can be a maintenance nightmare.  It seems that being forced to include the compileArgs to the test source build is a better overall solution. 

Any opinions or comments on the above proposal?

I've attach the test pom and patched TestCompilerMojo files.

Thanks,

--Claudio

Attachment: TestCompilerMojo.java
Description: Binary data

Attachment: pom.xml
Description: XML document

Reply via email to