Maven downloading dependencies into project root instead of home/.m2 - maven

Whenever I run mvn in my project dir it always downloads the dependencies into that dir... I want it to download them into the main repo (home/.m2 )

After a quick google search : how-to-get-the-maven-local-repo.
Run mvn -X and check on the first the line the one that start with:
[DEBUG] Using local repository at *YOUR_LOCAL_REPOSITORY*
If it is not the folder you want, you can change the value in your global settings or your user settings.
[DEBUG] Reading global settings from *YOUR_GLOBAL_SETTINGS*
[DEBUG] Reading user settings from *YOUR_USER_SETTINGS*
To change the value you have to add or update the element "localRepository" in one or both of the above settings file.
<localRepository>/path/to/local/repo/</localRepository>
Maven - Guide to Configuring Maven

Related

Vscode with custom settings.xml Maven file per project in a workspace

I have a Vscode workspace with a bunch of Maven projects, and which one has its own Maven settings.xml file.
As each project has its own settings file with the same name but in its own root folder I got success configuring both the workspace as each of the projects with such an option: {"maven.executable.options": "-s my_settings.xml"}.
Although, I can run successfully any of Maven commands for all projects, Vscode reports problems in the pom.xml files about missing artifacts and the same errors are reported when I run tests from the "Testing" in the Side Bar.
When I copy one of the project's settings file to the ~/.m2/settings.xml that given project works fine, no errors at all.
So, I conclude Vscode doesn't respect the "maven.executable.options" for all of project operations inside IDE, like tests and maybe others operations but only for directly launched Maven commands.
I don't want to merge all those settings.xml files into a big one in my local ~/.m2/settgins.xml each time I have to open a given set of projects.
Is there a way to teach Vscode to use different settings.xml for each project and make it work properly in all Vscode operations (not only Maven commands)?
VSCode setting ID: java.configuration.maven.userSettings
In VSCode, go to Preferences > Settings > Workspace
Search 'maven'
Set 'Java > Configuration > Maven: User Settings' to ./settings.xml to make VSCode use the local settings.xml of the project
Restart VSCode if needed
Screenshot
you can use
mvn --settings settings.xml clean install
to individually point respective settings.xml for each java app
First, check whether the terminal executes the command "mvn ... -s my_settings.xml", which indicates the correctness of vscode-maven plugin. If so, the problem comes from the command itself.
A workaround is to delete the global maven settings.xml, typically in $MAVEN_HOME\conf\settings.xml. Then mvn ... -s my_settings.xml should work. It works fine for me.
The reason may be that when global maven settings file is found, mvn CLI may ignore the settings file in mvn option.

How to create multiple Maven m2 repositories for single user

I am setting up 2 jenkins instances on same server. I would like to create 2 local maven repositories for both jenkins seprately. Jenkins 1 is already set up and operational and I woudnt like to touch it.
Can we have 2 local maven repositories for single user as both jenkins are running as the same user ?
Is there any way I can point maven from jenkins to the new repository ?
Thanks
AI
Thanks for your replies, I figured it out, every maven command I am running, I am adding extra parameter
-Dmaven.repo.local=/path/to/alternate/local/repository/
and it overrides default local maven repository.
Cheers,
AI
You can specify a local repository in the settings.xml under <settings><localRepository>.
Alternatively, you can also specify it on the command line as described here:
https://stackoverflow.com/a/7071791/927493
Maven configuration occurs at 3 levels:
◾ Project - most static configuration occurs in pom.xml
◾ Installation - this is configuration added once for a Maven installation
◾ User - this is configuration specific to a particular user
Maven reads the settings from the settings.xml file which can be located in ${M2_HOME}/conf/settings.xml, as well as ${user.home}/.m2/settings.xml
<settings>
...
<localRepository>/path/to/local/repo/</localRepository>
...
</settings>
The default value for the local maven repository is ${user.home}/.m2/repository/
Even though you are are constrained in that you are using the same use for both Jenkins, remember the jobs run on nodes and those can be launched by different users (if via SSH), but it's that node's user's ${user.home}, not the user running Jenkins itself which is read (See Node note below).
What's the simplest way to get two different <localRepository> for a given user?
Jenkins has a Global Tool Configuration (${JENKINS_URL}/configureTools) for Maven:
Further down the the same page you must configure the Maven installations
You could choose the Global Tool Configuration | Maven Configuration to NOT use the default maven settings (the two mentioned above) and specify one from the filesystem:
One one instance, choose ${user.home}/.m2/settings.J1.xml, and on the other, choose ${user.home}/.m2/settings.J2.xml
Alternatively, you could even choose two different "Maven installations", with a different MAVEN_HOME, then have a different ${M2_HOME}/conf/settings.xml in each (awkward, but sometimes useful).
JOB SPECIFIC REPO?
But, if disk space is not really an issue, you could go a step further and give every single job its own private local repository. This is especially handy when building shared libraries in parallel, parallel branches and other scenarios using -SNAPSHOT. Under the Advanced ... options in the maven step, select [ X ] use private Maven repository. That repository ends up residing in ${WORKSPACE}/.repository. Suggest adding the Workspace Cleanup plugin to help manage your space.
You can also, on a per-job basis, specify a specific file-system settings.xml.
NODE SPECIFIC REPO?
Also, each Node has its own Node Properties which again let you customize the "Tool Locations". You can override the "(Maven) Home" location here (but not settings location).
[
PIPELINE
All this is also supported and configurable if using a Pipeline, as described in the Pipeline Maven Integration Plugin.
Extra Plugins
Finally, if you don't like the idea of having all these settings.xml lying all over the filesystems, you can install the Config File Provider plugin which lets you store the custom settings.xml within Jenkins. After installation, the Global Tool Configuration (shown) and Job steps now has the added option to choose from the "provided settings.xml" options you create:

Can we configure settings.xml to our own location in maven

Recently I am working with Apache Maven in Windows machine.
I am executing maven from Command Prompt every time.
I know that MAVEN will pick its settings.xml from C:\Users\abc\.m2\settings.xml
Question: Can't I change this settings.xml to be picked from my own provided location instead of default location, like C:\Users\abc\.m2\settings.xml
That is, instead of C:\Users\abc\.m2\settings.xml location I will configuring it in F:\myFolder\settings.xml.
If so, what are the steps I need to follow and check?
You can change the location of user settings using the -s option from the command line:
-s,--settings <arg> Alternate path for the user settings file
You could hence invoke Maven as following:
mvn clean install -s F:\myFolder\settings.xml
If you don't want to specify it at each and every invocation, since Maven 3.3.1 you can specify a .m/config folder in your project folder and a maven.config file in it providing this option.
The .m/config/maven.config file would have in your case the following content:
-s F:\myFolder\settings.xml
And it will be applied to all Maven invocation for that project.

~/.gradle/gradle.properties file not being read

There is a similar question here: Gradle properties not being read from ~/.gradle/gradle.properties but it does not solve my problem.
It seems to me that gradle is NOT reading my ~/.gradle/gradle.properties file.
I have a gradle.properties file in ~/.gradle, and it has properties needed to sign artifacts before uploading to maven central. It looks like this:
signing.keyId=12345678
signing.password=myPassword
signing.secretKeyRingFile=/home/me/.gnupg/secring.gpg
sonatypeUsername=me
sonatypePassword=myOtherPassword
When I try to build my project, it complains that there's no sonatypeUsername property, thus:
> Could not find property 'sonatypeUsername' on root project 'yourProject'.
Here's the relevant portion of my project's build.gradle:
uploadArchives {
repositories {
mavenDeployer {
// lots of non-interesting things here
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: project.property("sonatypeUsername"), password: project.property("sonatypePassword"))
}
}
}
}
When I try to build the project with debugging, here's what I see regarding properties:
$ ./gradlew --stacktrace --debug build
[INFO] [o.g.BuildLogger] Starting Build
[DEBUG] [o.g.BuildLogger] Gradle user home: /home/me
[DEBUG] [o.g.BuildLogger] Current dir: /home/me/dev/yourProject
[DEBUG] [o.g.BuildLogger] Settings file: null
[DEBUG] [o.g.BuildLogger] Build file: null
[DEBUG] [o.g.i.b.BuildSourceBuilder] Starting to build the build sources.
[DEBUG] [o.g.i.b.BuildSourceBuilder] Gradle source dir does not exist. We leave.
[DEBUG] [o.g.i.DefaultGradlePropertiesLoader] Found env project properties: []
[DEBUG] [o.g.i.DefaultGradlePropertiesLoader] Found system project properties: []
[DEBUG] [o.g.a.i.a.m.DefaultLocalMavenRepositoryLocator] No local repository in Settings file defined. Using default path: /home/me/.m2/repository
[DEBUG] [o.g.i.ScriptEvaluatingSettingsProcessor] Timing: Processing settings took: 0.286 secs
[INFO] [o.g.BuildLogger] Settings evaluated using empty settings script.
[DEBUG] [o.g.i.ProjectPropertySettingBuildLoader] Looking for project properties from: /home/me/dev/yourProject/gradle.properties
[DEBUG] [o.g.i.ProjectPropertySettingBuildLoader] project property file does not exists. We continue!
[INFO] [o.g.BuildLogger] Projects loaded. Root project using build file '/home/me/dev/yourProject/build.gradle'.
The problem was that I made an assumption that wasn't true. If you look at section 14.2 of the gradle documentation, it says:
You can place a gradle.properties file in the Gradle user home directory (defined by the “GRADLE_USER_HOME” environment variable, which if not set defaults to USER_HOME/.gradle) or in your project directory.
My incorrect assumption was that USER_HOME just defaulted to the standard linux HOME environment variable. This is not true.
As soon as I export USER_HOME=$HOME in my ~/.bashrc everything works
By default, without setting GRADLE_USER_HOME, it should work. I tested it in v3.5.
But make sure if your are running it as the right user. For ex, if you do your ./gradlew build using sudo, then gradle.properties in your home folder will not be picked up.
To make sure the default gradle user home, you can run gradle with the --debug option and look out for the below line,
[DEBUG] [org.gradle.internal.buildevents.BuildLogger] Gradle user home:
I am using Windows and I was experiencing the same issue. I noticed that my ~/.gradle/gradle.properties file had a byte-order-marker (BOM) at the beginning and I think it had windows line endings (\r\n). I recreated my ~/.gradle/gradle.properties file by deleting it and then making a new one with vim with unix line endings (\n) with the same content as before and then it started working. I don't know if it was the BOM, line-endings, or the combination of the two that was causing the issue, but I thought I should share this in case someone else has the same cause for this issue.
A quick and dirty solution is just to simply put the gradle.properties right next to your build.gradle, that will guarantee it will be read. I realize this doesn't solve a single centralized source of common properties, but at least it's something.
I did check Proxy Settings in Android Studio and i had No Proxy selected.
I did Invalidate Cache and Restart with no luck.
I added an exception in Firewall, still no luck.
In project gradle.properties i have no proxy configurations. Its empty, only comments.
In a previous version of Android Studio i had enabled Global Proxy Setting and it was a file stored in my user profile %userprofile%\.gradle\gradle.properties
Removing proxy lines in this file, solved my issue. I had to restart Android Studio to make it work.
:)
I myself stumbled upon this question when I was struggling with apk building via gradlew.
I tried setting Gradle_user_home earlier for the mentioned error in question, but it was of no use, below solution worked for me.
I have below configuration for gradle-wrapper.properties :
*distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip*
gradlew itself at run time downloads the correct distribution of gradle from above config (distributionUrl). Nothing else is required.
I faced the same issue and solved it by using gradle binary, to build, instead of wrapper that comes with android project. You can install gradle from here: https://docs.gradle.org/current/userguide/installation.html#installation
Then used system-wide gradle to build, while on project root, instead of wrapper, like ex:
$ gradle installDebug
I made a different mistake. Why did i set up this field in "intelligent idea"? :) However when i wrote correct value in this field I get correct reading gradle.properties from .gradle
some setting in Idea
Oh I noted that the "project property file does not exists. We continue!" is shown because the script will search for gradle properties in different routes.
it will check both in root/gradle.properties and in root/app/gradle.properties, so it will only add the ones from the one found and display the "error" from the other one.
However in the log you can actually see the properties that were added from the read files:
Adding project properties (if not overwritten by user properties): [android.nonTransitiveRClass, kotlin.code.style, org.gradle.jvmargs, android.useAndroidX]

No archetypes currently available. The archetype list will refresh when the indexes finish updating

In Eclipse with m2eclipse installed, when I choose
File > New > Project... > Maven > Maven Project
with
Use default Workspace location
and
Catalog: Nexus Indexer
selected, the following error message is showing up:
No archetypes currently available. The archetype list will refresh when the indexes finish updating.
Why does this error occur?
I had same problem and it was solved by adding a remote catalog:
http://repo1.maven.org/maven2/archetype-catalog.xml
This solution i found at :
http://jira.talendforge.org/secure/attachment/36787/DeveloperGuide-Feedback.pdf
in this case you can use another solution, you need to configure a local ctalogue using eclipse :
1- suppose that you need to use mave catalogue : http://repo1.maven.org/maven2/archetype-catalog.xml
2- download from your web broser the file http://repo1.maven.org/maven2/archetype-catalog.xml and save it in ~/.m2/archetype-catalog.xml
3 now go to eclipse "Window-->Preferences-->maven-->Archetypes" then click the "Add Local catalogue" and set the ctalogue file to the ~/.m2/archetype-catalog.xml, save and exit
4- now go back to eclipse and try to create project using your new local catatlogue archetypes, it should work
Updated remote catalog link: https://repo1.maven.org/maven2/archetype-catalog.xml
Either copy the link and add a remote archetype or download the XML file and go for a local archetype option and follow #sofiene-zaghdoudi 's solution.

Resources