How to create Maven Parent POM speciific to organization? - java

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.

Related

Importing a Maven project as a sub project in an existing project in eclipse

I am trying to import a maven project into eclipse as a subproject of a non-maven project.
The structure am seeking should look like the image below
I have tried to import the parent project and then create spring starter project and select the path to be inside the parent but I ended up with an independent project in the workspace that is duplicated in parent.
Thank in advance.
I don't think you can do that. What you can do is a modular maven project. Declare your spring-cloud as parent and guest-services as a module:
spring-cloud pom like:
<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>x.y.z</modelVersion>
<groupId>org.yourCompany.spring-cloud</groupId>
<artifactId>spring-cloud-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<properties/>...
<dependencies/>...
<build/>...
<modules>
<module>guest-services</module>
</modules>
</project>
guest-services pom like:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>x.y.z</modelVersion>
<groupId>org.yourCompany.guest</groupId>
<artifactId>guest-services</artifactId>
<name>guest-services</name>
<description>Description for this module</description>
<parent>
<groupId>org.yourCompany.spring-cloud</groupId>
<artifactId>spring-cloud-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<properties/>...
<dependencies/>...
<build/>...
</project>

Is there a way to build all my Maven projects with just one command ?

I am really new with Maven, and I have set up 2 maven projects, one of them is my utility API, the second is Jersey API. I installed utility API as part of my local Maven repository and added it in the pom.xml file.
Everything is working fine, it's just that when I want to build/run tests I need to go to each directory where projects are and run the commands. Is there a way to run one command or reconfigure the project in a way, so that I am able to process everything just from my REST API directory?
Eg. mvn clean test ... something ... -> goes and tests both of my projects.
Once again, I am new to Maven, but also I did a research and could not find a proper useful information that could help me out. If this is one of those questions that needs to be close, could you please at least provide me with some more information before closing it ? Thank you.
You can use multimodule maven projects. You define a structure like this
/
pom.xml (A)
util-api
pom.xml (B)
jersey.api
pom.xml (C)
In the children pom's you specify a <parent> node.
(A) pom.xml
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mygroup.id</groupId>
<artifactId>myparentartifact</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
...
<modules>
<module>util-api</module>
<module>jersey-api</module>
</modules>
</pom>
(B) pom.xml
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>myparentartifact</artifactId>
<groupId>mygroup.id</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>util-api</artifactId>
<packaging>jar</packaging>
</pom>
(C) pom.xml
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>myparentartifact</artifactId>
<groupId>mygroup.id</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>jersey-api</artifactId>
<packaging>jar</packaging>
</pom>
With this configuration you can build all modules with one command: mvn clean install
You can use Maven's aggregation pom.
Are you looking for a command , which can run your test and also build you app with all the module included ?
I thin you are looking for mvn clean install.
Please make sure, you say this in the highest maven pom.xml, which all include all the dependencies.
There is an option to combine related projects into one by having a top level POM using modules feature.
<modules>
<module>utility-api</module>
<module>Jersey-api</module>
</modules>
and can compile and run the test cases with
mvn clean install
You need to create parent POM for your 2 modules with
<packaging>pom</packaging>
so when you run for example mvn install on your parent POM it will run it on it's every child module.
More info on working with multiple modules here

Resolve maven dependencies from child pom

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.

how to make maven build dependent project

I have project with several dependencies on other project.
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>group1</groupId>
<artifactId>artifact1<artifactId>
<name>RealtyRegistry</name>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>group1</groupId>
<artifactId>artifact2</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>group1</groupId>
<artifactId>artifact3</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
All of them developed by me simultaniously. I add edition to files of all of project and i need to build main project together with dependent ones. How to do that for projects without tree structure?
There can be 2 or more covering trees for projects hierachy, for example: A depends on B,C; D depends on C,E; A and D are independent.
You can build multiple projects together using "Modules". Normally, you would do this by creating a "mother" project with <packaging>pom</packaging> and adding your real project as modules using the <modules> tag. Then, when you build the "mother" project, all modules are automatically built in the right order.
Here is an example from the Maven by Example book:
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
Note that this requires you to have your modules in subfolders that are named accordingly. For example, you would have the "mother" pom in some folder:
/.../my-project/
and the modules in:
/.../my-project/simple-weather/
/.../my-project/simple-webapp/
For more information, read Chapter 6. A Multi-module Project of the book, it's freely available on the Sonatype website.

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.

Categories

Resources