Maven modules and dependencies - functional test a library - java

The ultimate goal is to find out a way to functional test a library.
I have a parent project A with sub modules B,C and D.
Let us assume that the module C has a dependency on module B (B being the library) and is included as a maven dependency using the <dependency> tag.
I am just curious to know when I start up the application server say Tomcat would a jar for B (along with others) be created and then C uses this jar?
I tried to monitor my directories to see whether jars were being created every time the server was started up but that doesn't seem to be the case.
If B isn't used as a jar then it would not exactly be a FT since the ultimate goal is to be able to use B as a library in other projects as well. I feel like this shouldn't be a problem since the code itself does not change but I'd like to get some insight into the same and comments if any. Thank you !

I hope I get it right, because you seem to mix building, deploying and testing
Maven will only build the current pom. It will not build dependency libraries but expect to find them in the repository.
Only if module A is a multi-module build, then B, C and D will be built when you build A.
Starting a tomcat will not trigger any build.
For a functional test you would build, package and deploy and execute your tests against the full application.

Related

IntelliJ: How do I override a dependency in SBT with a module imported from local source?

I have 2 projects, Project A and Project B. Project A is a Play2 App that depends on Project B (which is a client library.) Currently Project B is pulled from our artifactory with SBT. I would like to set up IntelliJ such that Project B is pulled from the project source on my computer, rather than from the artifactory that is specified in.
I have added Project B as a module of Project A and added Module B to the dependencies of Module A. I then ordered Module B to be at the very top of the dependency list. The static analysis of the code seems to be working fine, there's not compilation errors showing when I updated Project A's code to use a new method signature that I've updated in Project B. However, when I run the Play App I get a compilation error stating that the method signature is incorrect.
Is there a way to override the module used at runtime for SBT and the Play App?
You can do this via sbt. In your build.sbt, for example:
val localDep = ProjectRef(file("/Users/me/projects/b"), "b")
dependsOn(localDep)
IntelliJ will import this dependency as a module. You should remove the library dependency however, to avoid conflicting classpaths.
Naturally, this makes the project hard to share unless other developers have the project in the same location. In that case, I would create a multi-project build instead, which is usually the best choice for tightly coupled projects with individual resulting artifacts.
Another option is a git project dependency:
val projectDep = ProjectRef(uri("git://github.com/me/b"),"b")

IVY - multiproject local build - mimcking "resolve dependencies in workspace"

I have three projects: A, B, C
and build.xml in ant that should compile them.
C depends on B that depends on A
So far I had all dependencies were in eclipse and I had a build.xml in ant with:
<eclipse.incrementalBuild project="A" kind="incremental"/>
<eclipse.incrementalBuild project="B" kind="incremental"/>
<eclipse.incrementalBuild project="C" kind="incremental"/>
Now, I have ivy.xml files - for example:
<ivy>
<ivy module="B"/>
<dependencies>
<dependency name="A">
</dependencies>
</ivy>
Now, all works great when I publish A, B, C in this order to a repository (I use sonatype nexus repository), since the repository is also used for resolving, so the process is:
1. resolve dependencies for A - no such
2. upload currently in workspace A as jar to repository
3. resolve dependencies for B - A is resolved as dependency
4. upload currently in workspace B as jar to repository
5. resolve dependenceies for C - B and A are resolved as dependencies
6. upload currently in workspace C as jar to repository
The way I see it publishing to nexus is for delivering the product
What I need is a simple build.xml to just compile my project - and so I don't want to publish all the time new version.
Instead I am looking for I way to use ivy to
1. compile A
2. and then compile B using already compiled A
2. and then compile C using already compiled B,A
I thought about using a local repository in the file system, in which stages 1-6 are made - producing local jars to be used - however, this is a bit cumbersome since:
1. my resolve will have to contain two resolvers - the file system preceding the nexus repo - which means that on a real publish, I will have to remember to delete the local jars
2. Sounds a bit too much work for something I believe may have a simpler solution.
In essence, I want to mimic eclipse's IVY plugin that can resolve dependencies in workspace and compile projects using other projects in the workspace - but I am looking for the best, most recommended way of doing this.
Thank you
You stated:
I have three projects: A, B, C and build.xml in ant that should
compile them.
That suggests that your project is in fact one large monolithic build.
Instead you need to emulate how Maven does its work. Each module needs to have its own build file and an ivy file that lists that module's dependencies. Each module publishes its own jar artifact into a repository (local or remote) making it accessible to other modules using ivy resolve operations.
The following examples give links to the ivy documentation on multi-module builds using ivy.
ivy simple shared repository
Ivy: Using dynamic revisions
It's not easy to break up a big project, I hope this helps.

How to build a maven project with a specific library?

I've a web-maven project 'A' that depends of a maven project 'B'. I'm using Jenkins to deploy. Before project 'A' deploy process, there is a pre-build parameter that builds the project 'B' and then proceeds with compilation. I'd need to indicate that the project 'A' will use the newly-compiled B.jar instead wich is defined on its pom.xml.
I saw this link How do I trigger another job from hudson as a pre-build step?, but I don't know how indicates the newly jar.
How can I do this?
The easy, but not guaranteed way is to force dependency updates (-U).
Project A pre builds causes B to build
B deploys B.jar to repository
A maven's build starts, check dependencies (-U), finds the most recent B which is the one you just built
A variation is to use a specific local repository shared between A and B jobs (in the advanced maven config in Jenkins), but that can be annoying if you want the jobs to execute on different slaves.
The more complex, but safer approach I'd recommend is to create a reactor project (i.e. a maven project that has A and B as modules), and build that. It can be tricky to set up if you're dealing with different repositories, but once that's done since A and B are executed in the same reactor you're guaranteed A will use the B that was just built.

Netbeans running multimodule Maven project

I've recently tried to work with NetBeans and I don't understand how it handles Maven multi-module projects.
My project has fairly complicated modules structure and when we're working on it (we mainly use IntelliJ community edition) we don't want to open all its modules (~50 modules) because it will take hours to load the project, instead we've created a 'workspace' module: a folder with pom.xml that has a packaging type pom and defines modules that I would like to load.
Lets say it defines modules A, B, C.
We have our main method (we don't use any type of container) in module A which is a low-level infrastructure module.
As a runtime dependency we need A, B, C. But A doesn't really depend on B, C, but rather B and C depend on A (in terms of Maven dependencies).
So we've created another module, lets call it runner, where we define all the dependencies. Our workspace pom.xml has module declaration of runner, so in order to run the project from IntelliJ, we use a 'classpath of module runner' while running method main in module A.
Now, How can I achieve the same affect with the latest Netbeans (7.1.2)?
I understand that question is kind of newbie's style, but I struggle with it a lot of time with no luck.
Just struggled with the same issue - from what's listed in the Netbeans wiki it seems that every module represents it's own Netbeans project and everything else would be expressed with project dependencies. See http://wiki.netbeans.org/MavenBestPractices
A project with modules is known as a multimodule, or aggregator project. Modules are projects that this POM lists, and are executed as a group. The Maven projects in NetBeans lists these modules as "Required Projects". So you will only get the required projects list populated for Maven projects with "pom" packaging that have modules defined.

Help me understand making maven project w/ non-maven jar dependencies usable by others

I'm in the process of learning maven (and java packaging & distribution) with a new oss project I'm making as practice. Here's my situation, all java of course:
My main project is ProjectA, maven-based in a github repository. I have also created one utility project, maven-based, in github: ProjectB. ProjectA depends on a project I have heavily modified that originally was from a google-code ant-based repository, ProjectC.
So, how do I set up the build for ProjectA such that someone can download ProjectA.jar and use it without needing to install jars for ProjectB and ProjectC, and also how do I set up the build such that someone could check out ProjectA and run only 'mvn package' for a full compile?
(Additionally, what should I do with my modified version of ProjectC? include the class files directly into ProjectA, or fork the project into something that could then be used by as a maven dependency?)
I've been reading around, links such as this SO question and this SO question, but I'm unclear how those relate to my particular circumstance. So, any help would be appreciated. Thanks!
So, how do I set up the build for ProjectA such that someone can download ProjectA.jar and use it without needing to install jars for ProjectB and ProjectC
Assuming ProjectA is a JAR, you can create an executable JAR that bundles the dependencies with the Maven Assembly Plugin (and the predefined jar-with-dependencies descriptor) or with the Maven Shade Plugin.
how do I set up the build such that someone could check out ProjectA and run only 'mvn package' for a full compile?
You have to deploy the dependencies to a repository that can be read over HTTP and to declare this repository in your pom.xml. AFAIK, git-hub doesn't offer any facility for that. But any web hosting service with FTP access (or better, scp) should do the trick. If your project is open source, another option would be to use Sonatype's OSS Repository Hosting service.
Just in case, you might want to read this blog post but you won't learn much more things.
The easiest would still be to organize the 3 projects as a multi-modules maven project and to build all modules.
Additionally, what should I do with my modified version of ProjectC?
From a modularization point of view (assuming you found a solution for the above part about repository), it would certainly make sense to have it as a separate module, especially if there is an opportunity someone can use ProjectC outside your project.
You have to publish the code from the additional dependencies. Two options:
Use the maven-shade-plugin to create a maven artifact containing all the content of the B and C jars, and publish that under your own G/A/V coordinates.
Publish copies of B and C under your own G/A/V coordinates using the maven-deploy-plugin to your forge just as you will publish your own code. Different forges have different policies; but if you abide by the licenses of B and C you should be OK.

Categories