Why MavenProject.addCompileSourceRoot() is not working as expected?

377 views Asked by At

I have a custom plugin with the following configuration, but I can't understand why the outputDir folder is not added to the classpath of the project running the goal generate-sources.

Mojo class

package testing.sources.maven;

import java.io.*;

import org.apache.maven.plugin.*;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.project.*;
import org.codehaus.plexus.util.*;

@Mojo(name = "SRXMLToJava", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class SRXMLToJava extends AbstractMojo {

    /**
     * SR XML default location
     */
    @Parameter(defaultValue = "${project.basedir}/src/main/resources", required = true)
    private String sourceResourceDir;
    
    /**
     * Project dependency
     */
    @Parameter(defaultValue = "${project}", required = true)
    public MavenProject project;
    
    /**
     * Directory wherein generated source will be put
     */
    @Parameter(defaultValue = "${project.build.directory}/generated-sources/code", required = true)
    private File outputDir;
    
    /**
     * Converts the SR XML documents to an equivalent Java classes
     * 
     * @throws MojoExecutionException - if an unexpected problem occurs. Throwing
     *                                this exception causes a "BUILD ERROR" message
     *                                to be displayed.
     * @throws MojoFailureException   - if an expected problem (such as a
     *                                compilation failure) occurs. Throwing this
     *                                exception causes a "BUILD FAILURE" message to
     *                                be displayed.
     */
    public void execute() throws MojoExecutionException, MojoFailureException {
        
        getLog().info("1 Output dir path --> " + outputDir.getAbsolutePath());
        
        try {
            outputDir.mkdirs();
            new File(outputDir.getAbsolutePath() + "/TestingPlugin.java").createNewFile();
            FileUtils.fileWrite(outputDir.getAbsolutePath() + "/TestingPlugin.java", "package codigo;\npublic class TestingPlugin {\n public void run(){\nSystem.out.println(\"Hello world\");}}");
        } catch (final IOException exception) {
            throw new MojoExecutionException("Failed to execute plugin", exception);
        }
        
        project.addCompileSourceRoot(outputDir.getAbsolutePath());

    }

}

Plugin pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>group.testing</groupId>
    <artifactId>sources-maven-plugin</artifactId>
    <packaging>maven-plugin</packaging>
    <version>10.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.8.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>3.8.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <configuration>
                    <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
                </configuration>
                <executions>
                    <execution>
                        <id>mojo-descriptor</id>
                        <goals>
                            <goal>descriptor</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>generate-helpmojo</id>
                        <goals>
                            <goal>helpmojo</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.4</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadoc</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.apache.maven.plugins
                                        </groupId>
                                        <artifactId>
                                            maven-plugin-plugin
                                        </artifactId>
                                        <versionRange>
                                            [3.2,)
                                        </versionRange>
                                        <goals>
                                            <goal>descriptor</goal>
                                            <goal>helpmojo</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Project pom.xml where the plugin is used

<plugin>
    <groupId>group.testing</groupId>
    <artifactId>sources-maven-plugin</artifactId>
    <version>10.0-SNAPSHOT</version>
    <executions>
        <execution>
            <goals>
                <goal>SRXMLToJava</goal>
            </goals>
        </execution>
    </executions>
</plugin>

After running maven > run as > generate-sources in Eclipse. Folder ${project.build.directory}/generated-sources/code hasnt been added to the classpath.

build-helper-maven-plugin works, but I want to avoid using it.

0

There are 0 answers