Purpose of POM file dependency [duplicate] - java

I have just started working with Maven in Eclipse.
I tried running a sample program in it, and I encountered some problems. The errors were in my pom.xml file. What is the significance of the pom.xml file in Maven?

In short the pom.xml will have all information to build your project.
For example you want to build a project and you have only the pom.xml sent to you via mail. If there are enough entries in the pom.xml then that is all you need! You can import it to Eclipse, Maven will download your source code from CVS, download various dependency jars (like Spring, Apache Commons), run your test cases, build the jar/war, deploy to your jboss/app server, generate a report of your code quality (using Sonar, maybe). Each task you want to do will be mentioned as a goal.
The links already provided are good enough for reference.

POM is an XML file that contains the project configuration details used by Maven. It provides all the configurations required for a project.
POM means Project Object Model, and, as the name suggests, it defines the model of the project as well.
In the normal project development you will add JAR files and libraries as required. In Maven-based development, those JAR files, libraries are added to the project using this pom.xml. In the pom context we call those JAR files, libraries as dependencies.

Maven is a build tool and pom.xml is the main file for the project.
The POM
The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of the information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively.
For more reference, check Maven in 5 Minutes.

POM stands for project object model. It's the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used to build the project. It downloads required libraries easily using POM XML tags.

When there is no Maven, it needs to add all the library JAR files one by one to the project. But when there is Pom.xml there is no need to add library JAR files one by one.
Simply add the dependency to the Pom.xml, and it will automatically add the library JAR files to the project.

pom.xml is a file which describes the project, configures plugins, and declares dependencies. The POM names the project, provides a set of unique identifiers (called coordinates) for a project, and defines the relationships between this project and others through dependencies, parents, and prerequisites.
A POM file can include a modules section, which tells Maven which directories have POM files which need to be built.
In the build section you can define plugins for which you need to build the artifacts in your project.

Pom.xml is part of your maven project, using pom.xml, maven life cycle you can achieve it.
The pom.xml is a project object model which tells everything 3rd party tool dependencies and library's and required plugins it will give everything to your project like project means any java based web content like itself. once you create the maven project you will get the pom.xml blog which is everything to handle your project
If you want to test your project, you need to add testing dependencies which is the maven community it will provide you once added it will have everything tested.

Related

How do I get a local Maven package to bring in its dependencies?

I have written a series of classes that I want to turn into a company library. Managing all it's dependencies was a pain so I made a Maven project for it to be handled automatically.
I have packaged my library project into a .jar and added it to my local Maven repository. I can now list it in my application project's pom.xml, it get's brought in just like any other dependency and works great.
The problem is the dependencies of my library do not get brought in. It seems like this should be straight forward but trying to copy other packages in the Maven repository doesn't work.
Right now my jar consists of two directories:
com\company\package\Main.class
And:
META-INF\maven\com.company\package\pom.xml
Where do I need to put the pom.xml file for Maven to go get my dependency's dependencies? Is there something else in the .jar build that I am missing?
The Maven POM file (pom.xml), which describes the project/module, is usually placed in the root directory.
The POM file can then be picked up by your Maven installation, which will automatically configure the project, which means also downloading dependencies from their repositories.
For an example of an artifact deployed to a repository, see
http://central.maven.org/maven2/org/springframework/spring-core/5.1.5.RELEASE/

Packaging to Maven project

currently we are using ANT script to build the project and running jUnit tests. Now, we decided to move to Maven.
We have two web projects, Core-Project and Sub-Project. Now here it gets complicate. Their project is as follow.
Now, If I want to add this Sub-Project to Core-Project then I will create a jar of Sub-Project with WebContent folder and put it into Core-Project. Now whenever I run Core-Project, we have one utility class which extract content of Sub-Project into Core-Project.
Final(expected) project structure should look like this.
How can I achieve this in Maven? I mean how do I create a jar which contain some files located in src->main->webapp.
It seems that you need to rethink your architecture a bit. This answer may go beyond the scope of your question, but it's important to treat Maven as 'convention over configuration'. It is possible to achieve your layout using a combination of maven packaging tools, but if you restructure and follow Maven conventions, it will make more sense to people outside your project and be less work to maintain.
Suggested:
config-project
sub-project
core-project
config-project can hold the configuration for all parts of your application. sub-project and core-project can depend on this project and use it at runtime. You should package this project as 'jar' or 'zip' depending on the resources you need to make available to other projects.
sub-project should only contain the binary code common to the non-web based part of your application. It should be packaged as a 'jar' project and not be packaged with the config-project dependencies.
core-project should be packaged as a 'war' project. and follow the directory structure as suggested here: Maven War Plugin
Keeping the separation between your configuration, your non-web code and your web code will take a little bit to get used to. There is an excellent archetype by Tomcat which generates a maven project structure composed of these parts. It is easy to generate and inspect: Maven Tomcat Archetype

Single or Multiple Maven pom.xml configuration files?

I would like to ask these questions related to pom.xml files in Maven projects.
What is a reason to have multiple pom.xml for all dependency libraries instead of having all dependencies in one single pom.xml?
Where should be these pom.xml files in Maven project placed?
This is an example of pom.xml for Spring framework - http://search.maven.org/remotecontent?filepath=org/springframework/spring-core/3.2.5.RELEASE/spring-core-3.2.5.RELEASE.pom
What is a reason to have multiple pom.xml for all dependency libraries instead of having all dependencies in one single pom.xml?
A maven project can be made of many artifacts. One artifact may be a String manipulation library. Another may be a webapp that uses that String manipulation library.
Here's why you shouldn't put all your dependencies in one pom: Your String manipulation library should not have a reference to the servlets.jar just because an unrelated pom is a webapp. Each artifact should have only what it needs in its classpath.
(You may be interested to learn about the dependencyManagement tag, but it does not directly relate to your question.)
Where should be these pom.xml files in Maven project placed?
As #MariuszS linked to, here's the Standard Directory Layout.
At the top level files descriptive of the project: a pom.xml file (and any properties, maven.xml or build.xml if using Ant). In addition, there are textual documents meant for the user to be able to read immediately on receiving the source: README.txt, LICENSE.txt, etc.
This depends on your project, if project is small and product of this project (artifact) is only one then one pom is enough. But if your project have many artifacts (libraries, WARs, EARs etc) then for every artifact pom is required (generally).
Maven concept is that one project definition (POM) generate only one artifact (there are exceptions). Every project should have separate directory with pom.xml inside and source directories if needed.
One maven project can build two diffrent apps (for example desktop and webapp). This diffrent applications has diffrents dependencies.
Sample multimodule project structure: https://github.com/peter-lawrey/Java-Chronicle
Read more
Introduction to the Standard Directory Layout
Guide to Working with Multiple Modules
Maven by Example
Chapter 6. A Multi-module Project

Web resources in Maven Central

What is the best way to add project in Maven Central repository if it uses jar-file and web-resources (js, css, images)?
I can't find any good way to do it so users could add dependency in pom.xml and get all required files (not only jar-file).
I found WebJars project, but it looks a little compicated and requires additional steps (adding and configuring webjars maven plugin before getting my project).
It would be useful to have another project with similar structure.
Thanks for your help!
OK - if you want to distrubute a webapp as a complete package the most common thing to do is package your application as a WAR.
To do this you just need to add the following to your pom:
<packaging>war</packaging>
Don't forget to ensure that your project conforms to the layout specified by the Maven WAR Plugin.
Once you have your WAR deployed, other WAR projects can depend on it which produce a single WAR that is a combination of the two.
If you need something a little more complicated look at using the Maven Assembly Plugin that allows you to create custom archives. As with the WAR, other assembly projects can depend on it, though as the unpacking is in your hands there is again an increase level of complexity.

Maven/Eclipse plugin: easiest way to have new Maven project have dependency on legacy non-Maven project?

I created a new Maven project in Eclipse. This was working fine until I needed to add a dependency to another Eclipse project, a legacy utility project, which does not have a pom.xml, and does not have the directory structure of a typical Maven project. (It has the directory structure of a typical eclipse Java project). This other project is in the same Eclipse workspace as the Maven project.
In looking at other posts on this, it seems that usually the solution is to build the jar for the other project and install it in Maven. However I am actively modifying code in the utility project while writing code in the Maven project, so I can't just install a jar once to satisfy the dependency.
What is the easiest way to handle this so that I can code simultaneously in both projects, and also get maven to build cleanly? (Of course Eclipse can build just fine with just a project dependency.)
UPDATE
Using the Build Helper plugin to add the utility projects source folder to my pom was a viable path to the solution, but then I needed to update all the dependencies of the utility project into my new Mavne project, which started to make the whole process too time consuming (and also not really the chain of dependencies I wanted). I think that if I would have added all the dependencies, then Build Helper suggestion would have worked.
For now, I built the utility project jar and installed it into maven. Which turned out to be the the quickest solution. I will try to Mavenize the utility project, without modifying its structure (as suggested by FrVaBe below), and see if I can link the poms afterward.
I am going to keep this question open until I have a full solution which can be reported back, since I assume this is a problem others will have (trying to integrate legacy projects with new maven projects).
For the development time you can add the dependency as a System Dependency. It will be resolved by the file path (which can be the path to your utility.jar file under development) in this case.
It is added as describe in the link above, e.g.:
<dependencies>
<dependency>
<groupId>my-utility</groupId>
<artifactId>my-utility</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${somewhere}/lib/my-utility.jar</systemPath>
</dependency>
</dependencies>
The maven handling of System dependencies is sometimes special. E.g. they will not be included in war-packages! Therefore, when you are finished I would strongly recommend to install your utility library to the maven respository or to deploy it to a repository manager (Nexus/Artifactory).
You can add utility project's src folder to your working project in eclipse. For your development purpose.
right click on Working project
go to properties and choose java build path
go to source tab
Add your utility project src folder to that.
Later you can install your jar as maven dependency.

Categories