8

If someone could help me out here it would save me a lot of time.

I maintain an open source library that gets pushed out to a sonatype repository. I make changes to that library a few times a day and push it out to the 1.0_snapshot build using mvn deploy. Let's call it project1

I work constantly in another project that uses that library let's call it project2.

Right now, whenever i make changes to project 1 or 2, i need to first build and deploy project 1 to the repo, then build project 2 so it downloads a fresh copy of project1.jar

Project2 has Project1 as a dependency in a pom.xml:

 <dependency>
            <groupId>com.group</groupId>
            <artifactId>project1</artifactId>
            <version>1.0-SNAPSHOT</version>

 </dependency>

in order to build in a way where all of my changes can be tested, I have to do something like this:

mvn  -f ./project1/pom.xml clean deploy  
mvn  -U -f ./project2/pom.xml clean package  

this uploads my project1.jar to sonatype, then project2 downloads the new snapshot and builds it.

This is a simplified picture of what i'm doing on a larger scale, where my compiles take 5 minutes of up and downloads.

Question: What is the proper way to use maven so it knows to use the source of project1 in a dependency in project 2?

bsautner
  • 4,479
  • 1
  • 36
  • 50

2 Answers2

7

IDE:

  • install m2e in eclipse
  • import your both projects into workspace
  • from consumer project (right click > maven > enable workspace resolution)

this will put project2's classes in classpath from its target/classes instead of the actual jar

native straight maven:

  • You can create a maven project tree, if this two are open source project going through same build cycle it must have one already, if they are not related but related for your usecase then you can temporarily create a maven tree on top of these 2 projects and build the top parent it will build from bottom up in one command

it will find the leaf project, build it install it in maven's cache and now while building projectA it will refer it from maven's cache so no need to deploy to sonatype

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 3
    thanks for helping, the solution i need should be independent of the IDE - i need it to build faster on the command line. – bsautner Jan 02 '15 at 21:10
  • thanks - i'll read up on maven trees, that must be what i'm missing – bsautner Jan 02 '15 at 21:23
  • Why does he need to create a project tree? If you're talking about solely local development, simply using `mvn install` on project1 will install `project1-1.0-SNAPSHOT` into the local `~/m2/repository` which maven will use when running `mvn` commands on project2. This is also true of Eclipse as long as you do as suggested above (workspace resolution) – Alex Jan 03 '15 at 04:09
  • @Alex to have single entry point of maven build from top level module – jmj Jan 03 '15 at 04:14
  • @JigarJoshi, I read the question such that project1 is a utility library that has no direct relation to project2... Restructuring them both under a parent module doesn't sound like something I would want to do in this scenario. I could be wrong, but the OP doesn't specifically outline their real relationship – Alex Jan 03 '15 at 05:22
  • yes there is not relationship case, but tweaking it temporarily will fix it – jmj Jan 03 '15 at 05:29
3

You can also point project2 to build using offline mode :

mvn -o package

Then you drop the upload part of the project1 build to the remote repo.

Check the following link: Intro to repositories and How do I configure Maven for offline development?

Community
  • 1
  • 1
liorsolomon
  • 1,785
  • 14
  • 24
  • when I make a change to project1 and compile it with mvn -o package, and then try and use that change in project2 - the build fails. It sounds like the right solution though - i want the builds to use my local repo. I'll check out the links you posted. Thanks – bsautner Jan 02 '15 at 21:34