Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create jar archive of projects sources with maven

I include the following snippet in a projects object model

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>2.1.2</version>
  </plugin>

according to maven.apache.org the plugin attaches the jar goal to the package phase. However doing "mvn clean ; mvn package" does not generate a project-sources.jar in the target directory.

EDIT: Propably i do not understand the comment from the website, which i quoted: "[The source:jar goal] Binds by default to the lifecycle phase: package." I expected that, when i include the plugin section as shown above maven already binds the source:jar goal to the package phase. Am i mistaking here? What does the comment mean?

matthias.

like image 983
Matthias Avatar asked Jul 24 '12 10:07

Matthias


People also ask

How do I create a source jar?

The source plugin can be used to create a jar file of the project sources from the command line or by binding the goal to the project's build lifecycle. To generate the jar from the command line, use the following command: mvn source:jar.

Which command should you use to package source code into jar file in Maven?

Execute the “mvn clean install” command to built the project and deploy the jar files to the target folder.

How do I create a jar folder in Target?

Go to that root and run mvn package. If this is correct, there should be a new folder with the name target in the root of the project. Inside this folder there should be the jar file.


3 Answers

The documentation is a little misleading. The plugin has a default execution phase of package but there is no default goal. I believe that you have specify a goal in order for the plugin to work.

like image 106
jmcmahon Avatar answered Nov 15 '22 03:11

jmcmahon


You need to bind the plugin to a maven life-cycle goal for it to generate the source jar. Otherwise, you need to invoke it explicitly mvn source:jar.

As documented here, you can bind it to the jar goal.

like image 37
Raghuram Avatar answered Nov 15 '22 04:11

Raghuram


Try this:

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

It uses then the default binding of jar-no-fork goal to package phase of the lifecycle and that's probably what you need here.

like image 33
Michał Kalinowski Avatar answered Nov 15 '22 05:11

Michał Kalinowski