How to create jar with sources in java maven

Task: Create separate jar file with all sources of the project in the target directory

Language: Java, Maven

Implementation:

Use next build plugin:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

As a result you will have a separate jar file with sources.

Or if you want add sources into the result jar file.

Add resources block into your build section as in this example:

...
    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
            <resource>
                <directory>${basedir}/src/main/java</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
...

Done.

Leave a Reply

Your email address will not be published. Required fields are marked *




Enter Captcha Here :