Finding archived maven artifacts when using internal repository - java

Maven is used in Java projects as a dependency manager.
There's Artifactory acting as internal repository and caching proxy for Maven central repository. All maven projects are configured to use it as repository and plugin-repository in their pom.xml.
There's all virtual repository inside Artifactory containing all other repositories (releases, snapshots, archive).
Archive repository contains some (probably old and unused) archived artifacts which should be deleted, but some of them are used.
How to detect which dependencies are used by java projects, but already archived or missing from archive also?
The ultimate goals are:
update versions of such dependencies in pom.xml and delete old versions from archive
restore missing dependencies from archive which are used by java projects and accidentally were deleted from archive. The only reason builds are passing is dependencies' presence in build server's .m2 local cache.
My solution so far, run Jenkins nightly job which automates the following steps:
create non-archived virtual repository in Artifactory which doesn't
contain archive repository, but contains all others.
create custom maven environment ( extract maven installation archive)
and configure it with the following settings.xml. Custom
environment is created to prevent finding the missing artifact in
build servers .m2 folder:
<localRepository>localTempFolder</localRepository>
<mirrors>
<mirror>
<id>noarchive</id>
<name>No archive mirror for csi-all</name>
<url>non-archived/url>
<mirrorOf>all</mirrorOf>
</mirror>
</mirrors>
run path_to_custom_maven dependency:list inside some java project.
Command returns maven missing dependencies (in non-archived repo, but present in archive or missing in archive and present in build server .m2 folder) in the errors.
However, this way is slow because .m2 cache is not used and i'm not sure it's the most correct way.
How to find missing artifacts in non-archived repo while utilizing .m2 cache?

Related

Instead of Maven Repo, usage of .m2 directory

I have worked in the offline development environment.
First, I downloaded dependencies from the maven repository via the maven central repository. After, moving the .m2 folder to the offline development environment is it possible to work without a maven repository
You should read https://maven.apache.org/guides/mini/guide-configuring-maven.html.
You may pass the path to a local repo via -Dmaven.repo.local=/path/to/repo. If you use it regularly you should probably add an alias for mvn.
Or you add <localRepository>/path/to/local/repo/</localRepository> to your build file.
Maven should look in the local repo first anyway (default is ~/.m2/repository). So if you have all the required dependencies in the required version there maven should not make any requests to the outside wordl. But that's just a guess.

How to include local artifacts with Maven dependency:go-offline

I'm trying to generate an offline local maven repository folder for my project, which includes both remote dependencies, and a few dependencies to local projects.
When I run mvn install in my project, Maven succeeds in resolving both remote and local dependencies. Now, in order to have all dependencies available offline, I want to have a local repository folder in my project. Using the command mvn dependency:go-offline -D"maven.repo.local"="./maven-local" I try to achieve this. However, Maven manages to place all remote dependencies in the local folder, but not the local dependencies to my local projects (which have been installed already).
The error I get is:
Failed to execute goal on project genericgateway: Could not resolve
dependencies for project org.my:ownproject:jar:1.0.1: The following
artifacts could not be resolved: org.my:otherproject:jar:1.0.1: Could
not find artifact org.my:otherproject:jar:1.0.1 in central
(https://repo.maven.apache.org/maven2)
How can I tell Maven to also search in my local ~/.m2 repository for these projects?
Basically you should work with one local repository for compilation of maven project.
So, you have three options:
Option 1
Don't specify a local repo while executing mvn dependency:go-offline. Let it download into you regular local repo (~/.m2 by default)
Install the local artifact there as well and compile against this repository.
Option 2
Compile local projects into ./maven-local with maven.repo.local flag as you did. The point is that that both local and downloaded artifacts will be in the same repo.
If you want, you can configure a local repo in the settings.xml (see this answer
Option 3
IMO Overkill for local dev env, but still...
If you absolutely have to separate the repositories you can install a software like Nexus/Artifactory locally - it can provide a flexible repository management and then configure different repositories, but then again, it will be like a remote repo residing in your local PC, maven will create a local cache with both local and remote artifacts, still in the same repo.

Quickest POM settings to turn an existing Eclipse web project in a Maven-managed project?

I'm converting an existing Eclipse-based web project to a Maven-managed one.
Since the project has lots of dependencies, many of which are custom (they're either internally made or they've been taken from sources that have no public repository), is there some 'magic' Maven POM setting that will let me load every jar from WebContent/WEB-INF/lib and make the project work as before right now, so that I can configure each dependency and do the necessary refactoring to turn it to a proper Maven project with a little more time and care?
I have already seen this question, but the project must continue to compile inside Eclipse, so - or at least I guess - it is not just a matter of using the Maven war plugin
What you want to do is called "installing" your non-mavenized JARs into your maven repository. This can be a local or remote repo that you host.
The command to install to your local repo is something like this: mvn install:install-file -Dfile=My-lib.jar -DgroupId=com.mycompany -DartifactId=My-lib -Dversion=1.2.3 -Dpackaging=jar
You'll want to review the various options for install to suit your project.
Once the non-mavenized dependencies are installed to your repo you can add them to your pom like any other maven dependency. They will be fetched from your local repo.
You will have to set up your own remote repo (like Artifactory) or install each plugin for every developer and CI server in your environment for others on your team to build the project. I strongly reccomend Artifactory, it makes it easy on your and your team to use maven and get dependencies.

Maven and m2e - build jar with dependencies information

We are using Maven and m2e tools for our development and today we encountered a problem.
One of our projects is small library that is required for other projects, so we packaged it into jar file and put in our private Maven repository.
For now, all of the jars that we put in this repository didn't have any external dependencies, but this library I mentioned uses some external jars.
Now, when I add information about this jar to other poms, this jar is downloaded from our private repository but Maven doesn't download dependencies needed by this jar.
I am wondering if I need to use some special target/add something to my pom.xml file that will inform Maven to include information about dependencies needed by this artifact.
EDIT:
Here is the workflow I perform when I upload jar to our private repository:
1.I generate jar file from Eclipse using m2e
2.mvn install:install-file -DgroupId=<your_group_name> -DartifactId=<your_artifact_name> -Dversion=<snapshot> -Dfile=<path_to_your_jar_file> -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true
3.I copy folder created in my local repository to remote repository
If your small library is a maven project as you state, there should be no reason to have eclipse build the jar and then use maven to install it and then manually copy to the remote repo. Instead you should use m2e to run the deploy goal:
mvn deploy
That will cause the jar to get built and then install it directly into your local maven repo then deploy it to the remote repo.
In eclipse this can be accomplished by right clicking your project, choosing Run As -> Maven Build... then in the run configuration window for Goals input type deploy then click Run. After this has been done once, you can just use Run As -> Maven Build to run the same config again.
I see you use -DgeneratePom=true during the installation of the jar file. What you need to do is create a pom.xml for your artifact. In the pom.xml, you can specify the dependencies that your jar file requires. When executing the install:install plugin goal, you use -DpomFile=pom.xml instead.
The best way to do this is to run mvn deploy
You have to setup the distribution repository to your private artifact manager (nexus or artifactory) in your settings.xml
see this for more details

What is a Maven artifact?

What is an artifact and why does Maven need it?
An artifact is a file, usually a JAR, that gets deployed to a Maven repository.
A Maven build produces one or more artifacts, such as a compiled JAR and a "sources" JAR.
Each artifact has a group ID (usually a reversed domain name, like com.example.foo), an artifact ID (just a name), and a version string. The three together uniquely identify the artifact.
A project's dependencies are specified as artifacts.
In general software terms, an "artifact" is something produced by the software development process, whether it be software related documentation or an executable file.
In Maven terminology, the artifact is the resulting output of the maven build, generally a jar or war or other executable file. Artifacts in maven are identified by a coordinate system of groupId, artifactId, and version. Maven uses the groupId, artifactId, and version to identify dependencies (usually other jar files) needed to build and run your code.
I know this is an ancient thread but I wanted to add a few nuances.
There are Maven artifacts, repository manager artifacts and then there are Maven Artifacts.
A Maven artifact is just as other commenters/responders say: it is a thing that is spat out by building a Maven project. That could be a .jar file, or a .war file, or a .zip file, or a .dll, or what have you.
A repository manager artifact is a thing that is, well, managed by a repository manager. A repository manager is basically a highly performant naming service for software executables and libraries. A repository manager doesn't care where its artifacts come from (maybe they came from a Maven build, or a local file, or an Ant build, or a by-hand compilation...).
A Maven Artifact is a Java class that represents the kind of "name" that gets dereferenced by a repository manager into a repository manager artifact. When used in this sense, an Artifact is just a glorified name made up of such parts as groupId, artifactId, version, scope, classifier and so on.
To put it all together:
Your Maven project probably depends on several Artifacts by way of its <dependency> elements.
Maven interacts with a repository manager to resolve those Artifacts into files by instructing the repository manager to send it some repository manager artifacts that correspond to the internal Artifacts.
Finally, after resolution, Maven builds your project and produces a Maven artifact. You may choose to "turn this into" a repository manager artifact by, in turn, using whatever tool you like, sending it to the repository manager with enough coordinating information that other people can find it when they ask the repository manager for it.
Hope that helps.
Maven organizes its build in projects.
An artifact in maven is a resource generated by a maven project. Each maven project can have exactly one artifact like a jar, war, ear, etc.
The project's configuration file "pom.xml" describes how the artifact is build, how unit tests are run, etc.
Commonly a software project build with maven consists of many maven-projects that build artifacts (e.g. jars) that constitute the product.
E.g.
Root-Project // produces no artifact, simply triggers the build of the other projects
App-Project // The application, that uses the libraries
Lib1-Project // A project that creates a library (jar)
Lib2-Project // Another library
Doc-Project // A project that generates the user documentation from some resources
Maven artifacts are not limited to java resources. You can generate whatever resource you need. E.g. documentation, project-site, zip-archives, native-libraries, etc.
Each maven project has a unique identifier consiting of [groupId, artifactId, version]. When a maven project requires resources of another project a dependency is configured in it's pom.xml using the above-mentioned identifier. Maven then automatically resolves the dependencies when a build is triggered. The artifacts of the required projects are then loaded either from the local repository, which is a simple directory in your user's home, or from other (remote) repositories specified in you pom.xml.
Q. What is Artifact in maven?
ANS: ARTIFACT is a JAR,(WAR or EAR), but it could be also something else. Each artifact has,
a group ID (like com.your.package),
an artifact ID (just a name), and
a version string. The three together uniquely identify the artifact.
Q.Why does Maven need them?
Ans: Maven is used to make them available for our applications.
An artifact is a JAR or something that you store in a repository. Maven gets them out and builds your code.
To maven, the build process is arranged as a set of artifacts. Artifacts include:
The plugins that make up Maven itself.
Dependencies that your code depends on.
Anything that your build produces that can, in turn be consumed by something else.
Artifacts live in repositories.
usually we talking Maven Terminology about Group Id , Artifact Id and Snapshot Version
Group Id:identity of the group of the project
Artifact Id:identity of the project
Snapshot version:the version used by the project.
Artifact is nothing but some resulting file like Jar, War, Ear....
simply says Artifacts are nothing but packages.
Usually, when you create a Java project you want to use functionalities made in another Java projects.
For example, if your project wants to send one email you dont need to create all the necessary code for doing that. You can bring a java library that does the most part of the work.
Maven is a building tool that will help you in several tasks. One of those tasks is to bring these external dependencies or artifacts to your project in an automatic way ( only with some configuration in a XML file ).
Of course Maven has more details but, for your question this is enough.
And, of course too, Maven can build your project as an artifact (usually a jar file ) that can be used or imported in other projects.
This website has several articles talking about Maven :
https://connected2know.com/programming/what-is-maven/
https://connected2know.com/programming/maven-configuration/
An artifact is an element that a project can either use or produce. In Maven terminology, an artifact is an output generated after a Maven project build. It can be, for example, a jar, war, or any other executable file.
Also, Maven artifacts include five key elements, groupId, artifactId, version, packaging, and classifier. Those are the elements we use to identify the artifact and are known as Maven coordinates.
read it from here

Categories

Resources