There is a website with a jar I want to use in my project. I could download it, and make a local dependency on it, but that is lame.
Other people will build this project. I don't want to clutter source control by checking it in. I want to comunicate where the resource can be downloaded from and to easily change the versions. At some point I would like to attach the sources and javadocs.
Is there a plugin that will let me use the URL and download it to my .m2 with a spoofed pom?
The way I do it, is to search for the jars in Maven's central repo:
http://search.maven.org/
This gives you enough data to build the dependency in your pom.xml file
Example:
If I wanted to add jSoup to my project as dependency, then I go and search in the central repo
and add the dependency to the pom file with the info that's in there:
http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.jsoup%22%20AND%20a%3A%22jsoup%22
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
</dependencies>
I think maven2 repo is included by default when creating the super pom, so you don't have to write it, it will look there by default.
I think the best solution is to set up a Nexus or Artifactory repo for the team available over the network. Download the jar from the third-party location, and then upload it with the proper pom GAV values to your new local repo. Then add the URL of this repo to the repositories section of the pom. Everyone will get it when they sync up with version control.
maven-install-plugin can install files in local repo and generate poms, usage example:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc6_g.jar -DgeneratePom=true
Related
Maven has repositories and dependecies. Some plugins allow to use s3 or github as repository. But is there something to use direct URL? Like:
<dependency>
<groupId>my-group-id</groupId>
<artifactId>artifact-id</artifactId>
<version>some-version</version>
<url>https:someurl</url>
</dependency>
May be define URL and dependency info (group and artifact) in properties and for it installed during some maven phase.
Is it possible?
No.
Maven resolves dependencies from Maven repositories. So you cannot just add a JAR that is present at some URL. The JAR has to come from a Maven repository and (if it is not MavenCentral or already specified in your settings.xml or POM), you need to add it.
Yes this is officially supported but not in as convenient a mechanism as you perhaps desire.
Maven resolves dependencies against your local repository. If it can't find them there, it then tries to resolve them against any remote repositories you have configured.
Therefore, if you manually install the jar into your local repository, Maven will find it and use it.
To do this you must:
Download the jar
You have to do this yourself manually or automate the process e.g. using curl or wget
Specify the Maven Install Plugin version in your pom
You are going to be relying on the Maven Install Plugin so you should install you specify the version in your pom.xml, rather than relying on the default version.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
</plugins>
</build>
Install the jar to your local repo
If the jar has been built with Maven, then it will include a pom.xml inside it. If you are unsure, open the jar with any archive manager and look inside the directory /META-INF/maven. You should find another directory named after the group ID, then another for the artifact ID, and then finally the pom.xml itself.
If the pom.xml is included, you can just run this and the tool will read the pom.xml to find the group ID and artifact ID so it can be placed in the right location in your repository:
mvn install:install-file -Dfile=/path/to/downloaded.jar
If the pom.xml is not included, you will need to specify all of this information yourself. It does not matter what you choose but you should choose something that isn't going to confuse you later:
mvn install:install-file -Dfile=/path/to/downloaded.jar -DgroupId=my-group-id -DartifactId=artifact-id -Dversion=some-version -Dpackaging=jar
Update your pom.xml
You can now just reference the dependency as normal in your pom.xml:
<dependency>
<groupId>my-group-id</groupId>
<artifactId>artifact-id</artifactId>
<version>some-version</version>
</dependency>
I'm trying to build the project from this site http://www.joptimizer.com/usage.html. I downloaded the sources jar file, unpacked it and ran maven package in the root folder. Maven fails at the last minute saying it couldn't resolve the dependency..
could not find artifact seventytwomiles:architecture-rules:jar:3.0.0-M1 in central repo - repo.maven.apache.org/maven2 ..
I have a feeling I might need to change something in the pom.xml file for this to work, but have no idea what. Googling for this missing dependency lead me no where. In general, how would one know what to do to handle such errors (and also please help with this specific case).
Specifically
According to the Building notes on http://www.joptimizer.com/usage.html:
JOptimizer is build on maven 3.0. Before building it, you must resolve
(in pom.xml) the external dependency on Colt and other dependencies
that aren't in public repositories. Please refer to the "Dependencies"
report for a complete treatment. For ease of use a boundle with
these external libraries is provided (visit "Download"): extract the
boundle in a folder and run the "maven-install.cmd" (translate it in
your own shell language), and you will get the artifacts in your local
repository.
To get the bundle for this, go to http://sourceforge.net/projects/cvxopt/files/, and download the appropriate version of joptimizer-3.X.X-dependencies.zip. Unzip in your own folder, and run mvn install:install-file -DgroupId=seventytwomiles -DartifactId=architecture-rules -Dversion=3.0.0-M1 -Dpackaging=jar -Dfile=architecture-rules-3.0.0-M1.jar -DpomFile=architecture-rules-3.0.0-M1.pom
Generally
Use a tool like http://mavenrepository.com to search for another version of the missing dependency and update your POM with the proper version. If MVNRepository doesn't know about it, you can install the dependency yourself. If you are working with a group of developers, as Eric Jablow mentions, an artifact repository like Nexus or Artifactory is great for sharing non-public dependencies. If it's just you, you can install the artifact in your local repo as described here: How to manually install an artifact in Maven 2?
You should add your own repository manager like Nexus or Artifactory. Then, find out where this dependency is kept; there are repositories other than central. If it's kept on another repository, have your repository mirror that too.
Otherwise, Nexus or Artifactory have commands to enter the dependency manually. Create a local repository called "Third-party" and add it there.
Finally, change your settings.xml file to refer everything to your repository manager.
The most common case for this is when a company refuses to license their products to be held at the central repository. For example, Microsoft won't let its sqljdbc.jar file be distributed through Central. So, you need to add it by hand.
Change the dependency as follows
<dependency>
<groupId>org.architecturerules</groupId>
<artifactId>architecture-rules</artifactId>
<version>3.0.0-rc1</version>
<scope>test</scope>
</dependency>
Add the repository in pom
<repositories>
<repository>
<id>architecturerules.googlecode.com</id>
<url>http://architecturerules.googlecode.com/svn/maven2/</url>
</repository>
</repositories>
There is a jar file lets say "abc.jar" which maven dependency does not exist(ie created a jar by using java command of own classes). I want to add this jar as maven dependency so that at build time it will automatically copy that jar in lib folder as like other maven dependency. how i will do. please help .
Add it as a dependency with a system scope. See the docs here.
However, rather than adding it as a system dependency it might be better to mavenize the jar itself, then you can build and install it into your dependency management system.
Also, see this question: Can I add jars to maven 2 build classpath without installing them?
You can use the systemPath attribute in the dependency tag in the POM file of your project.
In your pom.xml, use the following snippet corresponding to abc.jar:
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>abc</groupId>
<artifactId>x</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>{path_to_abc.jar}</systemPath>
</dependency>
</dependencies>
The scope parameter corresponding to this artifact must be set to system, for the artifact to be picked up from the specified systemPath.
Hope this helps!
A normal maven dependency is always resolved by looking into a repository. So you must put your JAR file into a repository.
You could install your JAR into your local repository. Have a look at the install plugin. The install-file goal is your friend.
If other developers also need this JAR (because they are working with the same project), they either need to install it locally too, or - better - you deploy the JAR to a remote repository. Have a look at the deploy plugin. Here the deploy-file goal is your friend. For deploying artifacts, you need a repository manager like Nexus or Artifactory.
However, a dependency could also have the system scope (look at the other answers).
I'm new to maven, sorry for nub question: need to build maven project using my version of artifact instead of the one from repo.
More detailed:
I downloaded jboss sources from github and built them using maven 3. It was great! I need to do some changes in jboss dependancy called "picketbox". Now it is an artifact in jboss's "pom.xml".
I built my own version of picketbox in my_picketbox.jar file. How can I tell maven to use my .jar instead of the one from repo?
Maybe I'm missing something but if you explicitly renamed the artifact then it will just be a matter of replacing picketbox with my_picketboxin the relevant JBoss POM-file(s).
<dependency>
<groupId>...</groupId>
<artifactId>my_picketbox</artifactId>
<version>...</version>
</dependency>
And of course, you make sure your artifact is in your local repo by mvn install'ing it.
Cheers,
i would install the jar file using the maven-install plugin.
http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html
First, install your version of Picketbox into your local Maven repository. If your custom version is a Maven project, you can do that by running
mvn install
If your custom Picketbox version is not a Maven project, install the Jar itself into your local Maven repository like this:
mvn install:install-file -Dfile=my_picketbox.jar -DgroupId=org.picketbox -DartifactId=my_picketbox -Dversion=2.3 -Dpackaging=jar
Then change the version of Picketbox that JBoss depends on by adding this snippet to the pom.xml file of the JBoss project you're building (replace the existing dependency on pocketbox with this one):
<dependency>
<groupId>org.picketbox</groupId>
<artifactId>my_picketbox</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>x.x.x</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/xxxx.jar</systemPath>
</dependency>
This allows you to reference libaries that are not stored localy rather than maven central.
Maven takes what has been specified in POM files; mind you, that the POM including the dependency you wrote about can be buried very deeply, not even within the sources you downloaded.
Search your project tree for the artifact in question and replace its version number to your version. If this does not help, do as Nils says in the other answer, but remember to install the artifact in the original version (you may have to find it first in your local repository and remove it).
How do I take a jar file that I have and add it to the dependency system in maven 2? I will be the maintainer of this dependency and my code needs this jar in the class path so that it will compile.
You'll have to do this in two steps:
1. Give your JAR a groupId, artifactId and version and add it to your repository.
If you don't have an internal repository, and you're just trying to add your JAR to your local repository, you can install it as follows, using any arbitrary groupId/artifactIds:
mvn install:install-file -DgroupId=com.stackoverflow... -DartifactId=yourartifactid... -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jarfile
You can also deploy it to your internal repository if you have one, and want to make this available to other developers in your organization. I just use my repository's web based interface to add artifacts, but you should be able to accomplish the same thing using mvn deploy:deploy-file ....
2. Update dependent projects to reference this JAR.
Then update the dependency in the pom.xml of the projects that use the JAR by adding the following to the element:
<dependencies>
...
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
You can also specify a dependency not in a maven repository. Could be usefull when no central maven repository for your team exist or if you have a CI server
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>commons-utils</artifactId>
<version>1.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/commons-utils.jar</systemPath>
</dependency>
Actually, on investigating this, I think all these answers are incorrect. Your question is misleading because of our level of understanding of maven. And I say our because I'm just getting introduced to maven.
In Eclipse, when you want to add a jar file to your project, normally you download the jar manually and then drop it into the lib directory. With maven, you don't do it this way. Here's what you do:
Go to mvnrepository
Search for the library you want to add
Copy the dependency statement into your pom.xml
rebuild via mvn
Now, maven will connect and download the jar along with the list of dependencies, and automatically resolve any additional dependencies that jar may have had. So if the jar also needed commons-logging, that will be downloaded as well.
I'd do this:
add the dependency as you like in your pom:
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
run mvn install it will try to download the jar and fail. On the process, it
will give you the complete command of installing the jar with the error message. Copy that command and run it! easy huh?!
I'll assume that you're asking how to push a dependency out to a "well-known repository," and not simply asking how to update your POM.
If yes, then this is what you want to read.
And for anyone looking to set up an internal repository server, look here (half of the problem with using Maven 2 is finding the docs)