Maven settings.xml not used - java

I am creating a maven module including a parent pom. This parent artifact is stored in a nexus maven repository. I added the nexus repository to my settings.xml.
Now if I am building my child module I am getting and error:
Could not find artifact test:my.parent:pom:1.0
It seems that the settings.xml is not used per default. If I add
<repositories><repository><id>random</id><url>http://test/repository/maven-releases/</url></repository></repositories> to my childs pom everything works fine, the settings.xml is used and the parent artifact is found.
Am I missing anything that the settings.xml will be used per default or is this the expected behaviour?
I do not want to add this random repo tag to every of my child poms.
Parent Pom:
<groupId>test</groupId>
<artifactId>my.parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
Modul Pom:
<parent>
<groupId>test</groupId>
<artifactId>my.parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>my.module</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
settings.xml
<settings>
<mirrors>
<mirror>
<id>Nexus</id>
<url>http://mynexus:8081/repository/maven-group/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<settings

As I know you can call maven by using IDE or command line.
If you are calling from IDE, check the settings if it is mapping to your settings.xml file
If you are using command line, you can check by using mvn --v and it shows you where your maven home is. Then you can check your settings file under conf/settings.xml

Related

Publish maven project to a local directory

I want to publish my maven project to a local directory but not in $HOME/.m2/repository. I have created the following directory structure:
$HOME/myrepo
|- third_party
+- private
The directory $HOME/myrepo/third_party holds, as the name suggests, external artifacts from 3rd parties. I want to publish my own projects into $HOME/myrepo/private. Therefore, I use the following adjusted $HOME/.m2/settings.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>maven-private</id>
<username></username>
<password></password>
</server>
</servers>
<mirrors>
<mirror>
<id>maven-local</id>
<url>file:///home/user/myrepo/third_party</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>
The file pom.xml for my test project looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>app</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<repository>
<id>maven-private</id>
<name>Internal Release Repository</name>
<url>file:///home/user/myrepo/private</url>
</repository>
</distributionManagement>
</project>
When I run
mvn install
The project compiles successfully and all required files are fetched from $HOME/myrepo/third_party, however, after compilation the project is always installed to $HOME/.m2/repository.
How do I have to configure maven in order to publish my projects into the $HOME/myrepo/private directory?
If you want to put the artifacts in a repository other than local, you are not thinking of installing but deploying them. Installing with maven by definition puts the artifacts in local repository - so unless you intend to overwrite the location of .m2/repository and all its contents, just run
mvn deploy
You can change the location of your local Maven repository, i.e. where artifacts are installed, by editing the Maven settings file ~/.m2/settings.xml. Specifically, you are interested in changing the <localRepository> to directory of your choice. One thing to look out for is that path you specify must be an absolute path according to the Configuring your Local Repository section of the user guide. Please also note that this will affect all artifacts (both your private and 3rd party ones) as Maven only supports a single local repository.
Example:
<settings>
<!-- settings -->
<localRepository>/path/to/local/repo/</localRepository>
<!-- more settings -->
</settings>

How to setup maven parent and child project as separate projects?

There is a project (type=pom), which is supposed to be used as a parent for another project.
parent
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>my-firm</groupId>
<artifactId>custom-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<distributionManagement>
<repository>
<id>lib-repo-local</id>
<name>my-releases</name>
<url>http://artifactory.local:8081/artifactory/libs-release-local</url>
</repository>
<snapshotRepository>
<id>lib-repo-snapshots</id>
<name>my-snapshots</name>
<url>http://artifactory.local:8081/artifactory/libs-snapshot-local</url>
</snapshotRepository>
</distributionManagement>
</project>
child
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>my-firm</groupId>
<artifactId>custom-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>child-sample</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<distributionManagement>
<repository>
<id>lib-repo-local</id>
<name>my-releases</name>
<url>http://artifactory.local:8081/artifactory/libs-release-local</url>
</repository>
<snapshotRepository>
<id>lib-repo-snapshots</id>
<name>my-snapshots</name>
<url>http://artifactory.local:8081/artifactory/libs-snapshot-local</url>
</snapshotRepository>
</distributionManagement>
</project>
The parent project is deployed to the remote repository - artifact my-firm:custom-parent:1.0.0 is available.
When I run mvn clean on the child project there's an error
[FATAL] Non-resolvable parent POM for my-firm:child-sample:0.0.1:
Failure to find my-firm:custom-parent:1.0.0 in https://repo.maven.apache.org/maven2
was cached in the local repository, resolution will not be reattempted until
the update interval of central has elapsed or updates are forced
and 'parent.relativePath' points at wrong local POM # line 7, column 11
All the three points in the error message seem to be unrelated to my intentions.
Maven does not try to look in the repo described in distributionManagement, but complaints about maven.central
Nothing is cached in local repository - all artifacts removed from there before the build.
The parent.relativePath is intentionally absent to have the child project agnostic to the parent project location and let it rely only on the deployed artifact (parent pom).
Please, show how to edit the poms to have child and parent as separate projects and let child to depend only on the parent artifact.
Please note that <distributionManagement> is only for upload. This is where Maven puts the artifacts if you run mvn deploy.
So if you want Maven to look into your artifactory for the parent POM, you need to add an appropriate <repository> element or -- which is the preferred way -- configure your artifactory in the settings.xml.
Nevertheless if you build the parent first on some machine and then build the child on the same machine, the parent POM is read from the local repository. It is not removed from there in any way. I don't know what went wrong in your case, but I guess you either had a different local repository for both builds or the content was somehow erased in between.

Maven submodule cannot find parent pom

I have a maven project structured like this:
parent
|- pom.xml
|- module1/ (extends parent)
| |- pom.xml
Inside the parent pom.xml:
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>0.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent POM</name>
<modules>
<module>module1</module>
</modules>
And inside the module pom.xml:
<parent>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>0.0.0-SNAPSHOT</version>
</parent>
When I do a install with the parent pom, I get an error:
Could not find artifact com.company:parent:pom:0.0.0-SNAPSHOT
When I install the parent pom first, then the entire project it works:
C:\dev\parent> mvn clean install -N
C:\dev\parent> mvn clean install
How do I configure maven to install the parent pom before any modules?
I've also attempted to restructure my project like in this answer but it is still not working: https://stackoverflow.com/a/9517053/4104760
When you execute Maven, it'll build the current pom and all its modules (recursive)
So it is only going down, it is not going back up to include the parents
It seems like you ran Maven like this
parent/module1> mvn validate (validate is enough to see the effect)
Running it like this will trigger both pom files:
parent> mvn validate

Import Maven local .jar as a lib

I made a simple Maven project for my class. According to the teachers tutorial we cannot upload it to our school repo due to some server issues, so we have to store it locally using altDeploymentRepository. I have the following pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dpp</groupId>
<artifactId>simple_lib</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<altDeploymentRepository>internal.repo::default::file://${project.basedir}/../${project.name}-mvn-repo</altDeploymentRepository>
</configuration>
</plugin>
</plugins>
</build>
</project>
So in the directory with my Maven project I have two directories:
sample_lib
sample_lib-mvn-repo
In the second one, deep down in : sample_lib-mvn-repo\com\dpp\sample_lib\1.0-SNAPSHOT I have a .jar file which I want to import (but not using just .jar file like passing the path to it - I need to do this "Maven way", import it as Maven lib). Can I do it if the file is not stored on any remote repository, but on my hard drive?
Running simply mvn install will install the file in your local repository. The local repository, by default, is in your home directory, under .m2\repository.
Using your pom above, after running mvn install, you would have jar (and some other files) in .m2\repository\com\dpp\sample_lib\1.0-SNAPSHOT.
To import this subsequently in another project, you would create a dependency in that project's pom like:
<dependency>
<groupId>com.dpp</groupId>
<artifactId>simple_lib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
This all takes place only on your machine, and will not use any remote repository.
Now you have a local simulation of a repository.
You can import it using the repository tag as described in https://maven.apache.org/pom.html#Repositories. To specify a file make it a file url like file:
Yes, you can add maven repository and point it to a local directory:
<repository>
<id>local</id>
<name>local</name>
<url>file:${user.dir}/sample_lib-mvn-repo</url>
</repository>
https://maven.apache.org/guides/introduction/introduction-to-repositories.html
Given that your jar file is here sample_lib-mvn-repo\com\dpp\sample_lib\1.0-SNAPSHOT.jar, you then can add it as a dependency:
<dependency>
<groupId>com.dpp</groupId>
<artifactId>sample_lib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
It's not exactly what you're asking. But a quick and dirty solution is to make a POM project (no source code).
Inside the POM project you have the main and external projects.
You can simply make a dependency on the other project.

Could not find parent artifact xxxxx:pom:2.0-SNAPSHOT

I have a parent project xxx-third, which has nothing but a pom declaring some dependencies and build configurations.
After I deploy it to the nexus and I can find it in the nexus web,I declare this xxx-thrid as my parent project of myProject.
Howerver, I still have the maven error complaining that Cound not find artifact com.myCom:xxx-third:pom:2.0-SNAPSHOT # com.myCom:myProject.
But if I do install locally with the code, the problem will resolve, this is annoying! Why and how to solve it?
It is because this artifact is in your local .m2 repository on your local machine, but can't be downloaded from remote respository by your build server.
The local repostiory is in path:
{USER_HOME}\.m2\repository\
You can copy the required dependency from your local repository to the local repository on your build server, then it will read the jar dependecy from local repo instead of fetching it from remote repo.
Your parent POM is not resolved. There are three options to solve this:
1) Add the parent POM to your local repository
2) Add a relativePath to your child POM pointing to your parent POM (only makes sense in a multi-module project where source is in the same repository)
<parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<relativePath>...</relativePath>
</parent>
3) Add a repository configuration to your settings.xml (e.g. makes sense if you have a company wide POM)
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<profiles>
<profile>
...
<repositories>
...
</repositories>
...
</profile>
</profiles>
...
As far as I understand 3) is your solution. See also:
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
https://maven.apache.org/settings.html

Categories

Resources