Java Maven pom.xml - dependencies issue - pom.xml

I have a J2EE web application setup using maven build based project.
i have pom.xml, where i have dependencies for my application.
Let's say my App only needed example hadoop-common.jar, log4j.jar becuase of pom.xml which downloads its dependencies jar.
Process downloads all jars in to .m2/repository - locally.
When I bundle war the then WEB-INF/lib has many jars along with hadoop-common.jar, log4j.jar.
How do i ensure only hadoop-common.jar, log4j.jar to be included as part of my war not its dependency in the myWebApp.war
<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>it.cvc.pcam_team</groupId>
<artifactId>pcamapplication</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>PCAMApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
...

Are these jars added along with the adoop-common? If hadoop-common is dependent on other jars, these may be added transitively. You can exclude specific jars from being included in that way.
https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html
Depending on what you're excluding hadoop-common could stop working though, if you exclude something that's needed for it to run.

Related

adding a "outdated " maven dependency from different url (repository)

let say we would like to add this maven dependency in our POM.xml file from http://maven.jahia.org/maven2/ to a exiting POM file. What are some good(industry) practices of adding com.sun(or tools) dependency super pom ? new to maven, example or short reasonings are appreciated.
<!-- https://mvnrepository.com/artifact/com.sun/tools -->
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
</dependency>
<!-- Note: (http://maven.jahia.org/maven2/) -->
The existing POM file 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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>example-common</artifactId>
<packaging>jar</packaging>
<description>example</description>
<properties>
<example-version>7.2.0</example-version>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-sdk</artifactId>
<version>${example-version}</version>
</dependency>
<dependencies>
</project>
This is a bit of an old question, but I had the same issue, and this worked.
Add into your project pom (also possible in maven settings if you need it for several projects), the Jahia repo.
<project>
...
<repositories>
<repository>
<id>jahia</id>
<name>jahia</name>
<url>http://maven.jahia.org/maven2/</url>
</repository>
</repositories>
...
</project>
If it's in the maven settings, then you put it inside of a profile but need to activate the profile when running maven (-Pprofilename) or via activation by default in settings.xml:
<profile>
...
<activation>
<activeByDefault>true
...
</profile>

In multimodule maven project, how to execute proper goal for each module?

I have three maven projects in my prototype:
bookman (main project)
|-- bookman-back-lend (module, simple service app)
\-- bookman-front-web (module, simple web app)
This is very simple example book lending app (for library or something) to learn various technologies and all of that.
Problem is that I can't make parent pom to execute goals in module poms.
Calling mvn clean package wildfly:deploy in any module individually works without problem. It compiles, deploys, war is replaced, Wildfly 8 does its thing, etc. But if I call parent POM with mvn clean package (I'm not even sure what to call...), it doesn't do much - certainly it does not deploy to wildfly any of modules. Wildfly does not budge. In parent POM, calling mvn clean package wildfly:deploy does not work, of course.
Here is parent pom.xml:
<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>org.mader.bookman</groupId>
<artifactId>bookman</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Main BookMan superproject.</name>
<description>Main BookMan superproject.</description>
<modules>
<module>bookman-front-web</module>
<module>bookman-back-lend</module>
</modules>
</project>
And module POMs, with some stuff cut out for brevity (I assume they aren't related to my problem).
bookman-front-web/pom.xml:
<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>
<artifactId>bookman-front-web</artifactId>
<packaging>war</packaging>
<name>Frontend project - webpage.</name>
<description>Web page to handle lending books.</description>
<parent>
<groupId>org.mader.bookman</groupId>
<artifactId>bookman</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
...
</properties>
<repositories>
...
</repositories>
<dependencyManagement>
...
</dependencyManagement>
<dependencies>
...
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>clean package wildfly:deploy</defaultGoal>
<plugins>
<plugin> <!-- To use, run: mvn clean package wildfly:deploy -->
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.maven.plugin}</version>
</plugin>
</plugins>
</build>
</project>
And bookman-back-lend/pom.xml:
<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>
<artifactId>bookman-back-lend</artifactId>
<packaging>war</packaging>
<name>Backend project - lending.</name>
<description>Business logic to handle books, users and act of lending.</description>
<parent>
<groupId>org.mader.bookman</groupId>
<artifactId>bookman</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
...
</properties>
<repositories>
...
</repositories>
<dependencyManagement>
...
</dependencyManagement>
<dependencies>
...
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>clean package wildfly:deploy</defaultGoal>
<plugins>
<plugin> <!-- To use, run: mvn clean package wildfly:deploy -->
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.maven.plugin}</version>
</plugin>
</plugins>
</build>
</project>
It should be possible to have customized goals to execute for each module individually, right? Right? Otherwise those aggregate POMs are rather useless.
Unfortunately, questions like this suggest that maven goal executed on parent POM is exactly same goal to execute on child POMs. Who thinks up something like that? What if I need completely different goals in each child module?
All answers I found are few years old, maybe... just maybe... sanity prevailed and maven now allows something like that? After all, all information neccessary to do this should be accessible, like defaultGoal tag.
Problem was solved in radical way. Maven is dead, long live Gradle.
I was tinkering with it last few days and I already moved prototype to point where it actually works. While there is still tons of work left, I already know I will not be looking back.
So, yeah... if you have requirements that cannot be done easily or at all with Maven for various reasons, then consider Gradle. In fact, consider Gradle anyway.

Updating versions in multi-module Maven project

I am trying to release our multi-module Maven project and I can't figure out how to properly update the version numbers in our POMs.
The maven release plugin doesn't update versions in the parent POM dependencyManagement section - that seems to be a known bug.
I tried to use the versions plugin, but I have two issues with that:
the plugin only updates my toplevel POM; probably because the multi-module project POM is not the paremt POM of the modules's POMs
more significantly, how can I mimic the behavior of the release plugin to automatically compute the versions to set (e.g. 1.0.0) from the current version in the POMs (e.g. 1.0.0-SNAPSHOT)... without giong into perl scripting?
Any suggestion that doesn't involve scripting, and doesn't require me to declare dependency versions outside of dependencyManagement would be very appreciated!
Here are the 4 POMs (module POMs in their module subdirectory):
============ pom-parent.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>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<version>2.5</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
============= multi-module pom: 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>
<groupId>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<relativePath>pom-parent.xml</relativePath>
<version>2.5</version>
</parent>
<groupId>com.acme</groupId>
<artifactId>multi-module</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
<scm>
<developerConnection>scm:svn:https://svnserver.fairisaac.com:8443/nowhere</developerConnection>
</scm>
</project>
============== module1 POM: module1/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>
<groupId>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<relativePath>../pom-parent.xml</relativePath>
<version>2.5</version>
</parent>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
============= Module 2 POM: module2/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>
<groupId>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<relativePath>../pom-parent.xml</relativePath>
<version>2.5</version>
</parent>
<groupId>com.acme</groupId>
<artifactId>module2</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
</dependency>
</dependencies>
</project>
========= Edit ==============
... and it gets better, if I replace the explicit version in the dependencyManagement section with
<version>${project.version}</version>
... maven 3.0.4 fails with an NPE:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.478s
[INFO] Finished at: Fri Apr 12 12:09:08 CEST 2013
[INFO] Final Memory: 9M/120M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.0:prepare (default-cli) on project multi-
module: Execution default-cli of goal org.apache.maven.plugins:maven-release-plugin:2.0:prepare failed. NullPointerExcep
tion -> [Help 1]
This is not a bug, the first pom.xml you showed is outside your multi-module project. The maven-release-plugin will consider it an external dependency, thus it won't change the version.
But there is a flaw in your design, you have to move the
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
to your multi-module pom.
I'm assuming your idea is to declare the module version in one place and share it among all modules. So, the module version goes into the multi-module pom dependencyManagement.
Usually external poms like this are used to put company information, repository paths, things like this, and they are shared by many different projects.

Sonar and MAVEN POM file with Dependencies

I have setup Maven and Sonar to analyze .NET projects and it works fine for winforms projects. However when I add an MVC project it gives a build Failure because it can't find the System.Web.MVC.dll file. I have an Fx-Cop plugin that runs as part of Sonar.
One way to get around this would be to package the dll, copy local= true with the project. However I dont want to do that(any thing wrong with packaging the MVC file along?)
How do I add a dependency to the MVC dll? I followed this example and added it like this:
<dependencies>
<dependency>
<groupId>artifact_group_id</groupId>
<artifactId>System.WEb.MVC</artifactId>
<version>4.0.30319</version>
<type>library</type>
<scope>system</scope>
<systemPath>C:\DOTNET\DLLS\System.Web.Mvc.dll</systemPath>
</dependency>
</dependencies>
Still I get a Build Failure caused by FX-Cop. Looking at the FX-Cop logs I get a message as below:
The following error was encountered while reading module 'MyTestMvcApp': Assembly reference cannot be resolved: System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
I'm using apache-maven-3.0.2 and sonar-3.2. Can anyone help with this?
This is the full POM.XML file
<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>Test</groupId>
<artifactId>Sonar.For.NET.Test</artifactId>
<version>1.0</version>
<name>Testing </name>
<packaging>netpack</packaging>
<properties>
<visual.studio.solution>TestProjectsForSonarBuild.sln</visual.studio.solution>
<visual.test.project.pattern>*.Tests;*Test</visual.test.project.pattern>
<dotnet.tool.version>4.0</dotnet.tool.version>
<sonar.language>cs</sonar.language>
</properties>
<dependencies>
<dependency>
<groupId>artifact_group_id</groupId>
<artifactId>System.WEb.MVC</artifactId>
<version>4.0.30319</version>
<type>library</type>
<scope>system</scope>
<systemPath>C:\DOTNET\DLLS\System.Web.Mvc.dll</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.sonar-plugins.dotnet</groupId>
<artifactId>maven-dotnet-plugin</artifactId>
<version>0.6</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<configuration>
<toolVersion>3.5</toolVersion>
<buildConfigurations>Debug</buildConfigurations>
<rebuild>true</rebuild>
<language>cs</language>
</configuration>
</plugin>
</plugins>
</build>
</project>
Thanks for your time.
You can use the "sonar.fxcop.assemblyDependencyDirectories" property as described on the documentation page for the FxCop plugin.
So you have 2 solutions from there:
either your build process copies "System.Web.MVC.dll" in a temp folder, which you reference through the this property
or you directly reference an absolute path where this DLL can be found (but your property may then be not portable accros different environments/servers)

how to copy 3rd party jars in WEB-INF/lib of war

I have created dynamic web project in eclipse.I have developed some code which requires
dependeny jars mentioned in POM as well as some 3rd party jars;so i added all jars in the
build path of the project.these jars are required to compile and run the code when we
deploy in the tomcat.
below pom.xml is used to build the war.I want to include these 3rd party jars in WEB-
INF/lib folder in the war.what changes do i need to make in pom so these jars will come in WEB-INF/lib folder?
Is there any way like whatever jars are added in the project build path does comes in WEB-
INF/lib folder.
or else
put all 3rd party jars in temp folder.then using copy command can we copy these jars from
this temp folder to WEB-INF/lib folder.what is the best approach?
<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>com.sample</groupId>
<artifactId>SampleWS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Simple CXF project using spring configuration</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
If you just want to add 3rd party jars in WEB-INF/lib folder, no changes needs to be made to POM.xml. Just do the following Project > Properties > Java Build Path > Libraries > Click on Add JARs....locate JARs in your you project's lib folder.. Now these JARs will be added to war along with those in POM.xml.
P.S: Personally i won't recommend to add jars partially from POM and partially from lib folder.

Resources