How to reference a parent project from a child - maven

I have a multi module project like this:
gwt-app
model
webapp (depends on gwt-app and model)
when I try to execute any goals in webapps, for example, launch jetty, build fails because maven can't find its dependencies (I didn't install modules into a local repo). Is there a possible way to reference the parent pom so that when I run any goals in a submodule, all its dependencies will be compiled (recompiled)?

An example of your pom files would be great but in multi module projects I always declare the dependencies in the parent pom in the dependencyManagement tag:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
...
In the module pom I just delcare the dependency without the version:
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
</dependency>
...
That way were are sure each module uses the same version.
The thing to remember is that modules in maven do not inherit dependencies from the parent. You must declare the dependencies used in the module itself.
Another thing is, I believe that when you are running outside of an IDE (which searches the workspace for dependencies) you need to have each module installed in your local repo. I do not think maven will search for un-installed dependencies within a multi module project if you are not executing on the parent pom.

If you make your parent pom just have regular setup like
<plugins>
</plugins>
<dependencies>
</dependencies>
Then anything in those groups are inherited automatically into the child pom. Child pom just needs the parent section in it:
<parent>
</parent>
You don't even need to declare the same plugins or dependencies in the child pom in this manner. You only need to list the plugins or dependencies in the child pom IF you use or in the parent pom OR you want to override something in the parent pom. I actually just went through all of this week and have my builds working nicely now (small child poms with more things in the parent pom like plugin configurations).
I asked a question about this that might help you:
Maven - Parent Pom - Child Inheritance

Related

How to figure out in what pom a dependency from effective pom is defined in IntelliJ Idea?

I have a big project that has parent pom, this one has another parent; in the project's pom file another project with bom file is included as a dependency, etc.
I click on pom and generate effective pom. Inside I see a dependency, for example this one
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
Is there an easy way in IntelliJ to find the pom where this dependency is defined?
In Ultimate version it's possible to generate a diagram for all maven dependencies:
https://www.jetbrains.com/help/idea/work-with-maven-dependencies.html#maven_dependency_diagram
Or you can execute mvn dependency:tree to build full dependency tree.
https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html
https://www.mkyong.com/maven/maven-display-project-dependency/

Functionality in a Dependency of type=pom

If I have a POM that contains a dependency of type=POM, e.g.
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>mysql-starter</artifactId>
<version>1.0</version>
<type>pom</type>
</dependency>
I see that the included POM can contain other dependencies, thus providing a way to package together a set of related dependencies.
Can the reference POM also contain build steps?
For example: Use the resource plugin to copy certain files that are needed when using the specific JAR mentioned in its dependencies.
Dependencies of type pom may contain some build configuration information (such as resources, plugins or pluginManagement) but any of them will be executed for the current project but if you configure the pom of the current project to invoke them.
In fact, only the current pom and the parent pom of the current project define in the build element the build information/taks of the current project.

Maven - import / group all modules of project

I have a parent project with around 20 child modules:
<project>
<modules>
<module>module-1</module>
<module>...</module>
<module>module-20</module>
</modules>
</project>
I would like to use this project as one single entity, with all 20 modules included, in other projects. What is the convenient way to do this in Maven?
Should I make a new child module which imports the other 20 modules and refer to this project? Should this be a JAR or a POM project?
<project>
<packaging>jar</packaging>
<dependencies>
<dependency>... module-1 ...</dependency>
<dependency>...</dependency>
<dependency>... module-20 ...</dependency>
</dependencies>
</project>
I think the way you mentioned in your question is a good idea. It is actually mentioned as a best practice in the Maven book, quoting:
If you have a set of dependencies which are logically grouped together. You can create a project with pom packaging that groups dependencies together.
You can create a new module called module-all, which would be of pom packaging, that simply has a dependency on each of the modules. The packaging should be pom because the primary artifact of this module will only be the pom.xml (there will be no sources to compile, no JAR...). Then, in your external projects, you can simply add a dependency to this new module (as <type>pom</type>) and every module-i dependencies will be included transitively.
There would be a cave-at if all of your modules did not share the same version: there would need to be a reference to a specific version of a specific module and you would have to update the module-all version each time a module's version changes. However, if they all share the same version, module-all release cycle would be in line with module-i's.

Maven: Import pom properties from different pom

I would like to import properties from a project X pom file into my project Y pom such as library versions. I do not want to make the project X my project's parent.
I have tried to include project Xs pom in the dependency management section as an import.
<dependency>
<groupId>abc</groupId>
<artifactId>def</artifactId>
<version>1.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Please advise.
Thanks.
I think, today, the answer is NO. You can't read properties from external dependency without inheritance.
However a hack can be done with the codehaus Properties Maven Plugin. In fact, it can load maven properties from an external file. It can even use classpath: URLs to load files from. So you might try to load those from another dependency (which should have an appropriate scope since you probably do not want that dependency's JAR to hang around at runtime).
The usual approach to share dependency versions without using parent POMs are BOMs.
These are separate projects that only contain a pom.xml which consists of <dependencyManagement>. This can then be imported in several other projects with <scope>import</scope>.
These other projects then import the dependencyManagement inside the BOM.
Basically, you need to create a parent pom which is imported by both projects.
The parent has a <dependencyManagement> section which lists groupId, artifactId and version
The child pom's only need to list groupId and artifactId since the version is inherited from the parent's <dependencyManagement> section
eg:
root/pom.xml - Builds all modules, a simple pom with a `<modules>` section which includes parent, project1 and project2
root/parent/pom.xml - This has a `<dependencyManagement>` section
root/project1/pom.xml - parent=../parent/pom.xml
root/project2/pom.xml - parent=../parent/pom.xml
More info here

How do I default sub-module versions in a Maven multi-module project using snapshots?

I looked at previous questions on the topic and it seems people were a step ahead of where am I at the moment.
I am currently trying to use the maven release plugin to my multi module project. I start from scratch (more exactly, I retrieved some old projects to put in a new multi module project).
I have a POM parent that I defines as:
<groupId>com.somestuff</groupId>
<artifactId>stuff</artifactId>
<version>10.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name> stuff </name>
My child modules inherits the parent using:
<parent>
<groupId>com.somestuff</groupId>
<artifactId>stuff</artifactId>
<version>10.0.0-SNAPSHOT</version>
</parent>
So since it’s a new project, I specify the dependencies between modules as snapshots. For example, if module A depends on module B, I will add to A’s POM the part:
<dependencies>
<dependency>
<groupId>com.somestuff</groupId>
<artifactId>divarmiclient</artifactId>
<version>10.0.0-SNAPSHOT </version>
</dependency>
</dependencies>
The result is when I try to do a “mvn release:prepare”, Maven will yell that there is snapshot dependencies. But since it’s a whole new project, and that versions prior to 10 don’t exist, I have no idea how to default the version values of the modules.
My question is, how shall I default the module values ? How do I do in order to make my multi-module project acceptable from a snapshot perspective ?
For clarification, you're saying your structure is:
theparent
pom.xml
A/
pom.xml
B/
pom.xml
And the following assumptions all hold true:
theparent, A, and B are currently on same version
releases will always be kicked off from 'theparent' level so they remain on same version
A depends on B, always same version
Then the solution is as follows:
<dependency>
<groupId>com.somestuff</groupId>
<artifactId>B</artifactId>
<version>${project.version}</version>
</dependency>

Resources