How declare maven dependecny from URL? - java

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>

Related

have maven download a jar as a dependency

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

How does Maven understand release:prepare command?

I'm trying to create a plugin which would download and install jars from Maven central as system tools. So I want my line to be like
mvn install-plugin:install org.chaschev:cap4j:1.0
similar to Ruby's
gem install capistrano
This plugin would gather all the needed information about the shortcuts to create from the JAR. I.e. this jar would contain a class implementing an installation interface.
How does Maven understand that in order to execute a command like release:prepare it requires to download the release plugin and to run it? Any better/other way to do this?
Do you mean how the relation between plugin/goal in the comamnd line and plugin implementation is defined? Then the answer is plugin.xml. See plugin.xml for release plugin, e.g. maven-release-plugin-2.0.jar:
<goalPrefix>release</goalPrefix>
...
<mojos>
<mojo>
<goal>help</goal>
...
<mojo>
<goal>prepare</goal>
...
Or do you mean, how Maven discovers which plugins are available? Then the answer is:
There are two default groups where plugins are searched, org.apache.maven.plugins and org.codehaus.mojo
For your own plugin you may want to use name ${prefix}-maven-plugin, e.g. cap4j-maven-plugin
You can keep your name cap4j, but then put the plugin description to your POM, under <plugins>
If you want your build to work at other machines, they should point <pluginRepositories> in POM or in settings.xml to your plugin repository
It is not good to use default Maven groups for your own project.
Instead, define your own group for your plugin, like this:
<pluginGroups>
<pluginGroup>org.chaschev</pluginGroup>
</pluginGroups>
And rename your plugin from cap4j to cap4j-maven-plugin. Then Maven will discover your plugin without further cahnges in POM.
Alternative, without <pluginGroups>, just put following to your POM:
<plugins>
<plugin>
<groupId>org.chaschev</groupId>
<artifactId>cap4j</artifactId>
<version>...</version>
<configuration>
...
</configuration>
</plugin>
...
</plugins>

How to use Maven pom to download jar files only to a specific directory?

Is there a way to download dependencies from a pom.xml file to a specified folder in java? I'm able to run maven command from java and I got download messages, but I don't know where maven stores these libraries? How can I download these dependencies to a specific folder?
Take a look at maven's dependency plugin, specifically the
copy-dependencies goal. The usage section describes how to do exactly what you want.
To do it from the command line just do:
$ mvn dependency:copy-dependencies -DoutputDirectory=OUTPUT_DIR
Add this to exclude the transitive or inner dependencies:
-DexcludeTransitive=true
As explained here, you can use maven-dependency-plugin:get for this.
For example, if you want to download org.apache.hive:hive-common:2.1.1 in your local folder, execute this:
mvn dependency:get -Ddest=./ -Dartifact=org.apache.hive:hive-common:2.1.1
If you want to download the latest 3.0.0-SNAPSHOT:tar.gz version of com.orientechnologies:orientdb-community-gremlin from https://oss.sonatype.org/content/repositories/snapshots snapshots repository, execute this:
mvn dependency:get -Ddest=./ -DremoteRepositories=sonatype-nexus-snapshots::::https://oss.sonatype.org/content/repositories/snapshots -Dartifact=com.orientechnologies:orientdb-community-gremlin:3.0.0-SNAPSHOT:tar.gz
Add something similar to the following to pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<outputDirectory>
${project.build.directory}
</outputDirectory>
</configuration>
</plugin>
Then run mvn clean dependency:copy-dependencies to perform the copy.
Combine this with the assembly plugin and you can package everything into a self contained archive for distribution.
Maven stores all of these in it's local Maven2 repository. By default, it will store them in your user home directory under a directory called repository.
You can use the maven-dependency-plugin's goal called copy to take all of your project's dependencies and put them in a folder.
http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html
Go to this site: http://jar-download.com/online-maven-download-tool.php
Insert the Maven dependencies XML
Download the jar files as a ZIP.

Add a dependency in Maven

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)

artifactory-maven-plugin: How to resolve dependencies from private Artifactory without settings.xml

How can I configure the artifactory-maven-plugin to resolve dependencies from our private Artifactory server?
The official documentation explains on how to publish:
https://www.jfrog.com/confluence/display/RTF/Maven+Artifactory+Plugin#MavenArtifactoryPlugin-Usage
Usually, dependency resolving works by adding servers to my .m2/settings.xml and specifying credentials there. However, in a cloud Docker build environment, it is difficult to place the settings.xml inside the container. It would be much easier if the plugin could be configured accordingly.
The Gradle Artifactory Plugin has explicit documentation about this (see dependencies resolution):
https://www.jfrog.com/confluence/display/RTF/Gradle+Artifactory+Plugin#GradleArtifactoryPlugin-UsingtheArtifactoryPluginDSL
Turns out the Maven plugin also has a resolver configuration option. But it is not documented anywhere. This does not work (Maven still won't try to download dependencies from Artifactory):
<plugin>
<groupId>org.jfrog.buildinfo</groupId>
<artifactId>artifactory-maven-plugin</artifactId>
<version>2.6.1</version>
<inherited>false</inherited>
<configuration>
<resolver>
<contextUrl>${artifactory.context.url}</contextUrl>
<username>${artifactory.username}</username>
<password>${artifactory.password}</password>
<repoKey>libs-local</repoKey>
</resolver>
</configuration>
</plugin>
You can configure Maven to resolve artifacts through Artifactory you need to modify the settings.xml. You can generate one automatically, or modify it manually.
jfrog docs has provided steps to do this ,
In the Artifact Repository Browser of the Artifacts module, select Set Me Up. In the Set Me Up dialog, set Maven in the Tool field and click "Generate Maven Settings". You can now specify the repositories you want to configure for Maven.
Check here

Categories