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.

Matthias
  • 3,458
  • 4
  • 27
  • 46

3 Answers3

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.

jmcmahon
  • 630
  • 1
  • 7
  • 14
  • 4
    This answer is indeed "correct" and answers the "why" question that @Matthias directly asked (I upvoted), but for my money/Google search, both Raghuram and MichalKalinowski gave great examples to answer the inevitable "how". – JJ Zabkar Dec 02 '14 at 17:04
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.

Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • 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). – Matthias Jul 24 '12 at 10:55
  • 1
    @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. – Raghuram Jul 24 '12 at 12:20
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.

Michał Kalinowski
  • 16,925
  • 5
  • 35
  • 48
  • 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. – Matthias Jul 24 '12 at 11:28
  • Take a look at this [answer](http://stackoverflow.com/a/10568004/944849) for help understanding the docs. – user944849 Jul 24 '12 at 14:04