Where to use which scope in maven pom.xml - maven

I have a project "framework-ws" that contains 3 modules (api, dictionary, webapp).
This project is used has a parent project for multiples other web-services project.
For example, the project "core-ws" has "framework-ws" for parent. It also contains 3 modules (api, dictionary, webapp).
Each of these modules has the framework as dependency.
core-ws-api => framework-ws-api
core-ws-dictionary => framework-ws-dictionary
core-ws-webapp => framework-ws-webapp
(subproject => dependency)
Now I have other dependencies (lombok for example) that is used in every project (framework + child-project).
I don't understand where I need the declare this dependency.
In the parent project with a "provided" scope, and in each
child-project without scope
In the parent project with no scope, and no dependency in each
child-project
Another solution that I didn't think of
The second solution seems cleaner because I don't need to duplicate the dependencies in each pom.xml. But I don't know if it's the best practice.
EDIT : Here is a picture of projects structure.

In your parent pom use a dependency management section see https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</dependencyManagement>
Then in your child pom you will use the dependency but not specify a version, i.e. the version will be specified in the parent once for all children. You still need to include the dependency in each module.
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>
In terms of scope read this https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope the default scope is compile which is what you will usually use. I think you are confused about the meaning of scope from readinf your question.

Related

Adding a dependency existing internally as a dependency

My project is a fairly large project consisting of many maven modules (but not microservices). I was trying to do Moving from spring to spring-bom on WAS but seems lot of clashes in versions. So for example one of my modules is using commons-collectionsversion 2.6.0 and my current project is using 3.2.2. I want the same jar to be used across. Since its more of a migration project I cannot do changes in container or repository changes at this time. I should only make sure that all the version are compatible with each other. My plan :
I want to include a dependency which is with in some other dependency
into the current pom as a dependency.
Also I want other jars in this pom (which exists as a dependency) to included the dependency
Is there anyway to do it?
I didn't completely understand your question, but the can help you to define a cross-module dependency version, as long as you place it in the parent-pom file.
<dependencyManagement>
<dependency>
<groupId>com.group</groupId>
<artifactId>project-1</artifactId>
<version>1.0.0</version>
</dependency>
</dependencyManagement>
and then define the dependency in the relevant module without providing it a version (it will be inherited from the parent-pom's <dependencyManagment> tag:
<dependencies>
<dependency>
<groupId>com.group</groupId>
<artifactId>project-1</artifactId>
</dependency>
</dependencies>

Maven: using a dependency version as a property across different modules

I have got a project with a number of modules in it, now, e.g. I have a module M1 and its version 1.0.0 and in the same project I have got 8 other modules in which I have M1 as a dependency.
The problem is when I make a change in M1, update version to 1.0.1 and upload it to central repo, I have to change that version number 8 times.
I want to use this version number as property, which I have tried to put it as as a prop in parent pom but a maven warning saying its not right to do...
any thoughts? thanks in advance
Don't define the version as a property.
Adjust the <dependencyManagement> section in the parent pom like below:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.yourGroup</groupId>
<artifactId>M1</artifactId>
<version>1.0.1</version>
</dependency>
<!-- eventually more dependencies -->
</dependencies>
</dependencyManagement>
In the 8 modules which depend on M1, insert:
<dependency>
<groupId>com.yourGroup</groupId>
<artifactId>M1</artifactId>
</dependency>
Take a look at the POM Reference - Dependency Management for further knowledge

locking snapshot versions when depending on multi-module project

I have a multi-module project (B) that depends on various modules of another multi-module project (A). Each module of (B) might depend on different parts of (A), but they should all use the same version of (A). Current that version is specified as a property in the parent pom of (B).
I would like be able to lock the version used to a specific build (x.y.z-ts-buildnum) from a single command while keeping the version of (B) to use listed only once.
In parent:
<dep.version>4.1.0-SNAPSHOT</dep.version>
In a module:
<dependency>
<groupId>com.example</groupId>
<artifactId>B-foo</artifactId>
<version>${dep.version}</version>
</dependency>
I tried:
versions:lock-snapshots but that does not seem to lock versions set by properties
versions:update-properties which is for updating property versions but not locking snapshots
Writing a script around resolving the latests snapshot of A's pom from the local repository, but that does not work because each module has it's own timestamp. I could write a script to look at every module, but that's getting close to the amount of work a maven plugin would do.
I would suggest to define the dependencies to your module-A in your parent of the module-b like this:
<properties>
<module-a.version>6.5.0-12-SNAPSHOT</module-b.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>A-foo</artifactId>
<version>${module-a.version}</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>A-bar</artifactId>
<version>${module-a.version}</version>
</dependency>
...
</dependencies>
</dependencyManagement>
Within your modules you now only need to write:
<dependency>
<groupId>com.example</groupId>
<artifactId>A-bar</artifactId>
</dependency>
which will give you the possibility to change the version of all modules only within a single place. At the root of your module-A.

Prevent maven to copy dependencies to WEB-INF\lib folder

I have this dependency in the pom.xml:
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
<version>${appengine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>${appengine.version}</version>
<scope>test</scope>
</dependency>
However although the scope is "test" the jars are still copied to the WEB-INF\lib folder:
src\main\webapp\WEB-INF\lib
I see 2 possible scenarios here.
1) These dependencies are somewhere referenced (directly or not) with compile or runtime scope besides their test-scoped use. Then, this wider scope is effectively used to satisfy all the needs about them.
2) You do (maybe unconsciously) some dependency processing hacking using Maven Dependency Plugin or stuff like that that do this mess.
And one issue at the end: I assume that this src/main/webapp/WEB-INF/lib directory is in fact empty (possibly doesn't even exist) and it's your mistake. As #Ryan said in his comment, all the libs (and any other build artifact) should land at target directory.

Adding POM type dependency using m2eclipse, unable to resolve

I am trying to add Hector dependencies to my POM. My IDE is Eclipse, and I am also using m2eclipse. Adding dependencies of JAR type is not a problem, but this dependency is of type POM. I have tried almost everything usual including cleaning, building, and using import scope but nothing seem to have helped. When I try to add import me.prettyprint.hector.api.Serializer;
I get the error "The import cannot be resolved".
Is there anything else I need to do to use POM type dependencies or is there a better way of using dependencies of POM types in the project?
I believe his question is not as obvious as simply including the necessary dependency. I have experienced this problem too and am looking for a solution. The problem can be clearer stated as the following:
Let's say I have two maven projects (project A and project B). Project A is a simple web-app which wants to include dependencies as stated in project B. However, project B packaging type is "pom". This should allow all of project B's dependencies to be included into project A. Here is an example:
Project A (packaging is "war"):
<dependencies>
<dependency>
<groupId>com.foo</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
<type>pom</type>
</dependency>
</dependencies>
Project B (packaging is "pom")
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
What we'd like to see in Eclipse is when you run maven eclipse:eclipse on Project A, that you can see the commons-lang-2.4.jar file as a dependency under project A such that you can resolve it in your code when imported. This is not happening and I'm still looking for such a solution.
The error indicates that the relevant class is missing in your classpath. A search of this class indicates, it is available in hector-core
This discussion indicates how this dependency can be imported, viz. adding the following entry to your project pom (or choosing this appropriately in m2eclipse).
<dependency>
<groupId>me.prettyprint</groupId>
<artifactId>hector-core</artifactId>
<version>0.7.0-29</version>
</dependency>

Resources