• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Maven | Maven Source Plugin
  1. Maven Source Plugin
  2. The help goal
  3. Example: Using source plugin

  1. Maven Source Plugin
    Creates a JAR archive of the source files of the current project. This plugin is essential for generating source JARs that can be used by IDEs for debugging and code navigation, and is often required when publishing artifacts to Maven Central.

    Plugin coordinates:
    • Group Id: org.apache.maven.plugins
    • Artifact Id: maven-source-plugin
    • Version: 3.3.1

    Plugin Prefix: source

    The source plugin has the following goals:
    • help: Displays help information about the maven-source-plugin.

    • jar: Bundles all the sources into a JAR archive. The JAR file is created in the target directory with the classifier "sources".

    • test-jar: Bundles all the test sources into a JAR archive. The JAR file is created with the classifier "test-sources".

    • jar-no-fork: Bundles all the sources into a JAR archive. This goal does not fork the build and is suitable for attaching to the build lifecycle. It is recommended for most use cases.

    • test-jar-no-fork: Bundles all the test sources into a JAR archive. This goal does not fork the build and is suitable for attaching to the build lifecycle.

    • aggregate: Aggregates sources for all modules in an aggregator project. This goal is useful for multi-module Maven projects.

    • generated-test-jar: Bundles all the generated test sources into a JAR archive. This includes sources generated during the build process.

    Command line usage examples:
    # Generate source JAR for current project
    $ mvn source:jar
    
    # Generate test source JAR
    $ mvn source:test-jar
    
    # Generate both during package phase
    $ mvn clean package
    
    # Generate source JAR without forking
    $ mvn source:jar-no-fork
    When configured properly, the plugin will generate files like:
    • my-project-1.0.0-sources.jar - Contains main source files
    • my-project-1.0.0-test-sources.jar - Contains test source files
  2. The help goal
    The help goal provides general information about the source plugin, including available goals and their parameters.

    Use the detail parameter to get detailed information about all goals and their configuration options:
    # Display basic plugin information
    $ mvn source:help
    
    # Display detailed information about all goals and parameters
    $ mvn source:help -Ddetail=true
    If you need to use the plugin's full coordinates instead of its prefix, you can do that as follows:
    # Display basic plugin information
    $ mvn org.apache.maven.plugins:maven-source-plugin:3.3.1:help
    
    # Display detailed information about all goals and parameters
    $ mvn org.apache.maven.plugins:maven-source-plugin:3.3.1:help -Ddetail=true
    You can also use the Maven help plugin to describe the source plugin:
    # Basic plugin description using help plugin
    $ mvn help:describe -Dplugin="org.apache.maven.plugins:maven-source-plugin:3.3.1"
    
    # Detailed plugin description with all parameters
    $ mvn help:describe -Dplugin="org.apache.maven.plugins:maven-source-plugin:3.3.1" -Ddetail=true
    To get specific information about a goal, use the goal parameter:
    # Help for the 'jar' goal
    $ mvn org.apache.maven.plugins:maven-source-plugin:3.3.1:help -Dgoal=jar -Ddetail=true
    
    # Help for the 'jar' goal using the help plugin
    $ mvn help:describe -Dplugin="org.apache.maven.plugins:maven-source-plugin:3.3.1" -Dgoal=jar -Ddetail=true
    Note: When using the plugin's help goal directly, you don't need to specify the version if the plugin is already configured in your POM or if you want to use the latest version.
  3. Example: Using source goals in the project pom.xml file
    Adjust your pom.xml file with the following configuration to automatically generate source JARs during the package phase:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.3.1</version>
        <executions>
            <execution>
                <id>attach-sources</id>
                <phase>package</phase>
                <goals>
                    <goal>jar-no-fork</goal>
                    <goal>test-jar-no-fork</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    Note: Using jar-no-fork and test-jar-no-fork goals is recommended over jar and test-jar as they don't fork the build process, making the build more efficient.

    For a simpler configuration that only generates the main source JAR:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.3.1</version>
        <executions>
            <execution>
                <id>attach-sources</id>
                <goals>
                    <goal>jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
© 2025  mtitek