3

Well i have reusable code which i developed using Maven and the result artifact is a JAR.

Now to use it in another project , i have simply added the this dependency to that project's POM.xml, but maven is not auto detecting and including the dependencies for the jar.

How do i go about do this ?

Appreciate any pointers in this regard .

Sudhakar
  • 4,823
  • 2
  • 35
  • 42
  • Please try to search for an answer to your question before posting: http://stackoverflow.com/search?q=maven+include+dependencies+in+jar If you've already tried the things suggested in the above results, please explain what you tried and what happened so we can try and work out the issue – chrisbunney Dec 11 '12 at 13:16
  • If you find another question that answers your query, I propose closing this question as a duplicate – chrisbunney Dec 11 '12 at 13:19
  • 1
    Maven should do this automatically. When you build your first project it should place the jar and the pom for that project in your local Maven repository (by default ~/.m2/repository). When you add the jar as a dependency Maven will read in the jar and the pom and will extract the dependencies from the pom. Do you have the jar and pom in your local repository? If so, post the pom file of your second project. If not, look at your first project. – Pace Dec 11 '12 at 13:19
  • The least you can do is to accept the answer below if it helps. – walters Dec 11 '12 at 13:21

2 Answers2

0

You should try to build the developed(reusable) code with dependencies. I'm not quite sure if this will help (I had some issues with the build with dependencies, too), but I think it's worth a try. So you should add this to the pom.xml (of the reusable project):

  <build>
  ...
     <plugins>
  ...
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-jar-plugin</artifactId>
           <configuration>
              <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>fully.qualified.MainClass</mainClass>
                </manifest>
              </archive>
           </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
         </plugin>
    </plugins>
   </build>

For details, have a look at this question and the maven-documenation

Community
  • 1
  • 1
MalaKa
  • 3,734
  • 2
  • 18
  • 31
  • 2
    The question is about transitively resolving dependencies on custom built artifacts, not about building a jar with dependencies in it. – Pace Dec 11 '12 at 13:20
  • As I understood, the problem is in using the (reusable) created dependency. Did I get that wrong? :| – MalaKa Dec 11 '12 at 13:26
  • 2
    No. But the instructions you gave are about creating an uber-jar. A single jar that includes all the dependencies within the jar itself. The question is about creating a reuable jar that can be used in other projects. When used in those other projects Maven should retrieve the dependencies of the first jar and make them available in the new projects. At no point are the dependencies actually placed inside the jar however. – Pace Dec 11 '12 at 13:30
  • 2
    @MalaKa The OP wants to use the jar in another Maven project. The whole point of Maven is to prevent needing to bundle all the transitive dependencies. – artbristol Dec 11 '12 at 13:31
0

I solved this by manually adding the pom file for the jar I created.

[file structure]

GroupIdFolder

ArtifactIdFolder

VersionFolder

ownjar.jar [artifact I created]
ownjar.pom [file I mannually created]

Then in the .pom file put in the dependencies.

<project>
    <dependencies>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
    </dependencies>
</project>
RamenChef
  • 5,557
  • 11
  • 31
  • 43