How do you create a build dependency with Maven? - java

I have a maven Java project (Project A) which depends on a second project (Project B). Right now, Project A downloads Project B from the repository so it can build. However, I am also working on Project B. How can I get Maven to use the version of Project B which is on my machine and ignore the version in the repository?

If both projects have the same version (and that's a SNAPSHOT version), build Project B locally (mvn clean install) and Project A will use that version.

You can do mvn install on B so it will be installed into local repo and used from there by A. You can aggregate these two projects as modules of multi-module project so they can be build within same build reactor and see each other. You can also use IDE plugin (like m2e for Eclipse) that supports what you're saying about if you have both project in the same workspace.
The last option is probably most comfortable.

Related

Eclipse Maven: How to build the added dependency first from the project itself?

I am using Eclipse with m2e plugin.
The steps I followed are as follows:
1. Created a sample Maven project
2. Imported an another Maven project in same workspace
3. Used imported project as dependency in my sample project
Now the problem is that since the imported project is not built and does not have target jar file, the sample project is showing error(jar not found).
So the question is it possible to run the build from sample project, which will build the dependency first, which in turn will be used in sample project build?
Run sample project build -> Build dependency -> Create dependency jar -> Dependency jar used in sample project build further
If this is possible, then how to do the same?
Thank you for any suggestion/feedback. :-)
m2e has a feature named Workspace Resolution that can be enabled/disabled for every project (right click on the project in the project explorer, then Maven). This allows you to directly use the classes of another project that you declared as dependency.
Note that is only works if your dependency (in your POM) and the other project have the same coordinates, including version number.

How can I make Eclipse reference to the latest version in remote repo instead of current version in workspace?

I use maven version range for one of the dependency in project A,
<dependency>
<groupId>com.test</groupId>
<artifactId>B</artifactId>
<version>[0.2,)</version>
</dependency>
In Eclipse I also have project com.test.B in the same Workspace as project A.
I want to get version 0.2.5 which is the latest version in my remote repo,
but in Eclipse it reference to 0.2.6-SNAPSHOT which is my current development version and I haven't upload to remote repo yet, so I got
'Maven dependencies' references non existing library
error in Eclipse. How can I make Eclipse reference to the latest version in remote repo instead of current version in workspace? I tried Disable workspace resolution, didn't work. delete project B from the workspace do the trick, but I need project B.
There are two ways that this can be resolved #Sophia!
You could either go to your Project, right click and say 'Maven' -> 'Update Project'
If the above approach doesn't help, you can clean your project. Right click on your pom.xml Run as -> Maven build (with the goals as clean install).
When u fire multiple commands in single line..say mvn versions:resolve-ranges clean install...it will fetch SNAPSHOT when do release build.
Try with separate execution of commands.
Firstly run mvn versions:resolve-ranges and then run clean install
Hope these steps solve your problem!

IntelliJ IDEA : support both SBT and Maven on a single project

I already have a project in IntelliJ IDEA based on SBT. It works fine, compile and run. Now I want to add the Maven support as well.
How is it possible in IntelliJ IDEA having support for both build managers SBT and Maven in the same project and switch between them ?
In my experience you should have either maven or sbt enabled. Never both.
The solution:
checkout project from repository to project_sbt directory
open project in intellij as sbt project (just open project_sbt/build.sbt from disk)
checkout the same project from repository project to project_maven directory.
open project in intellij as maven project (just open project_maven/pom.xml from disk)
Disadvantages of this solution:
Changes in sbt project are not visible immediately in maven project (and vice versa)
You have to maintenance two build systems (special plugins)

Add bundled maven project as external jar

I have a maven project that depends on another maven project which is not available on a public repo. Let's call them projects A and B, and A depends on B.
I'm currently pulling in B in project A by declaring it as an external dependency which I've placed inside an in project repo. (I call mvn bundle:bundle in project B and just copy the generated jar over to project A) The problem is, by doing this maven can't infer which dependencies B relies on since it's a bundled jar.
Let's suppose both projects A and B rely on another maven project C which is available on the remote maven repository. Project A uses project C, version 0.2 and project B uses project C, version 0.1. However, since project B is being loaded in project A as an external bundled jar, maven doesn't know this. During run time, project A tries to call a method which exists in C 0.2 but not in C 0.1. However maven built the project by importing project B first then C as the remote dependency, so C 0.2 is not in the bundled project A jar, and we get a runtime methodNotFound error.
Any idea how I can deal with this? Is it possible to just include a pom.xml file with the bundled project B jar somehow so maven knows that B relies on C as a dependency and can sort the issue out itself?
The way I'm currently working around this is just by manually setting the build order outside of maven and not using it at all for compilation.
EDIT: OK I got it to work by just including a (artifactId)-(version).pom file in the same directory as the jar for maven to get a dependency list
If you would use maven as per tutorials (e.g. here) it just works. Why are you doing bundle? Why do you need copy something? Just declare B as dependency in A's pom.xml and do mvn install for B, then A will use it from your local maven repo.
Also, if you just add both modules into single project (I'm using IDEA), IDE recognises the dependency.

java maven rebuild dependency

I have a project which has dependency A.
Project is packing into WAR and dependency A - into jar.
And another dependency B, which also depends on A.
I want to pack project and when project B is packing it must rebuild its dependency A instead of getting compiled dependency.
Please help, how can i achieve this
Always perform a clean when doing an install, ie mvn clean install. This will make sure that all modules in the project are rebuilt, packaged, and installed in the local .m2 repository for inclusion by parent dependencies and projects.
What you may want is a multi-module project containing your three projects (WAR, A and B). Then you can run mvn package on the multi-module project and it will build and package all of the latest project code against each other without having to run mvn install to update the dependencies in your local repository first.

Categories

Resources