Copying jar from local Maven repository - shell

I'm working on a sort of deployment script for a Java project using Python/shell. The script currently can copy jars either from a Sonatype Nexus repository or from the project's target folder. The remote/Nexus setup seems all good, but I'm interested in instead copying from the local maven repository because allows me to always know the location of the jar regardless of where the project is installed.
I guess my question is: Am I overlooking anything by just copying the first jar from the folder ~/.m2/repository/{groupid}/{artifactid}/{version}? Or is this totally a good way to go about this?

If the groupId consists of more than one part, for example org.apache.httpcomponents , then the folder structure reflects this: org/apache/httpcomponents/...
There may exist more than one jar file inside the version directory.

Related

Maven download dependencies

I don't know if there is a better title for this post (please suggest) but the situation is this: in my .m2/repository folder there are some dependencies with all the configuration files and the corresponing jar but in others I have the config files but not the jar. For instance, in .m2/repository/ant/ant/1.5/ I have the files:
_maven.repositories
ant-1.5.pom
ant-1.5.pom.lastUpdated
ant-1.5.pom.sha1
and ant-1.5.jar is missing. Is there a way to add these missing jar's to the local repository?
Thanks in advance for your answers.
The point is your projects should be first in version control and from there it shouldn't be a big issue to rebuilt them. If you have more than 10 projects than you are doing it wrong and you should do them via Jenkins or something similar...And the best maintenance I know of is simply delete it completely and build your project...btw. The local repository is not a repository it is only a local cache don't mistaken that...Start using a repository manager to hold the built artifacts in a reproducible manner...

target folder vs local repository

I know that Maven houses the outcome of the build in the local repository (artifacts goes installed under ~/.m2/repository/), but it also outputs the compiled classes in the target folder next to src.
Is there any difference between what goes in the local repository and what goes in the target folder?
They are completely different and shouldn't be mixed up.
target represents the build directory. This is to say, every temporary file that is generated during the build from the sources ends up there. Quite notably, you'll find the compiled classes of the main and test Java sources, but you'll also find lots of things in there (generated source files, filtered files, etc.). What matters, is that everything that is contained in this folder is inherently temporary. You can delete it at any time, running mvn clean, and be assured that the next build will (or at least should) work just fine. All the files and folders generated under target serve a single purpose: create the artifacts of the project. A Maven project, for example with jar packaging, will have a single main artifact, which is composed of its final name with a jar extension, and will contain the compiled Java classes. The final name can be a custom name, set within the POM, or the default one derived from the Maven coordinates of the project. Such a project can also have additional attached artifacts, like a test JAR, or a sources JAR.
The local repository only contains the artifacts. There are no temporary files in there. What is installed when running mvn install is strictly the generated artifacts of the Maven project, i.e. the end products, plus the POM file of the project. Everything that served to create them isn't put in the local repository, and the build of a project must never put temporary things in there. Keep in mind that the local repository is a Maven repository, and, as such, follows a strict naming scheme: a project with a group id of my.groupid, an artifact id of my-artifactid and a version of 1.0 will get installed in the folder my/groupid/my-artifactid/1.0; in which you'll find the POM file, and all the other artifacts. The name of the artifacts themselves cannot be overriden: it will be my-artifactid-1.0.jar for a JAR project (perhaps with a classifier added).
This is generally a source of confusion: the name of the main artifact file that is generated under the target folder is completely distinct from the name that it will have in the local repository when installed, or in remote repositories when deployed. The first can be controlled, but the latter is defined by the naming scheme of the repository, which is calculated from the coordinates.
To recap: target contains all the gory temporary details during the build which creates the artifacts of a project (main JAR, sources, Javadoc... i.e. everything that is supposed to be deployed and released by that project), while the local repository (and remote repositories) will contain only the artifacts themselves.
Not much in terms of the generated module.jar if that's what you are really concern about. The .jar generated is the same, also considering recompiling the code would clean your /target folder but not the .m2 one.
Though /target folder would generally be composed of the compiled source classes /target/classes and /target/generated-sourceetc along with a module.jar.
On the other hand the local ~.m2/repository would consist of module.jar along with the pom.xml for that module and all the configs(repositories, dependencies etc) to rebuild that module from if required.

Exporting Maven Archetype from Local Repository

I have a Maven archetype that I created myself from one of my projects and installed in my own local Maven repository. From Eclipse I am able to create new projects using this archetype and I obviously have access to the archetype's "installed" files (the ones that are placed in the repository after the install).
The problem is that now I want to share the archetype to a colleague, but after I created it and installed it to my local repository, I deleted the "installer" files (the ones that are generated in the generated-sources/archetype folder of my source project), so now I only have the archetype installed in my repository and I don't know how to export it in order to share it with someone else.
I could create a new archetype, but I put so much effort and spent several hours in doing that in the first place, so I would like to find out exporting the current archetype is possible, and if so, how can I do it?
EDIT
Asked in a different way, can I take the "installed" files from my local repository and put them in other repository, and have Maven recognize them as an Archetype?
What we ended up doing was to zip the repository archetype folder and send it to my colleague, who then performed these steps:
Unzip the archetype folder file to the root of his local Maven repository.
Execute the command ´mvn archetype:crawl´.
That was it. He was able to use the archetype for creating new projects.

Download artifacts from local Maven Repository

Hey im new to maven and i was wondering if anyone knew if i had a bunch of files in a directory, can i make that directory a local repo for maven. the projects in there are not maven projects but would i be able to somehow downlaod those files onto another location using maven?
this is how the folder is organized:
patch
--new folder
----versions (multiple folders which contain zip files)
is there a way to getting the version folders and copying that to a different directory with a maven command like
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=org.hibernate:hibernate-entitymanager:3.4.0.GA:jar:sources -DoutputDirectory=/home/-Dmdep.useBaseVersion=true
You were very close to the correct solution - same plugin, different mojo:
$ mvn maven-dependency-plugin:copy-dependencies -DoutputDirectory=/path/to/output
This will dump all dependencies into the location of your choosing.
The -DstripVersion=true argument is useful if you want to have the jars by their canonical name without the version suffix (ie log4j.jar vs. log4j-1.2.17.jar).
Hope that helps.

Maven―Dependencies, static content from remote repository

I am a bit new to maven, but I have some experiences with ant and the build process. I would like to do one thing that is kind of driving me nuts:
Given:
A remote repository (git, svn, hg,…) that holds static content (like images),
one maven project that uses/manages the mentioned repository in the same way as it does with all other dependencies (checkout on install, update whenever updates occur), in other words: the maven project depends on that repository
I finally want to be able to access the content (no *.svn or *.git) and copy it into my build, on build time*.
I want maven to store a local copy of that repository in maven`s local repository (~/.m2/repository) only once on each machine and manage it like all other dependencies.
*I am not trying to build a Java project
Thanks for help!
From what I've seen, Maven projects don't use version control repositories as external artifacts. That's a little too fine-grained for what you want, I think.
I've done something similar, when Project A wanted to use resources from Project B.
Project B, as part of its build procedure, collected it's resources into a ZIP file and deployed the ZIP file into a maven repository.
Project A then references the ZIP file artifact, unpacking it when building to where it needs it.
Look into the dependency plugin for maven, especially the dependency:unpack and dependency:unpack-dependencies goal.
Have fun

Resources