Running maven project with eclipse dependencies outside eclipse - java

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.)

Related

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

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.

Maven build - dependancy on core classes

I have two projects I am working on which share some common code - I am putting this common code in to a new project called Core.
Both my projects use maven to build, and my core classes will also use maven. In Eclipse how do I configure maven to do a maven build of the core classes and then use these in the build for my two other applications?
Is there some prebuilt rule I need to specify - for example build this project, however, go build core first and use the output of that for this.
Hope that makes some sort of sense.
You can add a dependency to Core artifact in your project.
If you use M2E Eclipse plug-in, workspace artifacts are quite easy to reference in the Maven editor.
The only condition for this kind of dependency to work is that dependent artifact must be retrievable from a maven repository (eventually putting it to the local maven repository through install goal).
You could add a parent pom, with modules
<project>
<groupId>com.mypackage</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent</name>
<packaging>pom</packaging>
...
<modules>
<module>Core</module>
<module>MyModuleA</module>
<module>MyModuleB</module>
</modules>
...
</project>
And then just add your dependency in MyModuleA and MyModuleB like a normal dependency
<dependency>
<groupId>com.mypackage</groupId>
<artifactId>Core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

How to compile a maven project which reference to another local changed maven project?

I have 2 projects, A and B. B is a lib project and A reference to B.
When I add new function to B, it's ok to run mvn install on B, but it failed on mvn install on A due to can not find the symbol from new B.
I'm sure I did install correctly on project B, but why A still failed to compile and install?
This is A's pom.xml:
<dependency>
<groupId>A and B's group</groupId>
<artifactId>B</artifactId>
<scope>provided</scope>
</dependency>
any clue? Thanks
I would suggest to create a multi-module build like the following:
+-- root
+-- pom.xml
+-- module-A
+-- module-B
In the root pom you need to define the modules like this and define the packaging to pom.
<modules>
<module>module-A</module>
<module>module-B</module>
</modules>
Furthermore you can define a dependency of module-A to module -B simply by:
<project ..
<parent>
<groupId>project.parent</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>module-A</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-B</artifactId>
<version>${project.version}</version>
</dependency>
..
</dependencies>
..
</project>
With this setup you can simply build all modules from the root folder just by:
mvn clean package
or you can import that structure into Eclipse (m2e installed?) or any other IDE like IntelliJ or Netbeans.
Try mvn clean install to do a totally fresh build of A, or mvn -U install to force Maven to look for updated snapshots. It sounds like your environment is still using the older JAR. It's hard to tell what your setup is from this description -- sounds like you're correctly installing B to your local repository, but I'm not sure if your IDE might be trying to be 'helpful' as well.
You can include the <version> element in the project's as well as dependency's declaration.
pass a version variable from Maven command line. Eg: ${build.version}
Module A's pom.xml:
<groupId>A & B's Group ID</groupId>
<artifactId>A</artifactId>
<version>${build.version}</version>
<dependencies>
<dependency>
<groupId>A & B's Group ID</groupId>
<artifactId>B</artifactId>
<version>${build.version}</version>
<type>war</type>
<scope>provided</scope>
</dependency>
...
So, whenever Project B is built and installed, the dependency will be available in the local maven repository for that particular version and will be picked by Project A

How to reference one maven project from another maven project?

I have an in-development library project (module by IntelliJ definition) that i want to reference from other projects. How would i go about to reference that project in other projects ?
you can use whether Dependency or module tags in pom.xml of your project.
Depends on what you trying to do.
<dependency>
<groupId>com.mysubpro</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Or
<modules>
<module>myproject</module>
</modules>
You have to use mvn install goal. This will install your library to your local repository. Link to Maven Install Plugin. Once it is done you can have a dependency to it in your other projects.

Categories

Resources