how to configure dependencies in maven pom.xml? - java

I am converting my simple working web application (runs from eclipse) to a maven project. This is my first maven project. I have 3 external jar dependencies to it and i added then to the pom.xml my build is failing saying that it can't find those 3 dependencies. here my pom.xml file
The build is saying that the 3 packages does not exist.
package javax.servlet does not exist
package org.apache.commons.codec.binary does not exist
package org.apache.commons.configuration does not exist
What i am missing? I am running my application from Mac and in the .m2/repository i see these 3 libraries/packages present.

You need to either remove the dependencyManagement tags that surround dependencies or add the dependencies again but nested within the project tag, i.e. at the same level as dependencyManagement.
dependencyManagement allows you to fix information about dependencies across a multi-module project - e.g. like version numbers - however you still need to provide a dependencies section alongside that so that Maven knows to include them.
If your project is not a multi-module project I would be tempted to not use a dependencyManagement section at all.

Just remove dependecyManagement tags

Related

Use maven local module in Intellij

I currently have the following setup:
parent-pom:
has our common as dependency
service-pom:
is child of parent-pom
contains purge for common, so its always the newest
If I now import the modules in Intellij, the common dependency is used from .m2 folder. The problem is that I cant do refactoring across modules or add something inside a class, I don't have autocomplete.
I tried then changing the project structure. First I've added my local common module as dependency for the service and placed it over the .m2/repository dependency. That worked for autocompletion and refactoring, but can get confusing if I want to use the .m2/repository version.
But compiling and starting spring-boot with it don't works. I added a field to a class from common and referenced it in the service. And when I compiled it, it failed due to this field.
Then I read that I need to configure an artifact (containing common) and added a run configuration and enabled "resolve workspace artifacts". But that also did not work as expected.
Then I've added my local common module to the parent-pom module and added the parent-pom module to the service. In addition I've configured an artifact for the parent-pom.
But that didn't work either.
How can I compile and start my service with the local version of my common (and also be able to use the .m2 if needed)
How do you declare the dependencies between these modules in maven?
Do you have these modules imported into the same IDE project as Maven projects?
For IDE to resolve dependencies to the Maven modules with sources instead of the jars from the local repository, these requirements should be met:
Those Maven modules with sources which you have on your local machine must be added to the same IDE project;
The Maven coordinates (groupId, artifactId, versionId) of these dependencies must match to coordinates of maven modules with sources.
To be able to switch to a local .m2 dependency instead of modules, I think a Maven profile may be used indeed.

Intelij, Spring - Multi module project does not detect any dependencies when using classpath of parent module

I have a multi-module project. For simplicity, lets say there is a parent pom, and one child pom. I have all my dependencies in <DependencyManagement> in the parent pom, and then all of them in the child pom as well. Building the project with maven gives no issues. When running the project, while editing the configuration of the project and setting Use Classpath of module: child module, works fine. But when setting the Use Classpath of module: to parent module (that was the default), intelij gives errors for every dependency. For every dependency it says that package does not exist.
Error:(21, 27) java: package com.squareup.okhttp does not exist
even though I have okhttp in the dependencies, I use okhhtp and it works when using classpath of module : child. And i can even see that intelij detects the okhttp library because I can use CTRL+leftClick to navigate to the library, but when clicking run, I still get the error. Why is this happening, this error is given for every single dependency I have. If project structure or pom details are required, please comment and I will provide as much info as I can. Thank you!

How to build packaged pom as jar

I've got maven project which contains 4 modules and "root". I've compiled all of modules but I have got a problem with main project. I must compile it as jar but it's not possible because error log tells me it must have be packaged as pom. It's possible to compile it as jar?
Here is my poms:
fabe-core (root): https://hastebin.com/goyuduvifu.xml
fabe-rp: https://hastebin.com/folezuhuqe.xml
fabe-api: https://hastebin.com/oveyucuvoj.xml
fabe-brooms: https://hastebin.com/umimojoley.xml
As far as I can tell you are trying to set up a Multi-Module project and want to do the main module and the parent pom in one pom file, as far as I know this is not possible.
I would suggest you read the instructions to a multi module project which is documented here https://books.sonatype.com/mvnex-book/reference/multimodule.html and adjust your project accordingly.

Package Hibernate does not exist when the package is added

Why do I get an error? When I run Maven compile I get errors package org.hibernate does not exist, but I add Hibernate to lib. errors
Defining in your project a lib directory and adding it in the classpath within your editor is not really how Maven works. Take a look at the Dependency Mechanism in Maven. In short you have to define the dependency coordinates in the Maven POM file and Maven will take care of the rest.

Use classpath of multiple modules in IntelliJ IDEA

I have a Maven project, imported from Eclipse, where the dependencies are set to scope provided. When the project is deployed, the jars are deployed as well so that works fine.
While developing, however, I use a "debugging project" that calls the Maven project, and when it runs I get a bunch of Class Not Found errors when the Maven dependencies are set to provided.
If I change the scope of the Maven dependencies to Compile then the project works fine.
If I change the scope of the dependencies to compile, would that change the output of the project? i.e. add a bunch of jars? That would be undesirable.
I also tried to change the Debug Configuration settings and specified the Maven project in "Use classpath of module", but then the files of the debugging project are not found.
How can I specify the classpath to be of both the Maven project and the debugging project, so that classes from both projects including the dependencies will be on the classpath?
Thanks!
There are 3 types of dependency scope: compile, test, and provided,
compile: the dependency library will be used in all steps: compile , test and run,
test: the dependency library will only be used in the test
provided: the dependency library will only be used in compile and test, but in the run time, the dependency library must be provided by the container otherwise it will throw class no find issues.
Your issues is that you did not provide the dependency library in the run environment ( container) when running your project.
hope this can help you
How did you import the project to Idea? If the project is opened as a Maven projects, it should work out of box.
Can you try to open the project by selecting pom.xml?

Categories

Resources