views:

1213

answers:

5

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.

A: 

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.

Christian Vest Hansen
+1  A: 

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!.

Pablo Fernandez
A: 

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

Stephen Denne
+4  A: 

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.

Micke
+1  A: 

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".

alexguev