'Unable to set directory as resource/source in Maven Custom Plugin

I have been unable to set a folder as source/resource using the most common code, copied and pasted from Apache repositories into a custom Maven Plugin:

Main/Mojo:

package com.example;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;

import java.io.File;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

/**
 * Goal that sets directory as source/resource.
 */
@Mojo(name = "set-some-dir-as-source", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public class SetDirectoryAsSourceMojo extends AbstractMojo {

    @Component
    private MavenSession mavenSession;

    @Component
    private BuildPluginManager pluginManager;

    @Parameter(defaultValue = "${project}", readonly = true, required = true)
    private MavenProject project;

    /**
     * Output location.
     */
    @Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}/some-folder/generated")
    protected File outputDirectory;

    /**
     * Main entry into mojo.
     *
     * @throws MojoExecutionException
     * @throws MojoFailureException
     */
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("Executing Mark Directory as Source");

        executeMojo(
                plugin(
                        groupId("org.codehaus.mojo"),
                        artifactId("build-helper-maven-plugin"),
                        version("3.3.0")
                ),
                goal("add-source"),
                configuration(
                        element(name("sources"),
                                element(name("source"), getOutputDirectory().getPath())
                        )
                ),
                executionEnvironment(
                        project,
                        mavenSession,
                        pluginManager
                )
        );

        getLog().info("Executing Mark Directory as Resource");

        executeMojo(
                plugin(
                        groupId("org.codehaus.mojo"),
                        artifactId("build-helper-maven-plugin"),
                        version("3.3.0")
                ),
                goal("add-resource"),
                configuration(
                        element(name("resources"),
                                element(name("resource"),
                                        element(name("directory"), getOutputDirectory().getPath())
                                )
                        )
                ),
                executionEnvironment(
                        project,
                        mavenSession,
                        pluginManager
                )
        );

        project.addCompileSourceRoot(getOutputDirectory().getPath());
        project.addCompileSourceRoot(getOutputDirectory().getAbsolutePath());
        if (getLog().isInfoEnabled()) {
            getLog().info("Source directory: " + getOutputDirectory().getPath() + " added.");
            getLog().info("Source directory: " + getOutputDirectory().getAbsolutePath() + " added.");
        }

        Resource resource = new Resource();
        resource.setTargetPath(getOutputDirectory().getPath());
        resource.setTargetPath(getOutputDirectory().getAbsolutePath());
        getProject().addCompileSourceRoot(getOutputDirectory().getAbsolutePath());
        if (getLog().isInfoEnabled()) {
            getLog().info("Resource directory: " + getOutputDirectory().getPath() + " added.");
            getLog().info("Resource directory: " + getOutputDirectory().getAbsolutePath() + " added.");
        }

    }

    /**
     * @return Returns the Maven project.
     */
    public MavenProject getProject() {
        return project;
    }

    /**
     * @return Returns the outputDirectory.
     */
    public File getOutputDirectory() {
        return this.outputDirectory;
    }

    /**
     * @param theOutputDirectory The outputDirectory to set.
     */
    public void setOutputDirectory(File theOutputDirectory) {
        this.outputDirectory = theOutputDirectory;
    }

}

In the example above, I am using executeMojo from:

https://github.com/mojo-executor/mojo-executor

And also doing it programatically in the last few lines.

Project pom.xml:

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

    <groupId>com.example</groupId>
    <artifactId>custom-set-source-maven-plugin</artifactId>
    <version>0.0.1</version>
    <packaging>maven-plugin</packaging>

    <name>custom-set-source-maven-plugin</name>

    <url>http://maven.apache.org</url>

    <organization>
        <name>The Example</name>
        <url>https://www.example.com/</url>
    </organization>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven-plugin-api.version>3.8.4</maven-plugin-api.version>
        <maven-plugin-annotations.version>3.6.4</maven-plugin-annotations.version>
        <maven-project.version>2.2.1</maven-project.version>
        <maven-plugin-plugin.version>3.6.4</maven-plugin-plugin.version>
        <maven-site-plugin.version>3.11.0</maven-site-plugin.version>
        <maven-dependency-plugin.version>3.2.0</maven-dependency-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>${maven-plugin-api.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>${maven-plugin-annotations.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-project</artifactId>
            <version>${maven-project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-dependency-plugin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.twdata.maven</groupId>
            <artifactId>mojo-executor</artifactId>
            <version>2.3.3</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-plugin-plugin</artifactId>
                    <version>${maven-plugin-plugin.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>${maven-site-plugin.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>${maven-dependency-plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>

</project>

If I simply declare the plugin in my project, from org.codehaus.mojo:build-helper-maven-plugin, it works:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
            <execution>
                <id>add-element-templates-as-source</id>
                <phase>generate-resources</phase>
                <goals>
                    <goal>add-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>${project.basedir}/src/main/resources/processes/some_folder/generated</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>

Am I missing any descriptor in my custom plugin? A way to tell the IDE that the directory has been set that I am missing? I can't seem to find anything in the Mojohaus source codes at:

https://github.com/mojohaus/build-helper-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java

or

https://github.com/mojohaus/build-helper-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java



Solution 1:[1]

  1. While building I was seeing following error:
[ERROR] 

Some dependencies of Maven Plugins are expected to be in provided scope.
Please make sure that dependencies listed below declared in POM
have set '<scope>provided</scope>' as well.

The following dependencies are in wrong scope:
 * org.apache.maven:maven-plugin-api:jar:3.8.4:compile
 * org.apache.maven:maven-model:jar:3.8.4:compile
 * org.apache.maven:maven-artifact:jar:3.8.4:compile
 * org.apache.maven:maven-project:jar:2.2.1:compile
 * org.apache.maven:maven-settings:jar:2.2.1:compile
 * org.apache.maven:maven-profile:jar:2.2.1:compile
 * org.apache.maven:maven-artifact-manager:jar:2.2.1:compile
 * org.apache.maven:maven-plugin-registry:jar:2.2.1:compile
 * org.apache.maven:maven-core:jar:3.1.1:compile
 * org.apache.maven:maven-settings-builder:jar:3.1.1:compile
 * org.apache.maven:maven-model-builder:jar:3.1.1:compile
 * org.apache.maven:maven-repository-metadata:jar:3.1.1:compile
 * org.apache.maven:maven-aether-provider:jar:3.1.1:compile

Some changes needed in the POM file to get it working.

  1. There was warning with Component annotation above Maven Session. Changed it as below:
    @Parameter( defaultValue = "${session}", readonly = true )
    private MavenSession mavenSession;

After above changes able to compile and execute the code using the command below:

mvn install com.example:custom-set-source-maven-plugin:0.0.1:set-some-dir-as-source install
  1. For some reason the code was complaining about the outputDirectory.

I have the fixed code at https://github.com/gopinnath/custom-set-source-maven-plugin

Can be executed with below maven command:

install com.example:custom-set-source-maven-plugin:0.0.1:set-some-dir-as-source install exec:java -Dexec.mainClass="Example"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1