Maven submodule cannot find parent pom - java

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

Related

Maven not using newer locally built/installed dependency

I've been hitting an annoying issue recently. I have two different maven projects checked out to my development machine. One project depends on the other (let's say Project A depends on Project B), and I actively make changes to both projects. Sometimes though, Project A won't pick up the latest Project B changes. Let's say I make some changes to Project B, I build/install it with...
mvn clean install
I even check my local ~/.m2/repository to see that the jar has been updated. But Project A will still continue to use an older version of Project B. Even though it was just updated... If I remove the entire Project B folder, as in...
rm -rf ~/.m2/repository/project-b/version/
And then build/install Project B again, then at this point my problem is gone. Project A will finally make use of the updated Project B. But I don't want to have to go through this exercise every time. Any clues what could be causing this?
Edit: Here's more or less the relevant parts of the pom.xml for both projects. It's extremely basic.
Project A pom.xml
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.opendaylight.mdsal</groupId>
<artifactId>binding-parent</artifactId>
<version>3.0.10</version>
<relativePath/>
</parent>
<groupId>company.group</groupId>
<version>1.0.0-SNAPSHOT</version>
<artifactId>project-A</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>company.group</groupId>
<artifactId>project-B</artifactId>
<version>3.1.0-SNAPSHOT</version>
</dependency>
...
</dependencies>
</project>
Project B pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>company.group</groupId>
<artifactId>project-B-parent</artifactId>
<version>3.1.0-SNAPSHOT</version>
</parent>
<groupId>company.group</groupId>
<artifactId>project-B</artifactId>
<version>3.1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<dependencies>
...
<dependencies>
</project>
Since you are using IntelliJ in the right upper corner there is this maven menu, where you can reimport all dependencies which helps me in this case :)
Try below maven command for loading all updated libraries,
mvn clean install -U

Resolve maven dependencies from child pom

I'm quite new to maven and I have a maven multi module project with the parent pom as
<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.test.cit</groupId>
<artifactId>cit</artifactId>
<version>LATEST-SNAPSHOT</version>
<name>Integration Test Framework</name>
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>core</module>
<module>login</module>
</modules>
</project>
I have put all the relevant external dependencies to the child poms of common, core and login. I then converted the project to an eclipse project (mvn eclipse:eclipse) and after that eclipse is unable to resolve the dependencies in the child pom though the respective jars are present in M2_HOME.
I then added all the dependencies to the parent pom(Whatever dependency was there in the child poms) from the child poms and then eclipse was able to resolve that.
I'm confused of this behavior. Since I've already added the external dependencies to the child poms why should I add again that to the parent pom?
Anybody could you please explain this or am I doing something wrong here to fix the problem.
In the parent pom you have the option to add two tags :
<dependencies></dependencies>
and
<dependencyManagement></dependencyManagement>
In the <dependencies> tag you have to place all the dependencies that you want that all you projects includes, for example JUnit dependency or Log4j.
In the <dependencyManagement> tag you should add all the dependencies that your project needs, It does not means that they are going to be included in all your projects. It only means that your child projects can include them or not. It is going to help you to manage the versions.
Maybe you have problems in the dependency bewteen your child projects for this reason when you add all the dependencies to your parent project It works.

Resolving dependencies ad-hoc in multi-module maven project

I have an multi-module project in maven, and when I run normal goals on it, everything works fine. For testing, I have an module mytest, and this module is run fine when I run mvn package in the parent directory.
But for not compiling and running everything everytime, I would prefer to run the tests standalone, and to take the packaged files as the dependencies. But if I try to do so using mvn test -Dtest=simpleTest in the test-submodule-directory, the tests are not run, because it can't find the dependencies. After a mvn install in the parent-directory, it finds them, but uses the artifacts from the repository - a behaviour I do not want, because I have to run install everytime. And if I try to run the tests from the parent directory with mvn test -Dtest=simpleTest, it does not find the tests in the other modules, and stops, or, if I add an project-list with -pl test, it just does not find the dependencies again.
Is there any way to run a maven test in a specific submodule, that has dependencies for other submodules, without installing the submodules to the local repository? It would be possible to archieve this by building the comand-line-call for the test onself, but that seems very time-consuming.
/EDIT:
To describe the problem more exactly, I'll add source of a minimal project.
There is a parent pom:
<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>minimalproject</groupId>
<artifactId>minimalproject</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>minimalproject</name>
<modules>
<module>datamodule</module>
<module>testmodule</module>
</modules>
</project>
a Pom for the module that needs to be tested (in a real-world project, there would be more similiar modules):
<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>minimalproject</groupId>
<artifactId>datamodule</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>datamodule</name>
</project>
and a Pom for the modules that tests:
<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>minimalproject</groupId>
<artifactId>testmodule</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testmodule</name>
<dependencies>
<dependency>
<groupId>minimalproject</groupId>
<artifactId>datamodule</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
There are now two classes, one in datamodule/src/main/java:
public class Thing{
int i = 5;
}
and one in testmodule/src/test/java:
import org.junit.Test;
import junit.framework.Assert;
public class MyTest{
#Test
public void testThing(){
Thing t = new Thing();
Assert.assertEquals(5, t.i);
}
}
When I now run mvn package in the parent folder, everything works fine. But if I run mvn test in the testmodule folder, it crashes because it can't resolve the dependency to the datamodule. If I run mvn install in the parent folder before, it works - but takes the old installed version.
What I want is the ability to run the integration-test in the testmodule, without calling other tests or invoking the build-process of other parts. Is there any possibility to do so?
/EDIT2:
I found out when googling the issue, that one could have done that in maven 2 with the reactor plugin: Build single module from multimodule pom . Anyways, this plugin is not used anymore (http://maven.40175.n5.nabble.com/VOTE-Retire-Maven-Reactor-Plugin-td5789172.html). In fact, like proposed in the first answer, people say one could use profiles to do this: How do you perform a reactor build in Maven3 without a root project?
But, when adding a profile like this:
<profiles>
<profile>
<id>onlyRunTests</id>
<modules>
<module>testmodule</module>
</modules>
</profile>
</profiles>
And runnning it with mvn test -P onlyRunTests, still everything which comes before testing the testmodule (so compiling and running the datamodule) is executed. Unfortunately, this is the behaviour not wanted - I want to call integration tests, not the unit tests from the other modules. So is there any way to do so?
/EDIT 3:
There were two other promising ways to avoid this Problem: building with --pl e.g. --pl testmodule --also-make-dependencies. This does not work either, it does not build the dependencies.
The other way would be defining a path (with systemPath) of the depencies. This does not work, as it only works with absolute paths, and this would make the pom unexecutable under all other systems.
The problem in your build is (based on the example pom files) not a multi-module build cause if you have a multi-module build all your childs need to have a parent which is not the case. In your case you are having an aggregator which is not what you need.
The test module should look 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>
<parent>
<groupId>minimalproject</groupId>
<artifactId>minimalproject</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>testmodule</artifactId>
<name>testmodule</name>
<dependencies>
<dependency>
<groupId>minimalproject</groupId>
<artifactId>datamodule</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
and your datamodule should look 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>
<parent>
<groupId>minimalproject</groupId>
<artifactId>minimalproject</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>datamodule</artifactId>
<name>datamodule</name>
</project>
Now if you look at the root and try mvn clean package you should see that the testmodule is build after the datamodule. This is needed as you already determined to let Maven calculate the build order of the artifacts.

Mulit-module application maven

I've got multi module maven project, where main project depend on sub-module. Every dependency of sub-module is define by version like this: ${pom.version}. I use maven release plug-in. If I try to prepare release, I've got an error about missing version of sub-module.
Example:
main pom is on version 1.0, I try to release it. Maven build every sub-module to version 1.1, then try to build parent, and then crash. Because it can't find sub-module-1.1.
I don't know how to tell maven to build, and immediate install to local-repo every sub-module witch it build. I use maven2.
My pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.voncuver</groupId>
<artifactId>voncuver</artifactId>
<packaging>pom</packaging>
<version>1.1-SNAPSHOT</version>
<name>multimodule</name>
<modules>
<module>mod1</module>
<module>mod2</module>
</modules>
(...)
<dependencyManagement>
<dependencies>
<dependency>
<artifactId>mod1</artifactId>
<groupId>com.voncuver</groupId>
<version>${pom.version}</version>
</dependency>
<dependency>
<artifactId>mod2</artifactId>
<groupId>com.voncuver</groupId>
<version>${pom.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
(...)
You should probably post a bit more of your project structure, but typically a multimodule project looks like this:
project
mod1
mod2
mod3
pom.xml
The main pom.xml would have "pom" packaging type, and have a section in it to build everything else:
<packaging>pom</packaging>
<modules>
<module>mod1</module>
<module>mod2</module>
<module>mod3</module>
</modules>
Then, the surest way to make sure things build properly is to execute:
mvn clean install
Without the "install", it's highly possible that things might not be found in the maven reactor, especially depending on what version of maven you are using (and a few other factors).

Running maven project with eclipse dependencies outside eclipse

I'm developing a maven project with several modules in eclipse. The parent pom.xml declares all submodules, and every submodule contains a pom.xml with a reference to the parent. Some submodules are dependent on other submodules, so I have added them as a dependency (m2e finds them when searching for dependencies). However, when I try to run a submodule outside eclipse using jetty (mvn -pl submodule jetty:run), I get the error that it is missing the other submodules.
In other words, and more elaborate: there's parent, sub1 and sub2. sub2 depends on sub1. I added
<modules>
<module>sub1</module>
<module>sub2</module>
</modules>
in the parent and
<parent>
<groupId>group</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
In both sub1 and sub2, and
<dependency>
<groupId>group.parent</groupId>
<artifactId>sub1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</depdency>
in sub2.
When I run:
mvn -pl sub2 jetty:run
I get:
[INFO] Failed to resolve artifact.
Missing:
----------
1) group.parent:sub1:jar:0.0.1-SNAPSHOT
How can I get maven to find the submodule dependencies?
Have you tried running mvn install in your parent project before running Jetty in the submodule? This will install your jars in your local Maven repository, following which Maven will be able to find them.
(Or I could be grossly misreading the complexity of your question, in which case: please correct me.)

Categories

Resources