Instead of Maven Repo, usage of .m2 directory - java

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.

Related

Finding archived maven artifacts when using internal repository

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?

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.

How to add java library without no repository to pom file?

I'd like to add one project A as my dependency, but unfortunately, there's no repository host this library. I know that I can install it to local repository manually, then refer this in pom file. But I have a travis build job where there's no such artifact, is there any way that I can install this library to local repo automatically ? Thanks
I would recommend to use the clean approach and uploading this library into your own repository. If you don't have one: time to get one running.
If you're really not up to this task the maven install plugin: http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html can install a jar in the local repository. This will work both locally and on a CI server.
To upload a jar in a remote repository there is the deploy plugin: http://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html
If you bind the execution of this plugin to a very early phase in the maven life-cycle (validate) you might be able to avoid a build step required prior of your own build.

Download a Maven artifact and install into a specific local repository (not .m2)

I have several local Maven repositories besides the one located in ~/.m2 directory and I want to simplify the process of installing new artifacts into them from Maven central.
So far I couldn't find a way to tell mvn dependency:get that dependency should be put into a specific local repository.
I did manage to find a way to install a given downloaded artifact using mvn install:install-file -DlocalRepositoryPath=, but I want to be able to get and put dependencies into a specific repository with as few manual steps as possible.
Before you ask why can't I just configure my project to use Maven central directly here is the answer: the project uses Gradle and I do not own its build script (i.e. I can't modify it). The project build script is written to work with several distinct repositories having the same base URI that I fortunately can change using build.properties file. So my idea is to have several local maven repositories in the same root directory and trick the build script to use them.
You can use the maven.repo.local property:
mvn dependency:get -Dmaven.repo.local=/path/to/localrepo

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.

Categories

Resources