maven project dependency is a generated jar (not in maven repo) - maven

I am a maven newbie. My project depends on another maven project (ProjectA) in that I need to run mvn clean package on ProjectA which gives me JarA.
Then, I need to run java JarA feeding it with an xml configuration file which gives me another JarB. I need both JarA and JarB as dependencies on my project (ProjectB).
Any comments on whether it is possible to achieve these steps in projectB's pom file? Would having parent-submodule type of a configuration help? Thanks!

Maybe. The most simple solution to get JarB would be to add a unit test to project A. But that doesn't tell Maven about this JAR, so it will ignore it.
The next step would be to get the test to write JarB as JarA-config into the target/ folder of project A. Maven supports multiple artifacts as "build results". You can then use the "qualifier" to distinguish between them.
Use build-helper:attach-artifact to tell Maven about the second JAR. See "Attach additional artifacts to your project" for an example.
Note that package happens after test, so your test case can create the second JAR and build-helper will then find it.
In project B, you can then use this to depend on both JARs
<dependency>
<groupId>x</groupId>
<artifactId>jarA</artifactId>
</dependency>
<dependency>
<groupId>x</groupId>
<artifactId>jarA</artifactId>
<classifier>config</classifier>
</dependency>
Note the additional <classifier> element.
Note: For this to work, you need to run mvn install in project A.

Related

How to create maven project as dependent jar to other projects without dependencies

I have a java project called "A" it has some custom annotations implemented in spring aop way and it is packaged as jar using maven jar plug-in, which contains only classes specific to project of A without any dependencies.now i have installed it in local maven repository using install command (mvn install:instlal-file) and used it as dependency in another maven project called "B".
I can able to get those annotations in project "B" but i cannot get dependencies automatically from pom file in dependent jar(project "A").due to which building project "B" is failing.
Q1: should we put all dependencies which we are using for project "A" in project "B" pom as well?
Q2: or it could get automatically downloaded from dependent jar pom file?
Note: am not interested in fat jar? but its working fine if we mention all dependencies in project "B". is there any way to get all dependencies when we build project "B" automatically?
Please help me in this.
I guess your error was to use mvn install:install-file. If you do not add the POM as parameter, an (almost) empty POM is created for you.
The right way:
Go to project A and run mvn clean install.
Then use A in B, and enjoy full transitive dependency resolution.
So, to summarize, the answer for Q1 is "no", and the answer for Q2 is "yes, use mvn clean install instead of mvn install:install-file.

include jar of one module to another

I have maven projet with this architecture:
++parent-project
+module-a
+module-b
module-b is a web application. it will be run on Jboss AS 7.1.1. I'm using netbeans IDE.
Now module-b depend on module-a. this is a porm section of module-b:
<dependency>
<groupId>groupid</groupId>
<artifactId>module-a</artifactId>
<scope>compile</scope>
</dependency>
When i build the war file of module-b, module-a is not present to lib folder ( in war file. i open it with archive explorer ). therefore JBoss return ClassNotFoundException.
I'm tried differends scope ( compile , provided , runtime , test ). But nothing.
Please how can i solve this.
First of all, I think you should try to see how does it work in "pure" maven, without the IDE at all (NetBeans). So my answer will be based only on maven knowledge:
A couple of facts:
Module b has to have the following in pom: <packaging>war</packaging> This will instruct maven that you really want to get a war from this module.
When packaging WAR is specified in some pom, maven will take all the dependencies defined in this pom and will put them into the WEB-INF/lib folder of the war. Automatically. Of course, you can customize the output, but its more advanced stuff (see Maven WAR plugin if required)
All the dependencies have to be defined with group id, artifact id, and version at least. So make sure that you have the dependency on module a with version. There is no need to fiddle with scopes in this case. The default scope (if you don't specify a scope at all) is 'compile' which is fine.
Go to the directory of module b and from within the directory type: mvn dependency:tree. Once its done, please carefully observe the output, especially make sure that module a is listed (with a correct version) in a tree.
Sometime to make sure that no stale artifacts reside in the local m2 repository you might want to delete all the jars of your project from there and then execute the mvn package command again. The war has to be created in module b/target - and this is the WAR you should check out.
Note, all these steps are done without any interaction with NetBeans at all.

Maven ant task to add system path jar to WAR file

Running into a small problem. I have a spring-maven project. And there are some external jars I need to add into the POM which I did using .
Now to build the WAR file we are using an Ant Maven task i.e. artifact:mvn providing the argument war:war.
Here somehow my external jars are not getting added to the WAR file i.e. WEB-INF/lib
Can some one please let me know if I am missing something. Below is my pom entry
<dependency>
<groupId>{test}</groupId>
<artifactId>Test</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/test.jar</systemPath>
</dependency>
Best solution is to start using a repository manager (a.k.a "Maven proxy server") and install the needed artifacts (test.jar) into the repository manager and use it as a usual dependency instead of using system scope via systemPath.
Calling mvn war:war via Ant does not make sense and shows you should learn how Maven works.
Change the packaging type in your pom file to war and you can simply call maven via:
mvn clean package
and everything should work. But this is only gues cause you didn't show your full pom file.
Install the test.jar locally using mvn install:install-file (docs). Now you can remove the system scope (and the systemPath) and everything will work out of the box.

How to manage compile time dependencies in Maven

Trying to avoid the use of jargon, so that I don't get misinterpreted.
Here is the scenario, My project requires a jar in order to get compiled(let say x.jar). My project get once compiled gets converted into a WAR file, which gets deployed somewhere.
Now I want x.jar just to be there for my project to compile and it should not be packed(or part of) inside WAR file.
How can I do this in Maven ? should I used dependency scope as "provided"
You are right, as stated in the Maven FAQs, the scope to use is provided,
How do I prevent including JARs in WEB-INF/lib? I need a "compile only" scope!
The scope you should use for this is provided. This indicates to Maven that the dependency will be provided at run time by its container or the JDK, for example.
Dependencies with this scope will not be passed on transitively, nor will they be bundled in an package such as a WAR, or included in the runtime classpath.
To quickly try it out, you can use
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp
to generate a "toy webapp" project, add a dependency to your project and set it to <scope>provided</scope>.

Maven build order

I have a multi-module maven build and I need one particular module (lets call it project-A) to be build at the end. It depends on a module (lets call it project-B) that holds native code that gets compiled to a dll and installed into the maven repository as a zip file using some maven trickery. As it doesn't depends on it directly because the native code is not a java jar, I use Maven Dependency Plugin to unpack the zip file and place the native dll in my build directory. Everything is working fine except for the building order. It builds first project-A in spite of being declared the other way around in the tag in the parent. I would like to tell maven that project-A depends on project-B. I tried to add project-B as a dependency, but as it builds no jar it throws an ERROR, also this seemed hacky to me. Any help would be appreciated.
Just declare dependency in project A to project B and it will work fine. It does not matter if the project B is a native rather than a java project. Just make sure you declare the dependency correctly taking the packaging into account as type.. (which is probably pom so you would have
<dependency>
<groupId>...</groupId>
<artifactId>B</artifactId>
<version>...</version>
<type>pom</type>
</dependency>
in Project A)
The order in which you specify the modules in the parent Pom is also relevant. Maven actually builds in this order unless it has to build a module out of sequence due to direct dependencies.

Resources