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.
Related
I have created two training projects aiming to the creation of a Java Test Automation Framework with Selenium.
Both projects have been stored in my personal git: https://github.com/omarjmc
Framework: Contains main classes and all the code needed to execute successfuly the tests
Project: Demo project that will use the methods specified in the Framework project
Additionally i added the dependency to the Framework project in the demo Project's POM and I also created a release of the Framework project and attached the JAR file
enter image description here
Currently I have to clone both repositories and run mvn clean install on the framework to get the JAR file and change the packaging back to pom, but I want to just clone the demo project, run mvn clean install there and still get the Framework JAR file.
What am I missing the POM file?
Omar you have to create a maven multi module project for this requirement. Project will be the parent project and it should contain framework as child.
The basic code structure for pom.xml for Project should be 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.project</groupId>
<artifactId>multi</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>project</name>
<modules>
<module>framework</module>
</modules>
</project>
You can refer below the github project for this exact requirement.
https://github.com/jamesward/maven-multi-module-example
I created a multi-module proyect, but it only works if you download the original repository, what I want is to have them in separate repositories and just download, and use, the project child
I have created a Java project with maven that I want to implement in other maven project.
The child project is not a spring project, is a Java project created from scratch migrated to maven to manage the dependencies.
I can generate the jar with mvn install. So far so good.
Then I try to implement this jar to other project that is built with maven and spring.
For that I added that jar to my local maven repo with the following command:
mvn install:install-file -Dfile=/Users/.../target/app-0.0.1-SNAPSHOT.jar -DgroupId=com.mycompany.childapp -DartifactId=childapp -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar
Then I am able to see that in the repo folder in .m2, the jar file is correct.
And this in the parent project pom.xml:
<dependency>
<groupId>com.mycompany.childapp</groupId>
<artifactId>childapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
And also, in the plugin section:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
At least for eclipse everything is good, as I can develop and implement the classes inside the library without any issue. I can build the parent app with mvn clean package But when I run the app and try to execute the section of the code that uses the child library I get ClassNotFoundException for the classes that are in the child project.
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.
How could this be fixed?
mvn clean package creates jar for parent project but it does not include child jar.
You should use SpringBoot
mvn bootRun to run your project locally
mvn bootJar to build fat jar which will include all required dependencies.
To run this fat jar call java -jar parent.jar
Finally it was that the library was like... 'corrupted'.. or something like that.. It was a Java Project (created with eclipse wizard) than then I converted to maven (with eclipse wizard).
I started a new project as maven project from the beggining and everything was working at the first attempt....
So I just have to run mvn clean install to generate the jar and installed in the .m2 repo
Then in the main app pom.xml I have to declare as any other dependency:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>mylibrary</artifactId>
<version>0.0.1</version>
</dependency>
As per the comments, you want to create a project with maven which will create a jar file. That jar file will be used in another project. If my understanding is correct. You have to do the followings.
Create a simple maven project with simple type archetype.
Run the pom.xml with the command like mvn clean install or mvn clean package.
Use the jar file from the simple project upload into your nexus/artifactory repository and use the maven definition in another maven project.
If you do not have nexus or artifactory, you can use the jar file locally in another maven project.
To add a jar file locally in another project, you have to add the following dependency structure in pom.xml.
<dependency>
<groupId>your group id</groupId>
<artifactId>name of the application</artifactId>
<version>some version</version>
<scope>system</scope>
<systemPath>your location/jar-file-name.jar</systemPath>
</dependency>
Hope it answers your question.
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
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
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.