How to create a Multi Module Maven Project in VSCode - java

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

Related

Unable to build JAR file for my maven PARENT prroject

I have created two training projects aiming to the creation of a Java Test Automation Framework with Selenium.
Both projects have been stored in my personal git: https://github.com/omarjmc
Framework: Contains main classes and all the code needed to execute successfuly the tests
Project: Demo project that will use the methods specified in the Framework project
Additionally i added the dependency to the Framework project in the demo Project's POM and I also created a release of the Framework project and attached the JAR file
enter image description here
Currently I have to clone both repositories and run mvn clean install on the framework to get the JAR file and change the packaging back to pom, but I want to just clone the demo project, run mvn clean install there and still get the Framework JAR file.
What am I missing the POM file?
Omar you have to create a maven multi module project for this requirement. Project will be the parent project and it should contain framework as child.
The basic code structure for pom.xml for Project should be 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.project</groupId>
<artifactId>multi</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>project</name>
<modules>
<module>framework</module>
</modules>
</project>
You can refer below the github project for this exact requirement.
https://github.com/jamesward/maven-multi-module-example
I created a multi-module proyect, but it only works if you download the original repository, what I want is to have them in separate repositories and just download, and use, the project child

Maven project inheritance: Cannot resolve imports from parent project

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.

How to avoid parent POM issue in Eclipse for a Maven project?

I have been working with multi-module Maven project, but in my workspace I use only child-modules, not their parent. In each pom.xml for each Maven module I have:
<parent>
<groupId>com.mycompany.contentbus.ui</groupId>
<artifactId>modules-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
It is possible to do "maven clean", "maven install" etc. in Eclipse, but Maven reports red errors in "Markers" window, and "red crosses" on the modules in Project Explorer. I successfully avoided it by following this pattern: set in \.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.m2e.core.prefs a property eclipse.m2.problem.notCoveredMojoExecution=ignore. But, I need to avoid this issue on the module level somehow, but inside the code (in project files), not in Eclipse options. Are there any possible ways to do this? Thank you in advance.

Easily add libraries to Eclipse Java Oxygen

I know there are post about JAVA libraries in Eclipse and I used a way to add some .jar files (like apache.poi and jsoup) to my project. My question is:
Where do we have to put those libraries and what settings do we have to change, so to make the libraries available for every future project we start ?
Thank you!
The most common tools for bringing dependencies into a Java project are Maven, Gradle, and Ant. I'll focus on Maven, as I estimate it to be the most popular of the three (though I have come to prefer Gradle).
I'm going to assume you have the following prerequisites installed on your machine and each of the developers on your team:
JDK 1.7 or above (in other words, Java)
A Java-focused IDE like IntelliJ or Eclipse
Here's what you need to do:
You and your team members will install Maven
You will create a pom.xml for each new project.
You will specify the dependencies for the project (like jsoup) in your pom.xml
Here's what Maven will do for you:
It will download all the dependencies you specify from an online repository like https://mvnrepository.com/
It will cache the dependencies in a directory in your user home folder called .m2 so that it doesn't need to download dependencies more than once per project
It will resolve the dependencies within each of your dependencies and avoid putting the same dependency on the classpath twice
The minimum pom.xml file you need is as follows:
<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>
<groupId>com.yourcompany</groupId>
<artifactId>your-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
</dependencies>
</project>
Copy that into a file called pom.xml in your root project directory. You can add any additional dependencies in the <dependencies> section of the XML file. By convention, all of your classes and code should go into the directory structure src/main/java/.
Any Java IDE you use will know how to open a project from a pom.xml file. In IntelliJ, you select File->New->Project from existing sources, navigate to your project's root directory, select your pom.xml, and click Open. IntelliJ will then open your project as a Maven project, read your pom.xml, and make all of your dependencies indexed and available for code completion and compilation.
Hope this helps

How Maven looks for dependencies

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.

Categories

Resources