Alex Man
Alex Man

Reputation: 4886

How to generate dependency project jar file

I have two maven java projects (lets say Project A and Project B). I have placed Project B as a dependency in Project A, since project A needs certain services from project B. I did like as shown below in pom.xml

<dependency>
    <groupId>com.projectB</groupId>
    <artifactId>api-projectB</artifactId>
    <version>1.1-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

Everything looks good, but when I did maven install for Project A I got the below warning

[WARNING] The POM for com.projectB:api-projectB:jar:1.1-SNAPSHOT is missing, no dependency information available

which leads to build failure with a message like as shown below

[ERROR] Failed to execute goal on project api-projectB: Could not resolve dependencies for project com.projectB:api-projectB:jar:1.1-SNAPSHOT: Could not find artifact com.projectB:api-projectB:jar:1.1-SNAPSHOT -> [Help 1]

How can I generate the Project B jar upon doing maven install for Project A. I know that if I do maven install first for Project B and then do maven install for Project A everything will work fine. But I want in the reverse way which I dont know how to do it

Can anyone please help me on this

Upvotes: 1

Views: 132

Answers (2)

Gerold Broser
Gerold Broser

Reputation: 14782

Maven resolves dependencies from two places only:

  1. the local repository (usually in ~/.m2/repository) or
  2. a remote repository, be it internal to your company or external like Maven Central.

(In fact, there are just these two types of repositories.)

[There is a third option if you're working with Eclipse, where the M2Eclipse plugin can resolve dependencies from the workspace while developing if the according project property is activated.]

So, to be able to resolve a dependency in another project you have to perform the following on the dependency's project at least once:

  • mvn install (to the local repo), if needed just on your local machine
  • mvn deploy (to a remote repo), if it is to be shared with others

To do this for all projects in one step you can create an aggregator / multi-module project like:

+- aggregator
   +- pom.xml
   +- A
   |  +- pom.xml 
   +- B
   |  +- pom.xml 
   +- C
   |  +- pom.xml 
   +- ...
   |  +- pom.xml 
   ...

where the aggregator's POM contains:

  ...
  <packaging>pom</packaging>

  <modules>
    <module>A</module>
    <module>B</module>
    <module>C</module>
    <module>...</module>
  </modules>
  ...

and perform mvn install or mvn deploy on this aggregator project.

If you don't like the extra directory level the aggregator project introduces you can create it on the same level and use <module>../A</module>, and so on, in its POM.

And, BTW, <scope>compile is the default so you don't have to declare it explicitely. Remember: Convention over configuration. ;)

Upvotes: 1

drekbour
drekbour

Reputation: 3091

Maven is saying it cannot find the jar for projectB. You need to mvn install projectB first.

If these are meant to be cohesive (i.e. they are really one build) then put them into one build with a Maven multi-module build which will then take care of doing the right things in the right order.

If they really are all seperate then, as someone mentioned, they should be kept in a central repository (e.g. Nexus, Artifactory etc) and built with CI (e.g. Jenkins) which can ensure your local build will always find a built copy of it's dependencies.

Upvotes: 1

Related Questions