Maven - creating and sharing packages between projects - maven

If I am working for a large organization, when is a situation where I would want to create my own Maven packages and share them with other projects inside my organization?
As I understand it a maven Package is just a maven project that you mvn deploy, then you can add the JAR to other projects. Is that it, or is there more to it?

You'll need an artifact repository such as Artifactory or Nexus. Once you setup artifact repo, you'll have to configure your projects to deploy artifacts there. This is done through your pom.xml file. Then when you run mvn deploy the artifacts will be published to your repo.
Other projects can declare your artifacts as dependencies the same way you do with any other dependencies, but to resolve them they need to know about your artifact repo. This is done in settings.xml

Related

Maven artifact not uploaded with pom file

I have a backend multimodule maven project, that is build on jenkins with mvn clean package command (maven version 3.3). After a sucessfull build I upload two of the maven subprojects target/*.jar to private nexus (using Nexus artifact uploader plugin). The jars from target folders are uploaded correctly, but the pom is not (there is no option in the plugin to upload the pom).
Then I have my webapp project, where I have dependency to the two artifacts on my private nexus. They are found and downloaded, but not the pom. Building the webapp project fails, because the dependencies are not resolved for the two jars. A warning is printed out during build:
[WARNING] The POM for <groupId>:<submodule-artifactId>:jar:1.0.0 is missing, no dependency information available
That is reasonable - I can clearly see in nexus repository that it has only jar, md5 and sha files. How should I build my multimodule maven project in a way I would be able to reference only submodules in my webapp project? Or should I upload the submodule projects poms manualy?
I am open to upload the whole backend project to nexus, but I would like to be able to add only subprojects as dependencies to my webapp.
Since you are using a private nexus, you need to specify in your maven where to look for your artifact. Usually the way to do it is to specify a repository configuration in your pom.xml as specified in this link. This will tell maven to contact your private nexus for your artifacts.

How to download only one jar with pom.xml?

I want to use a project called projectA that I have in a maven repository as dependency of a projectB.
The mvn deploy of projectA is successful (I can see in the repository the projectA-0.0.1-20190902.072951-1.jar, projectA-0.0.1-20190902.072951-1.pom and maven-metadata.xml files), but when I specify the dependency in the pom.xml file of projectB, the project works but it downloads two JARs of projectA from the repository:
- projectA-0.0.1-20190902.072951-1.jar
- projectA-0.0.1-SNAPSHOT.jar and the same issue for every file downloaded by the dependency to this project.
I think that only one JAR is necessary, and I don't know what I need to put maybe in settings.xml or in the pom.xml file of any project to get only one JAR when the dependency is downloaded.
Thank you so much for your help!
In the past it was possible to tell Maven to deploy Non-unique Snapshot Deployments but in Maven 3
... snapshot artifacts will always be deployed using a timestamped version
So:
the files with the timestamps are the one you have deployed to your remote repo (mvn deploy ...) and then downloaded from there as dependencies
the ones with -SNAPSHOT are the result of the local builds (install goal)
If your concern is download traffic - that is not an issue. The -SNAPSHOT ones are not downloaded.
If your concern is disk space then you can have a cron job or use something like Build Helper Maven Plugin to remove old version artifacts from the local repository.

Maven - do a mvn install on local artifact when packaging

i have a maven project which has some local artifacts as dependencies.
When I have to package my main application, i have to do a mvn install command on my local repositories before, which is quite annoying and easy to forget.
Is there a way to tell maven to install local repositories when packaging the main one?
One solution can be to wrap up all your components into a pom project.
When building a parent pom maven automatically triggers a build of all the sub-modules.

How to remove maven artifcat completely from SpringSource?

I have a local maven repository and installed a custom artifact. It works if i reference it in other projects. But now i want to use a server for a "own maven repository". If i delete the artifact from the local maven repository, i assumed that the project will not build when i do a maven clean and maven force update dependencies. The artifact cannot be found under .m2/ but Spring Source Tool Suite still can add the artifact to new Java Projects. Create New Java Project -> Edit Pom -> Maven Artifact is added, even if i deleted it from local repository .m2/ . How is this possible and how can i delete it completely, to be able to test if now all dependencies are updated from my server with the .m2/settings.xml configuration?
Your repository is just a directory/file structure. Go to your local repo, find the path (the group id is the path), and delete from the place where you start to see version numbers. When you rebuild, the artifact should be downloaded/replaced from your server/repo.

How to install a Maven/Gradle project along with all its dependencies to a local repository?

I have a Gradle project that depends on several open-source projects up on Maven Central. I'd like to install the project – along with all its direct and transitive dependencies – to my local maven repository, so that I could later zip it all up and put it on offline machines.
How do I do it with Gradle/Maven?
mvn dependency:get plugin will fetch the artifact with all dependencies to the local repository.
I had also developed a plugin to install remote artifacts to a local machine.
If you want to later ZIP up your project w/ dependencies and move them to a different machine, you could try Maven's appassembler plugin. It collects all dependencies and creates a launcher, all in the target folder, ready for deployment.
But note, this, by default, creates a flat directory structure with all dependencies, it doesn't preserve the Maven format. It also has the option to create a repository.

Resources