user11074381
user11074381

Reputation:

can maven generate jar including source code

I try 2 things which were taught by some tutorials : 1. In maven-source-plugin:

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

then using cmd:

mvn install

or

mvn source:jar

or

mvn source:jar-no-fork

none of them works. 2. In maven-jar-plugin:

<plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
            <includes>
              <include>**/*.java</include>
            </includes>
          </configuration>
        </plugin>

none of these methods works.

then how to generate a jar including source code.

Upvotes: 0

Views: 1567

Answers (1)

Puce
Puce

Reputation: 38152

To copy the Java source into the main JAR file you can use resouces:copy-resources:

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-java-sources</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        <resources>          
                            <resource>
                                <directory>${project.build.sourceDirectory}</directory>
                                <filtering>false</filtering>
                            </resource>
                        </resources>              
                    </configuration>            
                </execution>
            </executions>
        </plugin>

Please note however that this is not the standard approach to provide Java sources with Maven and should only be used for very special use cases.

The standard approach is to enable the release-profile, which uses the maven-source-plugin to create a secondary -sources JAR containing only the sources (defined in the Maven Super POM). IDEs can load these sources JAR files and show the sources to the developers.

To activate the release-profile as defined in the Maven Super POM you can use:

-DperformRelease=true

It does not actually perform a release (no version updates or Git tags), but activates some additional goals.

Upvotes: 2

Related Questions