Jenkins downloading in advance dependencies in maven dependencyManagement section - maven

I have the following maven structure in a multi-module project:
aggregator-level-0
-- module-level-1 (jar)
-- aggregator-level-1
---- module-level-2.1 (jar)
---- module-level-2.2 (jar)
The aggregator-level-0 pom is not only an aggregator but also the parent (inheritance) for the rest of the modules. It includes a DependencyManagement section.
The module-level-1 includes the module-level-2.1 as a runtime dependency, so I have added module-level-2.1 in the DependencyManagement section of the aggregator-level-0 pom.
When I build the project locally (maven 3.0.4), using the -o option and having my m2 local repository empty, everything works like a charm. Maven resolves perfectly the order to build the modules and deploys the artifacts well.
When I do the same in "jenkins" the build fails. It tries to get the module-level-2.1 when the dependency Management section in the parent pom is parsed, and of course it fails because it is not created/published yet.
Is this the expected behavior?
Do Maven and Jenkins get the dependencies at a different time?
Is it a bad practice to put in the dependency management section of a pom artifacts which will be built in a sub-module inheriting from that pom?

Related

Artifactory, Maven and a project with several modules

I have a Maven project with several modules, with a structure similar to:
project
module-1
pom.xml
module-2
pom.xml
..
module-N
pom.xml
pom.xml
I have defined the Artifactory Maven plugin in the parent pom.xml which is under project, like it is done in the examples they offer on their website and I've also tried the example they have in git.
My problem is that I don't want to publish to Artifactory all the artifacts generated by the parent pom, but only those under certain modules, so I tried defining the plugin in the parent pom with the tag <publishArtifacts>false</publishArtifacts> and then defining the plugin again on the modules which contain artifacts I really do want to deploy with <publishArtifacts>true</publishArtifacts>, however no artifact is deployed.
If I try the other way, only specifying I do not want to publish Artifacts on the modules I don't, it does deploy all ignoring that configuration.
How should this be done using this plugin?
You can use the publisher\excludePatterns in the artifactory plugin section of the pom.xml to exclude artifacts from being published.
you can declare multiple patterns with wildcards, and separate each with a comma.
For example, if you are using the sample from
"https://github.com/JFrogDev/project-examples/tree/master/artifactory-maven-plugin-example"
then, setting you're pom with
<excludePatterns>multi3*.war,multi2*.jar</excludePatterns>
would exclude those files from being published to Artifactory.
hope that helps...

How to properly remove a dependency in a Maven project

I have a Java Maven project where I have some dependencies defined in the pom.xml file. Recently I decided to move from Junit to TestNG so I deleted the Junit dependency from my pom.xml and added the TestNG one.
I was expecting to see the Junit jar library disappear from the Maven Dependencies folder as a part of the process but that didn't happen. I can still see the jar file in the dependencies folder and it is still being used by my test cases.
I can see TestNG jar is there as well but it's not being used. I can change it manually of course but that wasn't my intention.
Am I doing something wrong are there any additional steps that I missed that will allow me to remove the old dependency?
The following applies for the global .m2 folder where maven dependencies can be downloaded and is common to your maven projects.
the .m2 folder will not clean by itself as those dependencies could be used in another project so its wrong assumption that replacing a dependency in a a pom file will automatically remove from your repo.
you can look at the depedency plugin and run the following
mvn dependency:purge-local-repository
or
mvn dependency:purge-local-repository -DreResolve=false

how to run only specific maven module

how to run only specific maven module from maven eclipse project.
I have few modules in my EAR maven project and when I build whole project it fails at one module WAR,saying this war artifact module not found in repository.
Your multi module project must e having a common parent. First install that parent pom in your local repository. Now go to child module project and maven build the project.
It should work given that all its dependencies are present.

IntelliJ - How to link modules for maven goal

I have successfully imported a parent pom with child modules into IntelliJ. The child modules have dependencies between themselves and IntelliJ has correctly set up the classpaths so that changes to module A are reflected in module B. However these dependencies are not maintained when I execute a maven goal in IntelliJ (compile, jetty:run etc). Here is my structure:
client_libs
-- servlet-filter
-- filter-example
filter-example depends on servlet-filter. However when I run maven compile on filter-example I get:
The POM for com.cloudseal.client:servlet-filter:jar:1.0-SNAPSHOT is missing, no dependency information available
I can work around this by manually installing servlet-filter into my local repo before I execute a maven goal but obviously this is not ideal. How can I get IntelliJ to maintain the relationships between the modules when I execute a maven goal?
You shall be able to just run mvn package but from place/path where your parent pom is.
If this's not working for you, please post your pom.xml files.

Maven packaging ear finding dependency from project level but not from parent

I've currently got a parent pom that declares two modules: an ear and a war. The ear is reliant on the war (and declares a dependency for it with group/artifact id and packaging type).
When packaging from the parent pom level, the reactor picks up both artifacts and properly packages the war into the ear as you would expect. However, when packaging from the ear's project pom (despite having declared elements in both projects pointing to the parent pom) the ear fails to find the war artifact.
I know that when packaging at the ear level Maven finds its way to the parent correctly, but does it not then iterate down to the various modules that the parent contains to pick up artifacts?
Thanks :)
That's the way Maven works. It's OK. When resolving dependencies, Maven looks for them in reactor, then local repository, then remote repos. So, when doing a build from a parent project level, both projects are in the reactor, so EAR can pick WAR easily. (To be specific, it's not because the parent-child relation, but the fact they are modules.) However, when you build an EAR module in isolation, reactor can't provide WAR as well as local repo as well as remote repos. If you install WAR module into local repo by mvn install and then try to build EAR, WAR artifact will be found using local repo.
Sounds like your parenting structure is broken, if the EAR depends on the WAR then it should be a child module.

Resources