Resolve maven dependencies from child pom - java

I'm quite new to maven and I have a maven multi module project with the parent pom as
<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>
<groupId>com.test.cit</groupId>
<artifactId>cit</artifactId>
<version>LATEST-SNAPSHOT</version>
<name>Integration Test Framework</name>
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>core</module>
<module>login</module>
</modules>
</project>
I have put all the relevant external dependencies to the child poms of common, core and login. I then converted the project to an eclipse project (mvn eclipse:eclipse) and after that eclipse is unable to resolve the dependencies in the child pom though the respective jars are present in M2_HOME.
I then added all the dependencies to the parent pom(Whatever dependency was there in the child poms) from the child poms and then eclipse was able to resolve that.
I'm confused of this behavior. Since I've already added the external dependencies to the child poms why should I add again that to the parent pom?
Anybody could you please explain this or am I doing something wrong here to fix the problem.

In the parent pom you have the option to add two tags :
<dependencies></dependencies>
and
<dependencyManagement></dependencyManagement>
In the <dependencies> tag you have to place all the dependencies that you want that all you projects includes, for example JUnit dependency or Log4j.
In the <dependencyManagement> tag you should add all the dependencies that your project needs, It does not means that they are going to be included in all your projects. It only means that your child projects can include them or not. It is going to help you to manage the versions.
Maybe you have problems in the dependency bewteen your child projects for this reason when you add all the dependencies to your parent project It works.

Related

How to create Maven Parent POM speciific to organization?

Can any one explain how to create Parent POM, specific to organization .
Here I am not looking for multimodule project.
The POM what I am going to create will be used by all the projects and each project will have their own parent pom which extends the organization specific POM.
Please provide some steps how to create in Eclipse.
Parent POM is called a BOM in Maven, you can put it anywhere so long as you define modules inside it, which will have their own poms. Example
<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>
<groupId>myorg</groupId>
<artifactId>Myorg BOM</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Myorg BOM</name>
<modules>
<module>location/of/module1</module>
<module>location/of/module2</module>
<module>location/of/module3</module>
</modules>
...
Then you just create a POM for each individual component in the path provided in module. You can create this inside an eclipse project.

Maven build - dependancy on core classes

I have two projects I am working on which share some common code - I am putting this common code in to a new project called Core.
Both my projects use maven to build, and my core classes will also use maven. In Eclipse how do I configure maven to do a maven build of the core classes and then use these in the build for my two other applications?
Is there some prebuilt rule I need to specify - for example build this project, however, go build core first and use the output of that for this.
Hope that makes some sort of sense.
You can add a dependency to Core artifact in your project.
If you use M2E Eclipse plug-in, workspace artifacts are quite easy to reference in the Maven editor.
The only condition for this kind of dependency to work is that dependent artifact must be retrievable from a maven repository (eventually putting it to the local maven repository through install goal).
You could add a parent pom, with modules
<project>
<groupId>com.mypackage</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent</name>
<packaging>pom</packaging>
...
<modules>
<module>Core</module>
<module>MyModuleA</module>
<module>MyModuleB</module>
</modules>
...
</project>
And then just add your dependency in MyModuleA and MyModuleB like a normal dependency
<dependency>
<groupId>com.mypackage</groupId>
<artifactId>Core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

Unit more then one project to one maven project

i have 3 maven project (starto.commons,starto.hibernate,starto.server) that use some same dependencies and two of then use the thread project(commons).
i try to unit the 3 project to one big maven project (lets call him starto.bigMavenProject for the example)
i mean that:
1) every project stay project on its own bat use the starto.bigMavenProject pom for dependencies
2)when i build (run mvn insatll) starto.bigMavenProject it's build the all three project (starto.commons,starto.hibernate,starto.server).
thanks in advance.
It sounds like you want a Maven parent project for your three projects. I'd suggest taking a look at the POM documentation on inheritance as well as Sonatype's simple example.
Basically, you want a POM for start.bigMavenProject something like this:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>starto</groupId>
<artifactId>bigMavenProject</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>commons</module>
<module>hibernate</module>
<module>server</module>
</modules>
</project>
Though you may need to do some additional tweaking to, e.g., your directory structure, etc.
The key thing is your parent POM should have <packaging>pom</packaging> and define each sub-project as a module.

Can't find class from maven dependency project

[CLOSED - I had to move classes from test/java to main/java and update the maven repository via the IDE "maven options"]
I'm new to maven and inexperienced with java development. I'm using IntelliJ Idea as IDE. I'm using Maven 3.0.4.
I created a "project A" and a "project B", each with some classes. Now when I try to create a dependency in project A to a class in project B I can't seem to find any classes that are part of project B. When I check the maven repository I can see that a .jar file is created based on project B.
To clarify: when adding a dependency to project B I do find an artifact called "project B" but I cannot find any classes that are part of project B.
It doesn't seem like I can access and use any of the classes that are part of project B inside project A which would make this installation worthless.
--
Please tell me what information I should include for you to help me solve this problem.
[EDIT] Here is the project A's pom with a dependency on project B. However I either don't understand how to use it in my project or it doesn't work. While I can use IntelliJ's functionality to find and add the artifact, intelliJ can't seem to find any classes that are part of project B (while it does find classes that are part of other pre-defined packages in the maven repository):
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>planet</groupId>
<artifactId>planet</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>toolbox</groupId>
<artifactId>toolbox</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
I had to move my classes from test/java to main/java. Silly, but it took me a whole day to realise this. This only worked after updating the repository in "maven options" via the IDE.
You can do the following:
<dependency>
<groupId>toolbox</groupId>
<artifactId>toolbox</artifactId>
<version>1.0</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Works for me :)

Running maven project with eclipse dependencies outside eclipse

I'm developing a maven project with several modules in eclipse. The parent pom.xml declares all submodules, and every submodule contains a pom.xml with a reference to the parent. Some submodules are dependent on other submodules, so I have added them as a dependency (m2e finds them when searching for dependencies). However, when I try to run a submodule outside eclipse using jetty (mvn -pl submodule jetty:run), I get the error that it is missing the other submodules.
In other words, and more elaborate: there's parent, sub1 and sub2. sub2 depends on sub1. I added
<modules>
<module>sub1</module>
<module>sub2</module>
</modules>
in the parent and
<parent>
<groupId>group</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
In both sub1 and sub2, and
<dependency>
<groupId>group.parent</groupId>
<artifactId>sub1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</depdency>
in sub2.
When I run:
mvn -pl sub2 jetty:run
I get:
[INFO] Failed to resolve artifact.
Missing:
----------
1) group.parent:sub1:jar:0.0.1-SNAPSHOT
How can I get maven to find the submodule dependencies?
Have you tried running mvn install in your parent project before running Jetty in the submodule? This will install your jars in your local Maven repository, following which Maven will be able to find them.
(Or I could be grossly misreading the complexity of your question, in which case: please correct me.)

Categories

Resources