Including the Jar of a Maven Project in another Maven Project does not work but including the Project in another Maven Project works

StackOverflow https://stackoverflow.com/questions/10904570

Domanda

I have 2 Projects namely Project_1 and Project_2. Both projects are Maven and I am using Netbeans.

I want to include the jar of Project_1 in Project_2 which I am doing like this.

The problem is when I include the jar I do not get any compile time error, however I get a NoClassDefFoundError exception at runtime.

When I include the Project_1 in Project_2 by performing the steps mentioned here. (The Open Project example). I do not get any errors. Neither runtime nor compile time.

Can you please explain me what am I missing here?

Update

Project_2 is deployed on a Server which is not in my local machine however Project_1 is in my local machine.

Inclusion of Project_1 into Project_2 as a project was done for testing in my local machine.

È stato utile?

Soluzione

First of all, a good rule of thumb to adopt is never use the system scope and system path to pull in dependencies. In my experience there's always a better way :-)

If Project_2 depends on Project_1, then first install it's jar into the local repository:

   cd Project_1
   mvn clean install

Watch the output you'll discover the jar is placed somewhere under the following directory:

   $HOME/.m2/repository

Once this is done the jar will be available as a normal dependency to the second build

  cd Project_2
  mvn clean compile

The local repository ensures the projects are now decoupled from each other. Assuming you're using snapshot revisions of Project_1, the Project_2 build will always retrieve the latest revision built and tested.

Update

You should use a Maven repository manager to share jars between machines/teams. Recommendations are contained in the following answer:

Share jar of the module with another team

How to configure Maven to use a repository like Nexus is described in it's documentation.

As described in the deploy plugin documentation you'll need to add a distributionManagement section to your POM (detailing the repository URL) and then upload the project's jar to your repository as follows:

   cd Project_1
   mvn clean deploy
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top