How to create jar archive of projects sources with maven
Asked Answered
L

3

5

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.

Lamrert answered 24/7, 2012 at 10:32 Comment(0)
B
5

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.

Broody answered 24/7, 2012 at 13:54 Comment(1)
This answer is indeed "correct" and answers the "why" question that @Lamrert directly asked (I upvoted), but for my money/Google search, both Raghuram and MichalKalinowski gave great examples to answer the inevitable "how".Revisory
E
1

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.

Endotoxin answered 24/7, 2012 at 10:53 Comment(2)
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 the goal to be bound already (just be including the plugin definition).Lamrert
@Matthias. Guess the document is incorrect. You can run mvn help:effective-pom to see how other plugins (like compile get bound to a phase) but not this. Also, the examples clearly indicate the need to bind the plugin to a phase.Endotoxin
S
1

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.

Spracklen answered 24/7, 2012 at 11:24 Comment(2)
Please reread my question as i have tried to clarify my central point more clearly. I do not look for a working solution but for understanding the documenation that i quoted.Lamrert
Take a look at this answer for help understanding the docs.Mother

© 2022 - 2024 — McMap. All rights reserved.