I am putting together a pom.xml for a java project. This java project has some java code that depends on a number of other libraries that exist in a private remote repository.
In my pom.xml:
<dependency>
<groupId>com.mycom</groupId>
<artifactId>my-id</artifactId>
<type>pom</type>
<version>9.1.00-SNAPSHOT</version>
</dependency>
Note that the artifact is a pom. This POM contains references to all the dependencies for the artifact ID my-id.
The issue I'm facing is this:
If I change the <type> to jar, it doesn't work because the actual file is not a single jar, it is a POM file that references all of the dependencies.
The remote POM is downloaded, but maven doesn't download and install all of the dependent .jar libraries referenced in the remote POM.
How can I achieve the end goal of having maven grab the latest POM file from the remote repo and subsequently download as dependencies all of the jars listed in this remote pom.xml? Is this even possible?
Related
My project contains around 30 jars which does not have a maven dependency settings.
So I have to manually add the jars in th local maven repo and then use the custom maven dependencies to project.
However I have to add 30 dependencies in the POM file which I feel might not be a good way to do it.
Is there any way so that those all 30 jar files can be added with a single pom dependency?
For adding the jar in the local maven repo and then use the dependency, I am using below method:
Ex:
$ mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
and then use the dpendency as below:
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3</version>
</dependency>
Kapcha jar is just for reference and I have dfferent 30 jars to add in repo and use in the project.
Please help.
First of all, I would check whether you can draw a fair amount of these dependencies form MavenCentral (avoiding the manual hassle).
Secondly, if you are in a company with more than two Java developers, you should set up a Nexus or Artifactory server to handle the JARs. So no manual installation any more.
After saying this: You can create a POM that contains a list of dependencies. If you add this POM as dependency in your project, then all the dependencies of the POM will become (transitive) dependencies of your project.
I got the "Archive for required library cannot be read or is not a valid ZIP file" error for my maven project I imported in Eclipse. I read some posts about this and suggested is to delete the faulty directory like in my case:
~.m2\repository\com\cogentex\rpw\2.2 and
~.m2\repository\com\cogentex\rpw-lkb\2.2
Then you should update the project via Eclipse: Maven>Update project and click force update of snapshots/releases.
I followed these steps and the directory looks like this now:
So the correct .jar files are still missing and it also results in more erorrs now:
1: Missing artifact com.cogentex:rpw-lkb:jar:2.2
2: Missing artifact com.cogentex:rpw:jar:2.2
3: The container 'Maven Dependencies' references non existing library '~.m2\repository\com\cogentex\rpw\2.2\rpw-2.2.jar'
Here is the snippet from the pom.xml which throws the first two errors:
<dependency>
<groupId>com.cogentex</groupId>
<artifactId>rpw</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.cogentex</groupId>
<artifactId>rpw-lkb</artifactId>
<version>2.2</version>
</dependency>
What do I have to do to make maven download the correct .jar files? I already tried running maven cleanor maven install and restarting Eclipse.
What you see in the folder is some files that indicate the artifact was not found and when was the last time Maven checked.
Whatever com.cogentex:rpw is, it's not in Maven Central so Maven will not find it there. You need to tell Maven where to get it from by providing the URL to a repository that contains it. If/when your POM has the repository, make sure
you do have access to the reposiory from the environment you run your build in (check proxies, firewalls, ...)
the GAV coordinates (groupId, artifactId, version) are correct and match the one in the repository.
the artifact type in the repository is jar. If it is not, provide the correct type in the dependency
the artifact is not deployed to the repository with classifier. If it is, provide the classifier in the dependency
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.
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.
I try to compile this pom file but it doesn't download
import org.codehaus.swizzle.confluence.*;
dependency. I get pom file from maven's site. Search Maven Swizzle
That's the POM of the swizzle-confluence library, i.e. the POM file that the library's project uses for its own build.
Instead, you need to add the dependency declaration for this library to your project's POM. In Maven Central search, this is available on the left-hand-side of the version information page, under Dependency Information.
So, open up the page for swizzle-conflunce:1.6.2, and you can see that this is:
<dependency>
<groupId>org.codehaus.swizzle</groupId>
<artifactId>swizzle-confluence</artifactId>
<version>1.6.2</version>
</dependency>
Also, to help yourself in the future, consider reading about the POM and how dependencies work.