Kaltresian
Kaltresian

Reputation: 971

How to generate a JAR with the source code in Maven

How can I use Maven 2.2 to generate a JAR with the source code inside it?

Upvotes: 26

Views: 26401

Answers (3)

Ryan Bennetts
Ryan Bennetts

Reputation: 1183

To do this automatically as part of your build, configure the maven-source-plugin to bind to the appropriate phase of your build:

<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>

This will create an additional jar file with the source classifier, ie: <your-artifact-name>-source.jar

Upvotes: 9

Dave Newton
Dave Newton

Reputation: 160170

Use the <resources> element; nutshell:

<build>
<...>
<resources>
 <resource>
    <directory>${basedir}/src/main/resources</directory>
 </resource>
 <resource>
    <directory>${basedir}/src/main/java</directory>
 </resource>
</resources>
<...>
</build>

Edit: Oh, I thought you meant you wanted a single jar with both normal jar contents and the source.

Upvotes: 11

stacker
stacker

Reputation: 68942

mvn source:jar

This is using the source:jar plugin documentation

Usage documentation

Upvotes: 37

Related Questions