Unable to build JAR file for my maven PARENT prroject - java

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

Related

How to create a Multi Module Maven Project in VSCode

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

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.

How to use multiple GitHub projects in one Eclipse project?

So I have a GitHub repo and I want to supplement it with other projects in other repos. I am using Eclipse and Java as my dev tools. Is there a video I can watch or a tutorial? I've looked on YouTube and Googled the problem -- I'm probably not building a proper query so find what I need.
I don't want to merge two repos into one repo. I want to incorporate the code in another repo into an Eclipse project on my dev machine that uses one of my repos. I think.
If I'm understanding you correctly, you wish to use other projects/libraries in your project. You should look into Java build tools (ev. Maven, Gradle and others). Among other things, they let you specify a list of dependencies for your project.
Maven example
First of all, you should set up your project to use Maven.
For instance, if you wish to use joda-time (a popular date and time library for Java), you could go to http://mvnrepository.com/ and look for joda-time. From there select your desired version and copy-paste the Maven dependency to your pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<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.myproject</groupId>
<artifactId>myproject</artifactId>
<version>0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
</project>
Now when you call mvn clean install from your commandline or use the Maven Eclipse plugin, your project is built and the dependencies you specified are downloaded and added to your class path.
Keep in mind, the source code for the dependencies is never added to your project, only the jar files are added to your class path.

Maven Project Structure with Multiple Modules

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

Categories

Resources