12

I have two maven modules, one that ends up as a jar, and one war that depends on that jar.

I want the jar module to package it's source code together with the compiled classes in the jar, so that the second module is able to access it. I have tried using the maven-source-plugin, but I am confused as to how to add a dependency on the output of that. It seems that the dependency by default goes to the compiled jar, and not the source-code jar (ending with "-source.jar") that maven-source-plugin creates.

How do I add the "-source.jar" as a dependency, while still preserving the dependency on the compiled sources?

Jacob Hansson
  • 149
  • 3
  • 7
  • 19

2 Answers2

20

I've not tried this, but I think you need to create two profiles in your project. One which builds the main jar. The other which builds the sources jar. Unfortunately, I'm not exactly sure how you would build that profile. I couldn't find a good example of it so far.

(Accoding to the comments, you don't actually need a profile. You can just use the sources-plugin which will deploy the sources and make them available via the sources classifier)

In theory, you'd use the 2nd profile to attach the sources to the project. This creates a 2nd entry in your repository for the sources using that classifier. Once you install/deploy the sources to your repository, you should be able to include the sources as a dependency by using the classifier tag on the dependency to specify the sources directly.

So you'd have something like this in your webapp POM:

<dependencies>
  <dependency>
    <groupId>myGroup</groupId>
    <artifactId>myJar</artifactId>
    <version>4.0</version>
    <type>jar</type>
  </dependency>
  <dependency>
    <groupId>myGroup</groupId>
    <artifactId>myJar</artifactId>
    <version>4.0</version>
    <type>jar</type>
    <classifier>sources</classifier>
  </dependency>
</dependencies>
Mike Cornell
  • 5,909
  • 4
  • 29
  • 38
  • 3
    I didn't even need the profiles, maven-source-plugin made the "sources" classifier available automatically. Just added sources to the dependency :) – Jacob Hansson Jun 18 '09 at 10:21
  • 4
    Sorry - I meant , not ! – Jacob Hansson Jun 18 '09 at 12:46
  • 2
    We had the same setup for our mGWT project. We put the AutoBean interfaces in a "shared" maven module, used by the services on the server (REST services using CXF) and on the mGWT client. The problem is that the maven gwt:compile goal need the sources AND the compiled versions to complete. The above configuration worked well for us also. – peron Jun 14 '12 at 08:29
1

Did you try adding the src directory as a resource directory in the build section? That should copy the source into the jar on build.

sal
  • 23,373
  • 15
  • 66
  • 85