views:

1937

answers:

2

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?

+1  A: 

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
+4  A: 

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
I didn't even need the profiles, maven-source-plugin made the "sources" classifier available automatically. Just added <type>sources</type> to the dependency :)
Jacob Hansson
Sorry - I meant <classifier>, not <type>!
Jacob Hansson