I set up two (maven) projects, the first contains simple Java-Classes. The first project should act as a master/parent project over (yet only one) many project.
I need that the second project, should be able to use some classes from the parent project.
What I have tried yet.
1.) I have installed the first project with mvn install
2.) Declared the first-project as parent in the pom of the second-project. <parent>...</parent>
In the second project, try to import classes from first project, failes. I'm not able to use classes from the first-project.
Let me show some snippets of both pom:
1) First pom:
<groupId>com.sample</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
2.) Second pom.xml:
<parent>
<groupId>com.sample</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
</parent>
Is it possible, to use components from parent pom/project??
Your question was already answered there : Maven include parent classes
You need to understand the concepts behind a multi modules project and how the maven dependencies management works.
A lot of documentation is available, for example : https://www.baeldung.com/maven-multi-module
Technically you can do this by changing the packaging of the parent pom from pom to jar, but there cannot be a good reason to do what you want. The parent module is not there to be a provider of classes for the children modules.
Just move your classes from the parent module into another module, and add a dependency to this module into your second pom.xml.
Related
My question is very simple:
When i'm creating a maven project if i chose to make my pom inherit from
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
</parent>
for all dependencies I include, if it is included in the parent's pom i dont need to specify which version i'm using in the inherited pom.
That is an amazing thing and avoid using multiple versions of same library.
the question is, HOW TO ACHIEVE THIS BEHAVIOR
I've a parent pom for my projects, but when I extend it, the childs pom still need to specify each dependency version evem thought it is specified in the parents pom... this causes me the need to update dependencies version multiple places everytime one dependency changed it's version
I end up by doing somthing like (child pom):
<dependency>
<groupId>br.com.fisgar</groupId>
<artifactId>fisgar-model</artifactId>
<version>${project.version}</version>
</dependency>
but i would like to make possible omit the version as a whole, same way as the spring dependency.
how can i do that
In the parent.pom add dependency with version to <dependencyManagement> section. In this case, in the child.pom you will be able to add this dependency without version.
You can get more details in the official documentation Introduction to the Dependency Mechanism
I am planning to built multi module Maven Java Project as part of creating a mono repo.
InteliJ IDE has an easy way to build Maven Modules inside an existing Maven Project.
https://www.codejava.net/ides/intellij/create-multi-module-maven-project-intellij
(e.g. Right-click on the root project, and select New > Module:)
Is there a similar feature in VSCode or a plugin to create module in an existing Maven Project?
I have already installed the "Extension Pack for Java" plugin https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack
as well as "Maven for Java" plugin https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven
Interestingly VScode recognizes modules in a multi-module maven if you open one with it. it just doesn't have a way of adding new module afaik.
After you've created a parent with <packaging>pom<packaging> tag, right-click on plus-sign on "Maven" tab to create a new module, and choose parent's working directory when prompted (In my case "demo"). (Btw, it adds some extra new lines into your parent's pom.xml, so I undo their reformatting, and add <modules><module>Your Name<module><modules> tags manually. Simply copy them before undoing something.)
Maven Tab
To see a new module under "Java Projects", and be able to run, or debug it (debugging in vscode), type "Java: Import Java Projects into Workspace" into "Command Palette" (Cntrl+Shift+P)
In parent's pom.xml should appear modules tag:
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>demo-first</module>
</modules>
In child's pom.xml should be generated these tags:
<parent>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>1.0</version>
</parent>
<artifactId>demo-first</artifactId>
<version>1.0</version>
Of course yes. You can creat the Multi-Module Maven project according to the first four steps in the article.
And right-click your root project and choose "install".
I have read many documentation and tutorial about getting started with Maven. But few things are still not clear to me :
1) When pom.xml contains <dependency>, Maven will put that artifact/JAr in Maven local Repository. Right ?
2) Suppose, I have sub modules in my MAven project. Then what is the correct way to build the project ? one should start from parent module and then go to further sub modules. right ? and that way maven will create dependency jars and put them in local Maven Repo folder ?
3) For example, if my sub module pom.xml contains following dependency. How should I provide it (vd-ps.jar) to Maven so that Maven finds it when I do Maven clean install on Submodule/pom.xml? I mean to say what does Maven require or from where does it generate such user defined vd-ps.jar ? from compiled class files ?
<dependencies>
<dependency>
<groupId>com.b.t</groupId>
<artifactId>vd-ps</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Ok, let me ask directly what I want to achieve: I have following pom.xml. and from it I would like to create a separate maven project (Say PTest) that uses dependencies (jars) of another big multi module maven project. Here :- I have a module named v-parent in my multi module project which has pom.xml in its trunk folder.
<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>com.b.t</groupId>
<artifactId>v-parent</artifactId>
<version>3.9.0</version>
</parent>
<groupId>com.b.t.test</groupId>
<artifactId>p_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ptesting</name>
<dependencies>
<dependency>
<groupId>com.b.t</groupId>
<artifactId>v-ps</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>
1) Now where should I put this sub module v-parent in my newly created Maven project (PTest) in eclipse ?
I mean how do I exactly make my PTest/pom.xml find this parent jar v-parent.jar ?
I have separate jar file named v-ps.jar. where should I put it exactly (in which structure) in Maven local repo so that it can find it ?
Each time you run
mvn clean install
Your project will be built and the resulting artifact will be 'installed' in your local repository (usually ~/.m2/repository). So if you build the project which makes the jar com.b.t:vd-ps:1.1.0 this will then be available in your local repository for other projects to use as a dependency.
If you dependency is in another module under the same parent it can in included like so :
<dependency>
<groupId>com.b.t</groupId>
<artifactId>vd-ps</artifactId>
<version>${project.version)</version>
</dependency>
If you run a
mvn clean deploy
Your artifact will be built and deployed to the remote repository (Nexus or Artifactory or similar), provided you have the correct config in your pom.xml
See https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
1) Now where should I put this sub module v-parent in my newly created Maven project (PTest) in eclipse ?
I mean how do I exactly make my PTest/pom.xml find this parent jar v-parent.jar ?
So in this case "v-parent" will be another project in Eclipse of packaging type "pom". And if you want to build the child project "p_test" and its dependency "v-ps", you could list them as modules in the "v-parent" project's pom file like below:
<project>
<groupId>com.b.t</groupId>
<artifactId>v-parent</artifactId>
<version>3.9.0</version>
<packaging>pom</packaging>
<modules>
<module>p_test</module> <!-- Make sure you provide the right path here -->
<module>v-ps</module> <!-- Make sure you provide the right path here -->
</modules>
</project>
Now when you run "mvn clean install" on "v-parent" it should build "p_test" and "v-ps" projects and places a copy of their jar files in your local .m2/repository, assuming those projects don't error our during the build. So the parent project acts like an aggregate for the sub-projects and helps building them all at one shot.
I have separate jar file named v-ps.jar. where should I put it exactly (in which structure) in Maven local repo so that it can find it ?
If you have the source for this project, then you should include it as part of the "v-parent" project's pom as a module like I mentioned above, and when you build the parent, on success, the jar gets written to your local .m2 repo. Or if you have the source for "v-ps" project, you could directly build that using "mvn clean install" and the jar will be written to the repo.
I have a Maven project with 2 modules. I want the modules to inherit the version of that project, without defining it as a parent in the POM file of any submodules (the reason behind that is that the modules already have parents). What would be the best way to achieve that?
Importing the version from a properties file doesn't work because maven expects a constant value as a project version, not an expression. Maven plugins such as the version maven plugin or the maven release plugin are not solutions to my problem because I need something that would work in an IDE (I have to use Eclipse for packaging the projects, not my call).
Edit
To clarify things (apologies if my original post was not clear enough)
Main Project POM file
...
<groupId>org.mygroup</groupId>
<artifactId>parentproject</artifactId>
<version>1.1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
...
Module POM file example
...
<groupId>org.mygroup</groupId>
<artifactId>module1</artifactId>
<version>1.1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<!-- Some parent that's NOT the main project, e.g. Spring Boot -->
</parent>
...
What I want is a solution that would allow me to set the version only ONCE (e.g. in the main project POM file) and having every module of that project to "inherit" that version.
You should probably set the parent back to your actual parent project. Version numbers between modules can be simultanuously updated using mvn versions:set. If you need the version number for cross-module dependencies, use ${project.version}. If you want to embed another Maven configuration file for its dependencies, consider using a Bill-of-material (BOM): https://stackoverflow.com/a/14876651 . Hope things brings you to your answer!
I really can't think of any Maven facility that fit 100% your necessities. Event if you could set the parentproject as an actual parent on each submodule, you'd need to specify its version in the parent declaration, so...
But I think of a trick to do the job through an authomatism, so that every time the parent version is changed, it will be automatically propagated to each submodule. It can be done like this:
Program a plugin in the parent project that writes the version id on each of its modules' pom files (for example, through an XSL transformation with the xml-maven-plugin).
Then, link this plugin to the package phase, so that every time the parent is build, the versions gets propagated to the submodules.
You only will have to refresh the submodules projects in Eclipse to make Eclipse be aware of the changes.
But if you don't want to refresh manually, there is still another alternative - fully based on Eclipse:
Make an Ant script to perform the copy-version-to-all-module-poms task. And, instead of calling it from a Maven phase, program an Eclipse builder to call it and, within this builder, program also a refresh of the specific modules. So, every time you execute a build of your project, it will copy its version to the submodules and make Eclipse be aware of this change.
I am somewhat new to the Maven project structure. I am creating a project this is going have two jars. Neither jar is dependent on each other, however, they will use some of the same libraries and there are two helper classes I created (one for logging) that would be used in both.
I was following this guide as far as project structure goes: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
My current project only has one module. I am using the IntelliJ IDE to create my build.xml (using ANT) to create my Jars. The IntelliJ build.xml, however, did not work off the bat and I had to do some manual editing to have it build both jars. I believe this would be solved if each jar was in it's own module. Also, according to the following, it seems they should be anyway http://www.jetbrains.com/idea/webhelp/module.html
This is where I am a little confused. If I do create a second module for my second jar, how do I deal with classes that are shared by both modules? Whenever I go to create a new module , IntelliJ gives the new module it's own src/ path.
As I said, I am new to the Maven project structure. I am also fairly new to ant and building jars using a build.xml. If I am completely on the wrong path please let me know so I can fix this problem early.
Thanks ahead of time.
Create a multi-module project (x) which contains a pom.xml with packaging = 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>test</groupId>
<artifactId>x</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>x1</module>
<module>x2</module>
</modules>
</project>
and 2 regular (jar) projects (x1 and x2). This is how the project structure should look like
x
x1
...
pom.xml
x2
...
pom.xml
pom.xml
The main project pom should contain dependencies common to both modules. Nested projects poms should have a reference to the parent.
<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>test</groupId>
<artifactId>x</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>x1</artifactId>
</project>
See more here http://maven.apache.org/guides/mini/guide-multiple-modules.html
If you are going to do things the maven way you should remember that each (maven-)project only builds one artifact. So if you want to build two jars you will need two maven-projects (p1,p2) each with their own pom.xml.
If you got some classes that are used by both of these projects they will have to go to their own maven-project as well to build their own module-jar (p3).
p3 will then be included in p1 and p2 as a dependency.
To build these jarrs in one go you could resort to the module-layout suggested by Evgeniy Dorofeev