Why maven is not finding a package located in another module/project? - java

I'm trying to compile a multi-module maven project. I have problems to compile a new module I added. This problem arises because the new module is trying to import a pair of packages which exist in another module.
This is the pom.xml of the new module:
Here is the view in the eclipse's project explorer, where i highlighted the 2 packages I'm importing from the class ServicioJMS of the new module:
And here's the error showed in the command line after doing a mvn clean package
As you can see below I added the module in the parent pom.xml:
So, I don't know what I'm doing wrong here ¿Any ideas?
NOTE: Eclipse has no problem resolving the dependencies related to the discussed imports.

I had a similar problem. Go to the library on which you are depending (under your ~/.m2/repository directory or in the ./target directory of the library) and find the compiled library jar. Use jar -tf to list it and check that the missing classes are listed directly in the jar.
If you are using the spring boot maven plugin or similar in the library on which you are depending then it constructs a jar with a radically different structure and the classes, while being in there, will be hidden within the structure rather than being directly in the jar by name. Eclipse understands this, but the spring boot loader won't unpack the spring boot maven plugin structure of a library within a dependency, it will only do that for the runnable jar being executed.
If this is true for you then you can remove the offending build plugin from the pom of your library making it just a library, not a runnable spring boot jar.

A few things:
1) Don't put a version or group if you have a parent, let them be picked up from the parent.
2) When importing a dependency from a module under the same parent use ${project.version}
here's an example:
<parent>
<artifactId>parent</artifactId>
<groupId>com.essexboy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>web</artifactId>
<dependencies>
<dependency>
<groupId>com.essexboy</groupId>
<artifactId>library</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

Use install instead of package because IntegradorTransformador artifact (jar) was created but not exists in m2 repository and IntegradorTransformadorReproceso project can't found the artifact
mvn clean install

Related

Maven project architecture

I am trying to add the locally created project which contains JARs added using java build path to my maven project as dependency and the JARs used in the dependency project are also be useful in the main project. how to deal in this scenario if I don't want to convert my dependency project to maven?
You have different possibilities:
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>2.53.1</version>
<scope>system</scope>
<systemPath>
C:/.../your-jar.jar
</systemPath>
</dependency>
or you can install it in your local .m2 repo
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
This doesn't force you in any way to convert your project into a maven project, but let you reuse the jar you already have.
This is similar like Maven multimodule project. In that all the submodules gets added to the root project as a jar after successful compile. In your case try covert the project into jar and copy the same jar file to the lib folder of parent project.

Easily add libraries to Eclipse Java Oxygen

I know there are post about JAVA libraries in Eclipse and I used a way to add some .jar files (like apache.poi and jsoup) to my project. My question is:
Where do we have to put those libraries and what settings do we have to change, so to make the libraries available for every future project we start ?
Thank you!
The most common tools for bringing dependencies into a Java project are Maven, Gradle, and Ant. I'll focus on Maven, as I estimate it to be the most popular of the three (though I have come to prefer Gradle).
I'm going to assume you have the following prerequisites installed on your machine and each of the developers on your team:
JDK 1.7 or above (in other words, Java)
A Java-focused IDE like IntelliJ or Eclipse
Here's what you need to do:
You and your team members will install Maven
You will create a pom.xml for each new project.
You will specify the dependencies for the project (like jsoup) in your pom.xml
Here's what Maven will do for you:
It will download all the dependencies you specify from an online repository like https://mvnrepository.com/
It will cache the dependencies in a directory in your user home folder called .m2 so that it doesn't need to download dependencies more than once per project
It will resolve the dependencies within each of your dependencies and avoid putting the same dependency on the classpath twice
The minimum pom.xml file you need is as follows:
<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.yourcompany</groupId>
<artifactId>your-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
</dependencies>
</project>
Copy that into a file called pom.xml in your root project directory. You can add any additional dependencies in the <dependencies> section of the XML file. By convention, all of your classes and code should go into the directory structure src/main/java/.
Any Java IDE you use will know how to open a project from a pom.xml file. In IntelliJ, you select File->New->Project from existing sources, navigate to your project's root directory, select your pom.xml, and click Open. IntelliJ will then open your project as a Maven project, read your pom.xml, and make all of your dependencies indexed and available for code completion and compilation.
Hope this helps

How Maven looks for dependencies

I have read many documentation and tutorial about getting started with Maven. But few things are still not clear to me :
1) When pom.xml contains <dependency>, Maven will put that artifact/JAr in Maven local Repository. Right ?
2) Suppose, I have sub modules in my MAven project. Then what is the correct way to build the project ? one should start from parent module and then go to further sub modules. right ? and that way maven will create dependency jars and put them in local Maven Repo folder ?
3) For example, if my sub module pom.xml contains following dependency. How should I provide it (vd-ps.jar) to Maven so that Maven finds it when I do Maven clean install on Submodule/pom.xml? I mean to say what does Maven require or from where does it generate such user defined vd-ps.jar ? from compiled class files ?
<dependencies>
<dependency>
<groupId>com.b.t</groupId>
<artifactId>vd-ps</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Ok, let me ask directly what I want to achieve: I have following pom.xml. and from it I would like to create a separate maven project (Say PTest) that uses dependencies (jars) of another big multi module maven project. Here :- I have a module named v-parent in my multi module project which has pom.xml in its trunk folder.
<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>
<parent>
<groupId>com.b.t</groupId>
<artifactId>v-parent</artifactId>
<version>3.9.0</version>
</parent>
<groupId>com.b.t.test</groupId>
<artifactId>p_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ptesting</name>
<dependencies>
<dependency>
<groupId>com.b.t</groupId>
<artifactId>v-ps</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>
1) Now where should I put this sub module v-parent in my newly created Maven project (PTest) in eclipse ?
I mean how do I exactly make my PTest/pom.xml find this parent jar v-parent.jar ?
I have separate jar file named v-ps.jar. where should I put it exactly (in which structure) in Maven local repo so that it can find it ?
Each time you run
mvn clean install
Your project will be built and the resulting artifact will be 'installed' in your local repository (usually ~/.m2/repository). So if you build the project which makes the jar com.b.t:vd-ps:1.1.0 this will then be available in your local repository for other projects to use as a dependency.
If you dependency is in another module under the same parent it can in included like so :
<dependency>
<groupId>com.b.t</groupId>
<artifactId>vd-ps</artifactId>
<version>${project.version)</version>
</dependency>
If you run a
mvn clean deploy
Your artifact will be built and deployed to the remote repository (Nexus or Artifactory or similar), provided you have the correct config in your pom.xml
See https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
1) Now where should I put this sub module v-parent in my newly created Maven project (PTest) in eclipse ?
I mean how do I exactly make my PTest/pom.xml find this parent jar v-parent.jar ?
So in this case "v-parent" will be another project in Eclipse of packaging type "pom". And if you want to build the child project "p_test" and its dependency "v-ps", you could list them as modules in the "v-parent" project's pom file like below:
<project>
<groupId>com.b.t</groupId>
<artifactId>v-parent</artifactId>
<version>3.9.0</version>
<packaging>pom</packaging>
<modules>
<module>p_test</module> <!-- Make sure you provide the right path here -->
<module>v-ps</module> <!-- Make sure you provide the right path here -->
</modules>
</project>
Now when you run "mvn clean install" on "v-parent" it should build "p_test" and "v-ps" projects and places a copy of their jar files in your local .m2/repository, assuming those projects don't error our during the build. So the parent project acts like an aggregate for the sub-projects and helps building them all at one shot.
I have separate jar file named v-ps.jar. where should I put it exactly (in which structure) in Maven local repo so that it can find it ?
If you have the source for this project, then you should include it as part of the "v-parent" project's pom as a module like I mentioned above, and when you build the parent, on success, the jar gets written to your local .m2 repo. Or if you have the source for "v-ps" project, you could directly build that using "mvn clean install" and the jar will be written to the repo.

how to add jar dependency in maven?

When I run
mvn compile
I get package com.ibm.icu.util does not exist. So I downloaded the ICU4J jar and installed it into the local repository. I confirmed it's in .m2/repository/com/ibm/icu/icu4j/3.4.4/icu4j-3.4.4.jar. Inside that jar file is the missing class file com/ibm/icu/util/Calendar.class. Then I added the following into the dependencies section of pom.xml:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>3.4.4</version>
</dependency>
But when I run mvn compile again, I get the same error. What am I doing wrong?
You should avoid adding dependencies manually.
If you don't know a groupId and artifactId of the dependency you need, search for it at http://mvnrepository.com/. Usually, groupId matches the package names in the jar file.
For your case, the dependency is already there: http://mvnrepository.com/search?q=com.ibm.icu
So, go to http://mvnrepository.com/artifact/com.ibm.icu/icu4j and get the version of the dependency you need, e.g. 55.1: http://mvnrepository.com/artifact/com.ibm.icu/icu4j/55.1
Grab maven dependency xml and put it to your pom.xml file:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>55.1</version>
</dependency>
If you didn't find your dependency try to find it in google. Sometimes the dependency may be found in some corporate public repositories, not in a central. In this case you need to add the third-party repository to repositories section of your pom.xml.
If you're unable to find your dependency in the public repository then you have three options:
A. Install jar to internal repository server (e.g. nexus)
B. Put the JAR file in your project sources and declare project maven repository :
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${basedir}/my-repo</url>
</repository>
</repositories>
Important: You should keep the maven repository layout in your local repository.
C. [Bad Practice] Use maven install plugin to install your custom jar to local repository on your machine. But it's a badIt's not recommended.
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=path-to-your-artifact-jar -DpomFile=path-to-pom
D. [Bad Practice] Use system dependency with absolute path to the JAR file, although it's a bad practice and should be avoided.
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>X.Y.Z</version>
<scope>system</scope>
<systemPath>${user.home}/jars/my.jar</systemPath>
</dependency>
You should not be manually installing things into the maven repository directory. That directory is maintained by maven itself.
The way dependencies work is that when you run mvn compile or some other goal, it will connect to the maven central repository and download the needed dependencies and their dependencies.
If you manually install a jar file, it may not have it's dependencies. That icu artifact will likely have other things it depends on. Maven will automatically resolve these dependencies.
I would recommend using mvn clean install this will clean the target directory and rebuild everything.
If it fails to download, then you likely need to change the maven configuration. For example, if you are behind a proxy server you need to configure maven with the proxy credentials.
Also, if you manually copied anything into the .m2/repository/ directory you should delete it and let maven put it in there correctly instead.
The beauty of maven is that you don't need to worry about things like downloading jars. It just handles that for you. Let it do it's job.
If you have an internal artifactory like JFrog maybe you should check that the jar is there. Do not download manually to .m2 because it's at least strange. At most you can upload the jar in that artifactory manually.

Add dependency without being a child module

I have 2 projects which results WAR and JAR
in Order to build WAR i need the JAR .. so i need a dependency such that when i try to build the war automatically it should build the JAR and place it in the lib(or Repository)
Help please...
---Parent POM
|
|-web module
| |
| --dependency of jar
|-jar module
You need to configure multi module maven project
Also See:
having-a-maven-project-build-its-own-dependencies
Guide to Working with Multiple Modules
Normally you can just include the JAR-project as a pom-dependency.
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
</dependency>
However you might need to configure some dependency management in a POM-file where each project is as a module:
<modules>
<module>my-project</module>
<module>another-project</module>
</modules>
For more information see this link.
As you stated, if you don't want to use the module management you may just install the JAR with maven install. See the answer made to your last question.
I understand the title of you question "Add dependency without being a child module" in this way: you have two independent maven projects, and you do not want to put them in one project.
Build the Jar project by mvn install this put it in your local repository, then you could add it like a normal dependency to your war project.

Categories

Resources