Download a binary with Maven dependency plug-in - bash

I want to write a simple shell script that uses Maven to download the last version of the .zip deployed at this location:
https://github.com/EBIBioSamples/EBIBioSamples.github.io/tree/maven-repo/uk/ac/ebi/fg/myequivalents-rdf/1.1-SNAPSHOT
(that is the file myequivalents-rdf-1.1-20161007.151102-1.zip as of today).
I'm trying to use dependency:copy this way:
mvn dependency:copy \
-DremoteRepositories=https://raw.githubusercontent.com/EBIBioSamples/EBIBioSamples.github.io/maven-repo \
-DgroupId=uk.ac.ebi.fg -DartifactId=myequivalents-rdf -Dversion=1.1-SNAPSHOT -Dartifact=zip \
-Dtransitive=false -DoutputDirectory=/tmp
But it keeps saying "Either artifact or artifactItems is required". I've tried artifact=jar (just to see if it download the .jar instead) and -DartifactItems in place of artifact, but no luck. So, is this possible with Maven? How? The reason I want to use Maven is that it should be faster/cleaner than downloading maven-metadata.xml, extracting the timestamp and using it to build the final URL.

Related

What is the command line approach to download a bunch of jars using maven without POM?

I am using maven and trying to download a bunch of jars.
I donot want to generate the POM file so i am using the below command
mvn dependency:copy -Dartifact=<group>:<artifact-name>:<version> -DoutputDirectory=./localdirectory
How can i use a similar command in maven to download a bunch of artifacts without the POM file?
Any examples using artifactItems would help
The below code snippet does not work
mvn dependency:copy -DartifactItems='com.google.guava:guava:18.0','com.fasterxml.jackson.core:jackson-databind:2.4.3' -DoutputDirectory=./localdirectory

Maven not finding dependency with only jar file

I have a jar in artifactory without pom. when I try to download it from maven/gradle, it is not being found with an error "Could not find com.kroger.galactus:koop-order:unspecified". My question is, is it possible to download artifact from artifactory which only has jar file and no pom file?
edited:
since it is not possible to download without pom file.
I have a third party jar file which I need to upload to artifactory. I used curl -X PUT -u u:p -T Maven-Upload-1.1.jar "https:///artifactory/repo/Maven-Upload-1.1.jar", which uploaded jar file.
Is there other way to upload it so it can create pom file in artifactory?
If there is no POM, you need to create one. You can e.g. do this through the artifactory REST api, possibly through the UI as well.
As Fabian mentioned earlier, this can be fixed by generating the POM for the library. This can be achieved by using this Generate POM REST API.

Maven deploy plugin keep the file name when uploading

I am using maven deploy plugin to upload a file inside bamboo deployment stage. I am uploading the file without pom file. When I upload the file to Nexus, the file name is changing completely. Its appending with project name, version number and build number. I want to keep the filename as it is. Any one know how to do this?
mvn deploy:deploy-file
-Dfile=${bamboo.artifacts.path.artifactFile}
-Dpackaging=cba
-url=https://nexus.internal.organisation.com/content/repositories/snapshots/
-DrepositoryId=snapshots
-DgroupId=com.organisation.art
-DartifactId=myproject
-Dversion=0.0.1-SNAPSHOT
A maven managed repository, such as that provided by Nexus et al, is set up in a way that is intended for deployed products to be returned to a maven build process that has them declared as dependencies.
It is not intended to be a generic file server.
If you have Nexus 3.0 or newer then you have access to so called "raw" repositories that you can set up any way you like.
However you would not use mvn deploy:deploy-file to add files to it. Instead you would follow the instructions in Uploading Files to Hosted Raw Repositories.

How to simply download a JAR using Maven?

How do I download JAR during a build in Maven script?
Maven does not work like that. Here's the closest you'll get to my knowledge:
mvn dependency:get -DremoteRepositories=http://repo1.maven.org/maven2/ \
-DgroupId=junit -DartifactId=junit -Dversion=4.8.2 \
-Dtransitive=false
Note that all parameters except transitive are required.
Also note that Maven will download the jar to your local repository, and there's no sensible way (that I know of) to copy it to a local directory.
Reference:
dependency:get
Or since 3.1, simply as
mvn dependency:get -Dartifact=org.springframework:spring-instrument:3.2.3.RELEASE
Note: This answer is for downloading the jars directly from maven without any scripts [That is how Google directed me here]
Assuming mvn dependency is like this:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.4.7</version>
</dependency>
Goto http://search.maven.org and search for g:"com.zaxxer" AND a:"HikariCP" AND v:"2.4.7" (simply searching for HikariCP also works. You may need to select the appropriate GroupId and Version from the results)
In the Search Results -> Download column, you should see jar javadoc.jar sources.jar available for direct download
You can use:
mvn dependency:copy -Dartifact=<group>:<artifact-name>:<version> -DoutputDirectory=/tmp/my_custom_dir
(Replace <values> with the ones of your case)
That's the full documentation of the goal: https://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html
Note: the other "dependecy:get" way of doing this has been deprecated.
You can download Jar package to specific directory.
mvn dependency:get -Dartifact=org.riversun:random-forest-codegen:1.0.0 -Ddest=./
See How to use Maven pom to download jar files only. This worked really nicely for me.
My use case was that I wanted to download some of the dependency jars to deploy to a QA server, and was doing it manually (outside of the Maven build). I'm not sure exactly what your use case is.
This is what I do (2022 answer), go to https://mvnrepository.com/, search for your .jar and click on here:
Normally you don't use Maven for "just downloading", but for your build process. So normally, you do the following steps:
Define a new project by defining the archetype of your project and some needed properties.
Define as a dependency the library you want to use.
Run Maven with mvn compile
As a side effect, you will have downloaded the library to your local Maven repository. There are a lot of plugins to do something with dependencies, so have e.g. a look at the Maven Dependency plugin.
If you just want to download a JAR once from a maven mirror I suggest you could just do this manually:
For Maven 1:
http://mirrors.ibiblio.org/pub/mirrors/maven/
For Maven 2:
http://mirrors.ibiblio.org/pub/mirrors/maven2/
These are the repositories (a mirror anyway) that maven will get its JARs from - you can easily access them in the webbrowser of your choice and download the JARs etc. Just browse through the hierarchy (it looks like any Java packag hierarchy) until you find the artefact, then pick the right version and you're good.
For example version 3.6.6.Final of hibernate-core from group org.hibernate you'd find here:
http://mirrors.ibiblio.org/pub/mirrors/maven2/org/hibernate/hibernate-core/3.6.6.Final/
You can setup a pom.xml to define your dependencies (the jars you want to copy). Then use the dependency:copy-dependencies goal to copy the jars to the desired location.
It's possible to download a JAR from a Gitlab Maven private repository. The URL is appearing when running some Maven command so it's a bit hacky but it's working for me.
Like this:
wget --header "PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}" "https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/packages/maven/${MAVEN_PACKAGE_NAME}/${MAVEN_VERSION}/${JAR_FILE}"
Where,
GITLAB_PRIVATE_TOKEN is a Gitlab Token with right "api" (atm the others are not enough)
GITLAB_PROJECT_ID e.g. 1462237
MAVEN_PACKAGE_NAME e.g. com/bar/foo
MAVEN_VERSION e.g. 0.0.1
JAR_FILE e.g. foo-0.0.1.jar
Updated 2022 answer for command line - building on Ivan Carcamo's answer:
wget [the link that Ivan Carcamo points to in his screenshot]
Edit: This may download the thin jar for some repositories, so you may need to find a different link for the fat jar
Use the below code snip
result = subprocess.check_output('mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get \
-DgroupId=%s \
-DartifactId=%s \
-Dversion=%s \
-Dtransitive=false \
-DremoteRepositories={repos_string} \
-Dpackaging=jar \
-DoutputDirectory=%s' % (group_id,
artifact_id,
version_name,
des_path), shell=True)
logger.info("success download jar: %s" % each_version)
except Exception as e:
logger.error("Error in download jar : %s" % str(e))
All the jars are available directly in the maven central repository. You don't have to use "maven" if all you want is the jar.
https://repo.maven.apache.org/maven2/
If the pom dependency is
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
You can download it from the corresponding directory structure by replacing the dots with forward slashses on the group id, artifact id, and version. I would navigate to the final folder to get the exact link of the jar.
https://repo.maven.apache.org/maven2/org/apache/poi/poi/5.2.2/poi-5.2.2.jar
As of December 12, 2022 the easiest way to download all dependencies including the jar file itself is to go to this link:
https://jar-download.com/online-maven-download-tool.php
And simply paste the Maven XML to the page and click download. God bless the person who built jar-download.com. :-)
This works for me, but it downloads only the latest version:
#! /bin/bash
set -eu
if [[ -z ${1:-} || -z ${2:-} ]]; then
printf 'Usage: %s group artifact\n' "$(basename "$0")" >&2
exit 1
fi
g="$1"
a="$2"
url='https://search.maven.org'
v="$(curl -s "$url/solrsearch/select?q=g:$g+AND+a:$a&core=gav&rows=20&wt=json" |
jq -r '.response.docs[0].v')"
curl -s -L -o "$a-$v.jar" "$url/remotecontent?filepath=${g//.//}/$a/$v/$a-$v.jar"
You need bash, curl and jq.
Example:
./maven-download net.sf.saxon Saxon-HE
Downloads Saxon-HE-11.4.jar today (2022-12-23).

Where can I find a repository containing functionaljava 3.0?

I would like to use functional Java 3.0 in a maven project. I have been googling a little, but I can't find valid information to include the corresponsding 'dependency' in my pom.xml.
Someone mentioned that he would upload the artifacts to the scala-tools maven repository, but it does not seem like he followed-up on his announcement.
It does not even seem that earlier versions of functional Java are available from maven.
Does anyone have valid and recent information about this issue?
Thanks!
P.S.: Yes, I know I can download and manually include the .jar in my project, but this is only a workaround solution...
Version 3.0 is now in Maven Central: http://repo1.maven.org/maven2/org/functionaljava/functionaljava/3.0/
There doesn't seem to be any maven distribution. But here's a small shell script that will install the jar and the sources in your local repo.
Unpack the zip distribution and execute this script from inside the unpacked directory:
#!/bin/bash
# edit these to suit your needs
GROUPID=org.functionaljava
ARTIFACTID=fj
VERSION=3.0
mvn install:install-file -DgroupId=$GROUPID -DartifactId=$ARTIFACTID \
-Dversion=$VERSION -Dpackaging=jar -Dfile=functionaljava.jar
cd src/main
jar cf src.jar *
mvn install:install-file -DgroupId=$GROUPID -DartifactId=$ARTIFACTID \
-Dversion=$VERSION -Dpackaging=jar -Dclassifier=sources -Dfile=src.jar
rm src.jar
(Windows version would be very similar, probably just replace $this with %this%, maybe prepend CALL to the maven calls)
Fact is, I don't like to manually include .jars in repositories, because it is not a distributed solution.
I have copied the functional java code in a maven 'repository' project called 'externals' and it compiles fine. I can generate the artifacts and have no need to upload them manually anymore.

Resources