Question

Right now, I have two Eclipse projects - they both use Maven 2 for all their jar-dependency goodness.

Inside Eclipse, I have project Foo included in project Bar's build path, so that I can use Foo's classes from project Bar. This works really well in Eclipse land, but when I try:

mvn compile 

inside Bar's directory, it fails because Maven doesn't know about the project-to-project relationship in Eclipse's build path.

If I were using Ant, I would just use it to do something silly like copy foo.jar into project Bar's classpath, but as far as I can tell, things are done a lot less hackishly in Maven-land.

I'm wondering if there's a standard workaround for this type of problem - it seems like it would be fairly common, and I'm just missing something basic about how Maven works.

Was it helpful?

Solution

Maybe you are referencing the other project via Eclipse configure-> build path only. This works as long as you use Eclipse to build your project.

Try running first mvn install in project Bar (in order to put Bar in your Maven repository), and then add the dependency to Foo's pom.xml.

That should work!.

OTHER TIPS

Check out the m2eclipse plugin. It will automatically and dynamically update the project build path when you change the pom. There is no need for running mvn eclipse:eclipse.

The plugin will also detect if any dependency is in the same workspace and add that project to the build path.

Ideally, if you use m2eclipse, you would never change the project build path manually. You would always edit pom.xml instead, which is the proper way to do it.

As has been previously stated, Maven will not know about the Eclipse project build path. You do need to add all dependencies to the pom, and you need to make sure all dependencies are built and installed first by running mvn install.

If you want to build both projects with a single command then you might find project aggregation interesting.

You might want to try an alternative approach, where you have a parent maven project and two children project. let's say:

Parent (pom.xml has references to both children projects/modules) --> A (depends on B) --> B

then when you run mvn eclipse:eclipse from the root of Parent, maven will generate eclipse projects for A and B, and it will have B as a required project in the classpath of A.

You can run mvn install from the root of Parent to get both projects to compile.

To complete your setup, you'll have to import both A and B into Eclipse, making sure you don't check "Copy projects into workspace".

I just needed to do this and I needed it to build with the external mvn clean install command. Here is the proper way to configure this in Eclipse. (With project B as a dependency of A)

  1. Open the pom.xml for project A in Eclipse.
  2. Go to the Dependencies tab.
  3. Click the Add... button in the middle of the page (for the left side Dependencies box)
  4. In the popup, there should be a box under a line with text above it saying Enter groupId, artifactId or sha1 prefix or pattern (*):. Enter the artifact ID for project B into this box.
  5. Double click the jar you want to add as a dependency to this project
    1. You may need to update the project after.
    2. Right click project A in you Package explorer
    3. Maven -> Update Project...
    4. Then hit OK in the popup.

I think the best way to handle it is to make Bar a Maven project just like Foo, and then mvn install it so it is available in your local Maven repository. The drawback is that you have to install that project every time you want Maven to see the changes you make to Bar.

Not a complete answer:
Bar's pom needs to include Foo in order to use maven to compile it.
I'm interested in this question too, but from the perspective of how to get eclipse to recognise a maven-added dependency is actually another project in the same workspace. I currently alter the build path after performing mvn eclipse:eclipse

If you reference a local project, but its version has been updated (usually increased), it could maybe only be found in your local repo and you have to update the (likely fixed) version of it in your POM(s).


We have a "common project" (used everywhere) which does not necessarily need to be versioned since we tag it via source control. so either

  • keeping it at a fixed version or
  • referencing it with the special LATEST version

are good workarounds to always be on the safe side.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top