importing maven depedency as pom - java

I need to import a dependency in my project; The problem is that that dependency is specified by a pom in this way:
<dependency>
<groupId>it.xxxx.yyyyy.be.esb</groupId>
<artifactId>CR_XXXXX_BE_PRODO_YYYYYY_V1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
When I add this dependency in my project it compile well, and in my local repository (.m2) I find the folder at the path it.xxxx.yyyyy.be.esb;
But not find any jar inside the folder, so How I can use that dependency?
That dependency should be a client to make soap call to a server!
In other project the dependency was:
<dependency>
<groupId>it.xxxx.yyyyy.be.esb</groupId>
<artifactId>CR_XXXXX_BE_PRODO_YYYYYY_V1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
And when I compile, the jar file is downloaded and i'm able to find it in my local repository (.m2), also I can decompile and see all class inside the package, and I can import in my class;
So my question is, is there a way to use the first dependency (the one with pom as type: pom)?
thanks

In dependencyManagement you can import a "bill of materials" (BOM) that is provided in pom format. Here all the supplied components, their requirements and their versions are included so that you know that you are using all the versions that the BOM provider used.
For example, Spring boot (I know you can do it as a parent pom):
<dependencyManagement>
<dependencies>
<!-- Import dependency management from Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
.
.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</dependency>
</dependencies>
In their BOM, all the versions of the Spring Boot dependencies, including the versions of their plugins, are defined in one place.
Another good example is junit:
<!-- use dependency management to import pom -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
.
.
<dependencies>
<!-- Version is defined in the parent pom -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Related

Automatically import nested BOMs version numbers in java project

I have a parent platform BOM like this:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>platform-bom</artifactId>
<version>7.0.LTS.9</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<!-- Third Party Dependencies Managed Via third-party BOM -->
<dependency>
<groupId>com.company</groupId>
<artifactId>third-party</artifactId>
<version>7.0.LTS.9</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.company</groupId>
<artifactId>api</artifactId>
<version>7.0.LTS.9</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
I am importing it like this in my project:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>platform-bom</artifactId>
<version>7.0.LTS.9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
When I am importing api dependency like below, it automatically pulls the version from the imported BOM.
<dependency>
<groupId>com.company</groupId>
<artifactId>api</artifactId>
</dependency>
However when I try to do the same for the third party bom, it is not able to pull the version from the imported BOM.
<dependency>
<groupId>com.company</groupId>
<artifactId>third-party</artifactId>
<type>pom</pom>
</dependency>
Error I get is: Unresolved dependency: 'com.company:third-party:pom:unknown'. Is there a way to pull the version from the BOM which is imported? If not, I think it defeats the purpose of having a BOM in the first place.

Get NoClassDefFoundError when implementing own jar

I'm trying to implement my own jar (library) in another project I'm working on. But I'm getting the error NoClassDefFoundError when I run the program and try to instantiate a class from the library I built.
I try to add the dependency in different ways, with maven local repo or adding the jar file directly as an external jar in Build Path. I don't get any warning/error in eclipse and I can build and install the app with maven successfully. But when I execute the part of the code that use a class from that library I get NoClassDefFoundError.
My library groupId is com.mycompany.myapp, and the classes I'm using are in the package com.mycompany.myapp.business. I'm try to move the class to the parent package just in case but I'm always getting the same error.
1 - Do I need to define which classes need to be available when importing the jar?
2- Do I need to compile the library in an specific way apart from mvn clan package/install?
3 - Do I need to add the same dependencies I have in my library in my parent project to have everything working?
This is my children 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>com.mycompany.mylibrary</groupId>
<artifactId>mylibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>javax.xml.rpc</groupId>
<artifactId>javax.xml.rpc-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.47</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.47</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.xml.rpc</groupId>
<artifactId>javax.xml.rpc-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.0-M1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.5.0-M1</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.3.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</project>
And then in my main project I instantiate the class in the following way:
Init oInit = new Init();
Where Init is a public class defined in com.mycompany.mylibrary.business package.
java.lang.NoClassDefFoundError: com/mycompany/mylibrary/business/Init
pom.xml from the main proyect:
<dependency>
<groupId>com.mycompany.mylibrary</groupId>
<artifactId>mylibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
In the parent project, in the libraries imported by maven I see that my library has a folder icon instead of the one the other have.
You can use the two ways to include a library into your project:
Including the library directly into your project classpath
Through Maven
When using the Maven option, you must be sure that the library is well exported to the Maven repository, local or remote (such as Nexus). The first place Maven will search when looking for a library defined in the pom.xml is on your local repository (located at ~/.m2). The it will look into the Central Maven Repository (located at https://mvnrepository.com/repos/central). In between, you can declare your own repositories into your pom.xml as
<repositories>
<repository>
<id>my-repo</id>
<name>My Maven Repository</name>
<url>${nexus.repositories.url}</url>
</repository>
<repositories>
How to build the library to be included into your own local or remote repository? Just be sure to include the install goal when compiling with Maven, and also the repository definition into its pom.xml

How to get dependency dependencies?

I created a project that includes multiple Java projects, each one has its own dependencies.
One project is the main project and uses all other projects, but not directly. For example :
Assume that I have three project's A, B and C, A is the main one (the application). A uses B as dependency while B uses C as dependency, project C has its own external dependencies.
Each person of my team has a permission to download only one project, and will use the others as dependencies as needed.
The problem appears on the team that developing main project, Maven did not download B & C dependencies to their local machine.
These are the pom files for each one:
A(main project)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.a.c</groupId>
<artifactId>D</artifactId>
<version>1.0.0</version>
<classifier>jar-with-dependencies</classifier>
<exclusions>
<exclusion>
<artifactId>commons-io</artifactId>
<groupId>commons-io</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.a.c</groupId>
<artifactId>B</artifactId>
<version>0.0.1</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
</dependencies>
</dependencyManagement>
B:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.a.c</groupId>
<artifactId>C</artifactId>
<version>0.0.1</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
</dependencies>
</dependencyManagement>
C:
<dependencies>
<dependency>
<groupId>org.jsystemtest</groupId>
<artifactId>jsystemApp</artifactId>
<version>6.1.01</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.50.0</version>
</dependency>
</dependencies>
Which configuration should I set in order to get the dependencies of B and C without downloading the source code for each machine?
You seem to confuse <dependencyManagement> with <dependencies>. <dependencyManagement> in A and B only sets the version of dependencies without including them. This is why Maven will not download the dependencies of B and thus not of C (except if there is an additional <dependency> section in both A and B which you didn't show).

Maven not downloading any dependencies of type pom

I am in the process of upgrading form Apache CXF 2.7.5 to 3.0.2 but I am having the problem below.
When I am including the dependency
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf</artifactId>
<version>3.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
to my pom.xml I don't see any .jar files downloaded to my project. There are no errors on the project.
I am using Maven 3.2.3 with Eclipse Juno Release2 and m2e 1.3.1
Why the <type>pom</type> is not getting resolved to the respective jar files of the framework?
Isn't the <type>pom</type> dependencies suppose to give the respective libs automatically? And if not what is the benefit of using them?
UPDATE
This is how my pom.xml look like
<?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.test.sysman</groupId>
<artifactId>serviceWrapper</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>serviceWrapper Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf</artifactId>
<version>3.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf</artifactId>
</dependency>
</dependencies>
<build>
<finalName>serviceWrapper</finalName>
<pluginManagement>
<plugins></plugins>
</pluginManagement>
<plugins></plugins>
</build>
</project>
Thanks
Your dependency scope is import. This means it must be part of a dependency management declaration.
With dependency management you are not directly download dependencies. It is a way to ensure that specific versions of dependencies are required. So further down your pom in the dependencies part if you declare
<groupId>org.apache.cxf</groupId>
<artifactId>cxf</artifactId>
without specifying the version, maven knows which artifacts to download. Also controls versions in transitive dependencies.
Your pom probably refers to a bill of materials (BOM)
For more details see http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management
Where do you include this statement?
<scope>import</scope> is only effective inside the <dependencyManagement> section, i.e.
<dependencyManagement>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf</artifactId>
<version>3.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement>
Also, it does not download dependency per se, but instead expands referenced pom in place.
If you just need the pom, exclude <scope>import</scope>.

Missing artifact javax.transaction:jta:jar:1.0.1B ( Issue was different as you may see the resolution is different)

I am trying to learn Hibernate-Spring-Struts using the example Struts 2 + Spring + Hibernate integration example.
But after creating the pom.xml getting this error :
Missing artifact javax.transaction:jta:jar:1.0.1B
I made a progress only up to creating the pom.xml file and made the changes to include most recent libraries.
Here is my 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>S3HMaven</groupId>
<artifactId>S3HMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>S3HMaven</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.0.1B</version>
</dependency>
<!-- Struts 2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.1.8</version>
</dependency>
<!-- Struts 2 + Spring plugins -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.15.2</version>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>2.5.6</version>
</dependency>
<!-- Hibernate core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.7.ga</version>
</dependency>
<!-- Hibernate core library dependency start -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<!-- Hibernate core library dependency end -->
<!-- Hibernate query library dependency start -->
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<!-- Hibernate query library dependency end -->
</dependencies>
</project>
I tried with and without dependency for javax.transation. Did not make difference. Can any one tell me what am I doing wrong ? What should I do to get rid of it?
In my case i tried example from mkyong
jsf-2.0 spring hibernate integration example
When i got the exception i searched alot i was using spring sts suit tool , and eclipse MARS with JDK 8 the solution was
I changed pom to 1.1 instead of 1.0.1B
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
And if it could not be downloaded automatically from m2 repo you should download it manualy (you can check this in the repo in folder C:\Users\pc1\.m2\repository\javax\transaction\jta\1.1\jta-1.1.jar )
and check the maven dependency in project properties it should not give you an error in the lib tab.
References :
reference 1
reference 2
reference 3
reference 4
reference 5
Related issues might appear after you solve this if you are applying the tutorial:
1-http://www.mkyong.com/web-development/the-web-xml-deployment-descriptor-examples/
2-http://jonathan.lalou.free.fr/?p=2026
3-Error creating bean with name 'sessionFactory' Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
Tomcat error: Not Found in ExternalContext as a Resource
The error in your pom.xml because you mess up different versions of Struts core and plugins.
Change
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.15.2</version>
</dependency>
I don't know why do you need JTA 1.0.1B but you could change hibernate to 3.3.2 (at least, without headaches)
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.3.2.ga</version>
</dependency>
it has a recommended dependency for JTA 1.1.
Create a new project from pom.xml then add source files to it.
Which repository are you using?
Add the java.net Maven repository as below.
<repository>
<id>java.net</id>
<url>http://download.java.net/maven/2/</url>
</repository>
This repo worked for me:
<repository>
<id>webpublico-repository</id>
<name>Webpublico Nexus Repository</name>
<url>http://repository.webpublico.com.br/repository/maven-public/</url>
</repository>
some approach from Unable to resolve javax.transaction dependency
clear the .ivy/cache folder. it works, but takes very long time.
At the moment, it may be unnecessary. But I think we should update this ticket for someone.
It works for me. Change porm.xml
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.16</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.3.Final</version>
</dependency>

Categories

Resources