Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6943755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:11:52+00:00 2026-05-27T13:11:52+00:00

I have created a new compiler for the maven-compiler-plugin. This compiler likes to look

  • 0

I have created a new compiler for the maven-compiler-plugin. This compiler likes to look for compile sources in both src/main/groovy and src/main/java. Similarly, test sources are in both src/test/groovy and src/test/java.

I am aware of the build-helper-maven-plugin that allows users to augment their pom to specify new source folders for a build, but using this plugin is not ideal since it requires more than 20 lines of extra configuration in the pom.

I would like to create a mojo that configures the extra source folders automatically.

Here is the mojo that I have created, but I do not know how to ensure that the mojo is executed at the right time.

/**
 * @goal add-groovy-source
 * @phase generate-sources
 * @requiresDependencyResolution compile
 * @execute phase="compile"
 */
public class AddGroovySourceFolders extends AbstractMojo {
    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("Adding /src/main/groovy to the list of source folders");
        this.project.addCompileSourceRoot(project.getBasedir() + "/src/main/groovy");
    }
}

Again, my goal is to ensure the minimal amount of configuration in the user’s pom. By this I mean that there should only be a declaration of a dependency to the plugin that contains this mojo and no further configuration.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:11:53+00:00Added an answer on May 27, 2026 at 1:11 pm

    It is possible by defining a custom lifecycle.

    Step 1: Define the MOJO to add paths. You did this already with AddGroovySourceFolders.

    Step 2: Create a META-INF/plexus/components.xml file in src/main/resources:

    <component-set>
        <components>
            <component>
                <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
                <role-hint>jar</role-hint>
                <implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
                <configuration>
                    <lifecycles>
                        <lifecycle>
                            <id>default</id>
    
                            <phases>
                                <!-- I added this one, use appropriate plugin groupId/artifactId instead-->
                                <initialize>groovyadd:maven-groovyadd-plugin:add-build-paths</initialize>
                                <process-resources>org.apache.maven.plugins:maven-resources-plugin:2.4.3:resources</process-resources>
                                <compile>org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile</compile>
                                <process-test-resources>org.apache.maven.plugins:maven-resources-plugin:2.4.3:testResources</process-test-resources>
                                <test-compile>org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile</test-compile>
                                <test>org.apache.maven.plugins:maven-surefire-plugin:2.7.2:test</test>
                                <package>org.apache.maven.plugins:maven-jar-plugin:2.3.1:jar</package>
                                <install>org.apache.maven.plugins:maven-install-plugin:2.3.1:install</install>
                                <deploy>org.apache.maven.plugins:maven-deploy-plugin:2.5:deploy</deploy>
                            </phases>
                        </lifecycle>
                    </lifecycles>
                </configuration>
            </component>
        </components>
    </component-set>
    

    The element specifies the packaging the lifecycle is for. Apparently you can override the jar lifecycle (I got this working with Maven 3.0.3). I copied everything apart from the initialize phase from the appropriate component in maven-core-3.0.3.jar\META-INF\plexus\components.xml.

    The side effect of overriding Maven’s JAR lifecycle is that you have now hardcoded the compile and other plugin’s versions what is in your plugin instead of what is in Maven. I’m not sure whether that’s a bad or good thing.

    Step 3: When using your plugin from another project, all you need is:

    <plugins>
        ...
        <plugin>
            <groupId>groovyadd</groupId>
            <artifactId>maven-groovyadd-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <extensions>true</extensions>
        </plugin>
        ...
    </plugins>
    

    The important part is the extensions element. Without it, the custom lifecycle from your plugin will not be picked up.

    You can also add other plugins to other phases in the lifecycle (e.g. compile groovy code in the compile phase in a separate plugin rather than add a compiler for the maven-compiler-plugin).

    References: Overriding the Default Lifecycle from the Maven book

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my quest to generate new code in a Scala compiler plugin, I have
As per @Potatoswatter's suggestion, I have created a new discussion. Reference is this response
I have created a new SharePoint 2007 MOSS Intranet. Our admin people are purchasing
I have created a new method in one of the project's controllers that it
I have created a new table including a column note. The default is varchar(255)
I have created a new ActionFilter for an ASP.NET MVC application that I'm creating.
I have created a new silverlight business application in visual studio. It auto generates
I have created a new application using Entity Framework 4.3 database migrations. The migrations
I have created a new branch and did svn import of the source files
I'm running MOSS 2007 and have created a new site under an existing one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.