One maven project using different dependencies in build phase - java

I've got a project, name it project1 and this project contains one pom.xml. Now, what I want to achieve is, that I want to specify two different "Profiles" in Eclipse when I run a maven build on the project and then each of the two profiles should use it's own dependencies. For example in build phase "build_project1_dependency1" it should use the following dependency:
<dependency>
<groupId>com.example</groupId>
<artifactId>ProjectX</artifactId>
<version>1.0.11</version>
</dependency>
and in the second build phase profile called "build_project1_dependency2" it should use the following dependency instead of the first one:
<dependency>
<groupId>com.example</groupId>
<artifactId>ProjectX</artifactId>
<version>1.0.12</version>
</dependency>

I canno't commet because i'm new here so i'll copy the link.
(i have no idea if it is right or not)
Maven assembly : add different version of the same artifact
or you can try this:
Copy two versions of same jar using maven
just search :)

Related

How Maven looks for dependencies

I have read many documentation and tutorial about getting started with Maven. But few things are still not clear to me :
1) When pom.xml contains <dependency>, Maven will put that artifact/JAr in Maven local Repository. Right ?
2) Suppose, I have sub modules in my MAven project. Then what is the correct way to build the project ? one should start from parent module and then go to further sub modules. right ? and that way maven will create dependency jars and put them in local Maven Repo folder ?
3) For example, if my sub module pom.xml contains following dependency. How should I provide it (vd-ps.jar) to Maven so that Maven finds it when I do Maven clean install on Submodule/pom.xml? I mean to say what does Maven require or from where does it generate such user defined vd-ps.jar ? from compiled class files ?
<dependencies>
<dependency>
<groupId>com.b.t</groupId>
<artifactId>vd-ps</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Ok, let me ask directly what I want to achieve: I have following pom.xml. and from it I would like to create a separate maven project (Say PTest) that uses dependencies (jars) of another big multi module maven project. Here :- I have a module named v-parent in my multi module project which has pom.xml in its trunk folder.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.b.t</groupId>
<artifactId>v-parent</artifactId>
<version>3.9.0</version>
</parent>
<groupId>com.b.t.test</groupId>
<artifactId>p_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ptesting</name>
<dependencies>
<dependency>
<groupId>com.b.t</groupId>
<artifactId>v-ps</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>
1) Now where should I put this sub module v-parent in my newly created Maven project (PTest) in eclipse ?
I mean how do I exactly make my PTest/pom.xml find this parent jar v-parent.jar ?
I have separate jar file named v-ps.jar. where should I put it exactly (in which structure) in Maven local repo so that it can find it ?
Each time you run
mvn clean install
Your project will be built and the resulting artifact will be 'installed' in your local repository (usually ~/.m2/repository). So if you build the project which makes the jar com.b.t:vd-ps:1.1.0 this will then be available in your local repository for other projects to use as a dependency.
If you dependency is in another module under the same parent it can in included like so :
<dependency>
<groupId>com.b.t</groupId>
<artifactId>vd-ps</artifactId>
<version>${project.version)</version>
</dependency>
If you run a
mvn clean deploy
Your artifact will be built and deployed to the remote repository (Nexus or Artifactory or similar), provided you have the correct config in your pom.xml
See https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
1) Now where should I put this sub module v-parent in my newly created Maven project (PTest) in eclipse ?
I mean how do I exactly make my PTest/pom.xml find this parent jar v-parent.jar ?
So in this case "v-parent" will be another project in Eclipse of packaging type "pom". And if you want to build the child project "p_test" and its dependency "v-ps", you could list them as modules in the "v-parent" project's pom file like below:
<project>
<groupId>com.b.t</groupId>
<artifactId>v-parent</artifactId>
<version>3.9.0</version>
<packaging>pom</packaging>
<modules>
<module>p_test</module> <!-- Make sure you provide the right path here -->
<module>v-ps</module> <!-- Make sure you provide the right path here -->
</modules>
</project>
Now when you run "mvn clean install" on "v-parent" it should build "p_test" and "v-ps" projects and places a copy of their jar files in your local .m2/repository, assuming those projects don't error our during the build. So the parent project acts like an aggregate for the sub-projects and helps building them all at one shot.
I have separate jar file named v-ps.jar. where should I put it exactly (in which structure) in Maven local repo so that it can find it ?
If you have the source for this project, then you should include it as part of the "v-parent" project's pom as a module like I mentioned above, and when you build the parent, on success, the jar gets written to your local .m2 repo. Or if you have the source for "v-ps" project, you could directly build that using "mvn clean install" and the jar will be written to the repo.

NoClassDefFoundError on Maven dependency present locally

I am new to Maven Project. I am making changes to one of the open source maven project. I am facing a problem in adding a library to the project. So far i have done this :-
I added a library named jni4net.j-0.8.8.0.jar to the resources folder of the project.
I right clicked the jar(in Intellij) and clicked 'Add as library'.
Then in the pom.xml i added:-
<dependency>
<groupId>jar.0.8.8.0.jni4net</groupId>
<artifactId>jar.0.8.8.0.jni4net</artifactId>
<version>0.8.8.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/jni4net.j-
0.8.8.0.jar</systemPath>
</dependency>
But when i build this project(build is successful, test cases are running) and use this it throws following error:-
java.lang.NoClassDefFoundError: net/sf/jni4net/Bridge
Please help me resolve it. I am new to maven and pom. I have looked at various answers, but not getting it right.
PS - I named groupId and artifactID as just reverse of jar file
This is not the right way to add that dependency.
All you need is:
<dependency>
<groupId>net.sf.jni4net</groupId>
<artifactId>jni4net.j</artifactId>
<version>0.8.8.0</version>
</dependency>
The dependency will be retrieved from Maven Central when you build.
Using <systemPath>...</systemPath> is highly discouraged as it usually ties your project to a local environment.
Since jni4net.j dependency is available in maven central, You don't have to download and put the dependency manually. Maven will download and store the dependency locally in `'.m2' folder. Just add dependency as bellow.
<dependency>
<groupId>net.sf.jni4net</groupId>
<artifactId>jni4net.j</artifactId>
<version>0.8.8.0</version>
</dependency>

Eclipse - How to add a project as a Maven dependency to another, instead of adding as a jar?

I have two Maven projects. One should be a dependency to another. Both have pom.xml files. When I search repository, it sees the local project and automatically added.
The pom.xml entry when I add it:
<dependencies>
<dependency> // for another dependency, an external jar
<groupId>..</groupId>
<artifactId>..</artifactId>
<version>...</version>
</dependency>
<dependency> // the one for other project which I want to add as dependency
<groupId>..</groupId>
<artifactId>..</artifactId>
<version>..</version>
</dependency>
</dependencies>
But the references to that project can not see the classes. Is there some specific way to do that, instead of adding from repository?
You set the type of dependency using the <type> tag inside <dependency> tag.
<dependency>
<groupId>..</groupId>
<artifactId>..</artifactId>
<version>...</version>
<type>jar OR war OR ejb-client OR test-jar</type>
</dependency>
The default value for type is jar
I supoose you want this because you are observing that the changes made in dependent are not getting reflected in parent project.
Ideally you should follow this
Make changes in dependent
mvn clean install dependent so that updated jar is added in local repo
mvn clean eclipse:eclipse parent jar so that updated dependent jar is added to build path of parent project
If you want to avoid this do the following (although not highly recommended)
Parent Project -> Properties -> Java Build Path -> Libraries -> Add JAR -> Select ur dependent project in same workspace-> build

How can add the proprietary library in maven project with dependency?

I have a proprietary library. I am using this library in my java desktop project but now, I have a maven project. I want to add this library in my maven project with dependency. How can I do this?
Firstly you should search if this library is available as maven dependency. Try maven search.
For example if you want to include a library commons-io-2.4.jar, you serach for it in the link above, if it is in public repository (it is), then you get maven dependency:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
You should paste this dependency to your pom to dependencies tag.
If above library is not available in public repository, you have to place your jar manually in your local repository by refering to: http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
Example:
mvn install:install-file -DgroupId=commons-io -DartifactId=commons-io -Dpackaging=jar -Dversion=2.4 -Dfile=home_folder_path/commons-io-2.4.jar -DgeneratePom=true
If it is not already on maven repository, simply add it to your pom.xml, and add it to your local repository.
<dependencies>
<dependency>
<groupId>com.proppath</groupId>
<artifactId>mylib.jar</artifactId>
<version>myjarversion</version>
</dependency>
And also put this prop library into
.m2\repository\com\proppath\myjarversion
Give any groupId you like but make sure that you have corresponding path on repository to find it.
Normally, in large projets these kind of propriatery libraries are stored in Nexus of your enterprise.

Pull dependencies of modules by referencing parent in maven java project?

I have a maven-java project (say Project A) with a parent defining modules in its pom.
I also have an external project (say Project B) that requires dependencies of two of the modules from Project A. For now, i have defined the dependency to pull each module individually.
When i replace these two with a dependency on the parent pom, it errors out on build. Is there some modification i need to make to my parent pom of Project A to make this work?
Can this be done in the first place?
Can this be done in the first place?
Declaring a dependency on an aggregating POM won't get the modules transitively. This is not going to work. It is possible to create a POM to group dependencies though.
For example, EHCache uses this technique. As mentioned in their documentation:
Maven Snippet
To include Ehcache in your project
use:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.0.1</version>
<type>pom</type>
</dependency>
The net.sf.ehcache:ehcache artifact is precisely used to group dependencies (and is distinct from net.sf.ehcache:ehcache-parent).
References
The Maven Guide
3.6.1. Grouping Dependencies

Categories

Resources