Maven: download artifact and its deps to a specific directory - maven-2

I must be missing something. I have searched and searched, and played and tinkered, and I still can't figure out how to do the following with Maven:
I would like to download an artifact and all its dependencies (and transitive dependencies), from our internal Nexus server, into a user-specified location. The idea here is to allow the person who is deploying the solution into production a way that they can easily get all the jar files they need in one place.
There is dependency:get, and this is close-but-no-cigar. With dependency:get, all the artifacts are downloaded into the local mvn repository, under directories according to each artifact's groupId and artifactId. This is NOT what I want, because then you have to trudge around all those directories to get at the jars. I want all the files downloaded to one directory so that they are in one place.
Then there is dependency:copy-dependencies. This again does almost what I want; it copies all of an artifact's deps into target/dependency. The two problems with this are 1) You need to have a pom.xml; you can't specify arbitrary coordinates like you can with dependency:get, and 2) dependency:copy-dependencies does't copy the main artifact itself into target/dependencies.
There must be a better way to do this, but I can't figure out where else to look for a solution. To summarize, I want to be able to given someone a set of maven coordinates (groupId:artifactId:version) and our internal Nexus URL, and have them download everything with one command into a directory of their choosing.

You can combine the use of dependency:copy and dependency:copy-dependencies to achieve your goal.
The idea is simple:
Use dependency:copy to fetch the pom.xml of your starting artifact.
Use dependency:copy-dependencies feeding it the pom.xml you fetched in the previous step to fetch all of the starting artifact's dependencies.
Use dependency:copy again to fetch the starting artifact itself.
#!/bin/sh
if [ "$#" -ne 2 ]; then
echo "Usage: $(basename $0) <artifact> <directory>" > /dev/stderr
exit 1
fi
ARTIFACT="$1"
OUTPUT="$2"
mkdir -p "$OUTPUT"
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
mvn dependency:copy "-DoutputDirectory=${TMP}" "-Dartifact=${ARTIFACT}:pom"
POM=$(find "$TMP" -type f)
mvn dependency:copy "-DoutputDirectory=${OUTPUT}" "-Dartifact=${ARTIFACT}"
mvn dependency:copy-dependencies "-DoutputDirectory=${OUTPUT}" -f "$POM" -DincludeScope=runtime
Save as download-jars and use like this:
download-jars org.apache.hadoop:hadoop-aws:2.7.3 /tmp/jars

Use the maven assembly plugin to package an additional "jar with dependencies" into a ZIP file that includes everything.
http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<!-- TODO: a jarjar format would be better -->
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
Then the user can just request <type>zip</type>, in addition to the regular 'maven coordinates' to get a zip file with all the dependencies.

If you "want to given someone a set of maven coordinates" it would be the best to put them in a special pom.xml (you have to write them down anywhere). This pom is not the pom of your "main artifact" but has the "main artifact" as dependency. The packaging type can be pom as this project will not create any artifcat itself.
Then use the dependency:copy-dependencies solution that you already evaluated and you will get all the dependencies you need. IMHO a elegant and simple solution. I do not know any better.

Related

How can I package a specific version with maven?

I'd like to call
mvn clean install -Dsomeproperty=1.2.3-20110526-1836
to get
artifact-1.2.3-20110526-1836.jar
instead of
artifact-1.2.3-SNAPSHOT.jar
How can I pass that timestamp to maven ??
The following artifact setting in pom.xml seem to be doing what you want:
<groupId>testgroup</groupId>
<artifactId>testartifact</artifactId>
<version>${someproperty}</version>
Now if you execute "mvn clean install -Dsomeproperty=1.1.timestamp", the jar file produced also contains the timestamp in its name.
I'm not sure if this is what you are looking for.
EDIT
Another solution since the pom file cannot be changed.
Execute the "mvn clean install" command normally. This generates a jar file like artifact-1.2.3-SNAPSHOT.jar.
Install this file again - this time with "mvn install:install-file -Dfile=artifact-1.2.3-SNAPSHOT.jar -DgroupId=testgroup -DartifactId=testartifact -Dversion=1.2.3-123456-1234 -Dpackaging=jar. This will install artifact-1.2.3-123456-1234.jar in your local repository
While this will do what you specified:
<project ...>
<properties>
<someproperty>somproperty-default-value</someproperty>
</properties>
<build>
<finalName>artifact-${someproperty}</finalName>
....
</build>
....
</project>
I would recommend to use this: How do I add time-stamp information to Maven artifacts?
The fastest hack for this is to run
mvn clean install --offline
This will prevent from loading your nightly build from remote repos.
You may also play with settings.xml

Maven - Best way to refer to a directory on the system path

I am trying to build an RPM from my Maven project. I have 5 different modules and each one has its own pom.xml, In the root I have one pom.xml which builds all modules (Typical Maven Setup). When I build an RPM, I want to include a directory that is not part of the maven directories. Its above a directory [from the root folder that contains my maven modules]. What is the best way to include that in my RPM? or rather what is the best way to refer to a directory with out hardcoding the path? I am confused about ${baseDir} and what it refers to?
Thank you.
${project.basedir} refers to the root of the project, ie where the pom.xml is, so you could use that in <systemPath>${project.baseDir}/../../dirYouWant</systemPath>
In general though, Maven best-practices would frown about relying on the relative paths around your projects from being there. Instead, I suggest deploying those files as there own project to your maven repository (as a zip, jar, whatever), and then getting them as part of your rpm build. Depending on what plugin you are using to build your RPM, you can unpack those files automatically.
Try this
<dependency>
...groupid,artifactid etc..
<scope>system</scope>
<systemPath>path/to/your/jar</systemPath>
</dependency>
Did you mean you want to add another project to your maven build being level above?
you can do it like this :
in your parent pom :
<modules>
<module>../projectdirectory</module>
</modules>
in your projectdirectory pom :
<parent>
<groupId>...</groupId>
<artifactId>...parent...</artifactId>
<version>...</version>
<relativePath>../parentProject/pom.xml</relativePath>
</parent>

Using Maven ant task to install jar to local repository

At the end of my ant build id like it to call the equivalent of the command line call
mvn install:install-file -Dfile=my.jar -DgroupId=com.company.project -DartifactId=my_project -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
so that it will add the newly built jar to a maven repository which another project will rely on.
Ive tried using the maven-ant-task and have added the maven-ant-task jar to the ant built project and the following code to the build.xml:
<target name ="minstall" depends="jar">
<artifact:pom id="maven_install" file="maven_install.xml" />
<artifact:install file="${out.dir}/my_project.jar">
<pom refid="maven_install"/>
</artifact:install>
</target>
but seem to be missing something as it wont work for me. To begin with i get the error in the build.xml (ant build file) saying
The prefix "artifact" for element "artifact:pom" is not bound.
What am I doing wrong. I am fairly new to ant?
On a realted question what is the purpose of the associated POM file? I would not normally have a POM in this project as it is an ant build
Perhaps maven-ant-task jar is not installed, i.e. not in your ant CLASSPATH. You can follow this instruction for this.
As mentioned previously, you need to make sure the tasks are defined in your ant script, and the artifact namespace is understood.
The POM file is used (in this case) to tell the Maven repositories the dependencies of the JAR you are putting in the repository. The POM should also specify the JAR's identification information (groupId, artifactId, version number, license, etc.).
Strictly speaking, you do not need an external POM, you could define the information in your build.xml file as follows:
<!-- Assuming tasks defined, and 'artifact' namespace exists -->
<artifact:pom id="maven_install" groupId="com.whatever" artifactId="some-jar"
version="1.0" packaging="jar">
<dependency groupId="..." artifactId="..." version="..."/>
<dependency groupId="..." artifactId="..." version="..."/>
<license name="apache" url="http://www.apache.org"/> <!-- can be omitted -->
</artifact:pom>
<target name ="minstall" depends="jar">
<artifact:install file="${out.dir}/my_project.jar" pomRefId="maven_install"/>
</target>
When you install the JAR in the 'minstall' task, the POM should be generated with the appropriate dependencies in the local Repository.
That message means you are missing an xmlns:artifact attribute in your build.xml. Have a look at the installation page in the docs for an example.
As to the purpose of the POM file, it's mostly metadata so that maven can figure out dependencies properly. In a real maven build it also describes how to build, test and package. But in your case all that is done by ant instead.
I think that it makes no sense to put such commands in Ant's build.xml. If you want to have your jar file installed in your maven repo just use mvn install command.
Besides that, I guess that you are somehow confusing the purpose of Maven and Ant tools in your project. What I'd suggest is to use Maven as your main build tool. You can configure invokation of Ant targets in your POM file if you really need that. Personally, I think it is the best solution to have Ant called by Maven. Maven goals (such as clean, test, package, install and so on) are very simple to use and powerful (I guess that you can read it in every Maven tutorial).

maven install file manually without version

How do I add a jar file to my local repository without appending the version number to the jar file?
Lets say I have a jar file named abc.jar and run the following command, it will create abc-1.0.jar and if I bundle this artifact in a war file, the resulting file name will be abc-1.0.jar. If I remove the -Dversion, the command fails. If I mention blank value -Dversion="", then abc-.jar is created. How do I keep the original jar's filename(abc.jar)?
mvn install:install-file -Dfile="d:\abc.jar" -DgroupId=grp1 -DartifactId=art1 -Dversion=1.0 -Dpackaging=jar
How do I add a jar file to my local repository without appending the version number to the jar file?
You can't.
This works for war packages. I haven't tried it for jars.
<build>
<!-- Ensures that the version number is not included in the packaged file name -->
<finalName>myrenamedpackage</finalName>
</build>
You can not change the name of the arifact in your maven repository, but you can configure the war plugin to use a specific nming scheme for the libs it bundles in WEB-INF/lib using the outputFileNameMapping option. To remove version information and classifiers the mapping pattern would be #{artifactId}#.#{extension}#. If the artifact id matches the original filename this should give the wanted result.

using maven to manage java dependencies in a jruby rails app

I'm trying to write a pom.xml that will allow me to run a command locally and fetch all dependencies that my jruby Rails app has. I'm seeing two different configs though and I'm not totally sure which to use (as I'm not a java person whatsoever)
First, many Pom's i'm seeing just have a tag under the root of the pom.xml that list all dependencies. This doesn't however have any information about where these are stored etc... so I feel like this isn't what I want (I need to copy them to my rails lib dir)
Second option, I'm seeing in the mvn docs is to use the maven-dependency-plugin, which seems more like what i'm looking for. I assume then that my outputDirectory would be something like lib
So I don't fully understand what the purpose of the first option's dependency list is for. All I want is mvn to copy my jars locally (and then eventually when my CI server does a deploy). Can someone point me in the right direction?
First Option
<project>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</project>
Second Option
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[ groupId ]</groupId>
<artifactId>[ artifactId ]</artifactId>
<version>[ version ]</version>
<type>[ packaging ]</type>
<classifier> [classifier - optional] </classifier>
<overWrite>[ true or false ]</overWrite>
<outputDirectory>[ output directory ]</outputDirectory>
<destFileName>[ filename ]</destFileName>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</plugin>
</plugins>
</build>
</project>
First, many Pom's i'm seeing just have a tag under the root of the pom.xml that list all dependencies. This doesn't however have any information about where these are stored etc... so I feel like this isn't what I want (I need to copy them to my rails lib dir)
This is the traditional way to declare and use dependencies on a Java project. Dependencies declared under the <dependencies> element are downloaded from a "remote repository" and installed to your "local repository" (in ~/.m2/repository by default) and artifacts are then handled from there. Maven projects (at least the Java ones) don't use a local lib/ folder for their dependencies.
Second option, I'm seeing in the mvn docs is to use the maven-dependency-plugin, which seems more like what i'm looking for. I assume then that my outputDirectory would be something like lib
The maven dependency plugin allows to interact with artifacts and to copy/unpack them from the local or remote repositories to a specified location. So it can be used to get some dependencies and copy them in lets say a lib/ directory indeed. Actually, it has several goals allowing to do this:
dependency:copy takes a list of artifacts defined in the plugin
configuration section and copies them
to a specified location, renaming them
or stripping the version if desired.
This goal can resolve the artifacts
from remote repositories if they don't
exist in local.
dependency:copy-dependencies takes the list of project direct
dependencies and optionally transitive
dependencies and copies them to a
specified location, stripping the
version if desired. This goal can also
be run from the command line.
The first goal would use the setup you described in your second option. The second goal would use the standard project dependencies that you described in your first option. Both approaches would work.
The problem here is that I don't know exactly what a JRuby Rails app is, what the development workflow is, how to build such an app, etc so I don't know exactly what you need to do and, consequently, what would be the best way to implement that with Maven.
So I googled a bit and found this post that shows another approach based on OS commands (using the maven exec plugin) and has a complete pom.xml doing some other things. Maybe you should look at it and use it as a starting point instead of reinventing everything. This is my suggestion actually.