Maven Project Structure with Multiple Modules - java

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

Related

Maven package module behaviour [duplicate]

Scenario:
I have a main level project A and within A , two child projects B and C worked on by different developers , but they agree on the abstraction through common interfaces.
B depends on C (dependency).
In B's pom I added
<dependency> .. details of project C..</dependency> .
Doing this, maven inserts the dependencies fine except that project C is not recompiled.
I want project C to automatically re-compile every time I compile B.
If you want to build B and automatically build it's dependencies you can use the advanced options of the maven reactor like –-also-make-dependents .
mvn clean install –-projects B –-also-make
Or short
mvn clean install -pl B -am
That will compile all submodules of A whose B depends on .
There are a useful post on sonatype blog on the advanced options of maven reactor.
http://www.sonatype.com/people/2009/10/maven-tips-and-tricks-advanced-reactor-options/
List projects B and C as modules in the pom of the project A. Now when you build project A, it should build project B and C automatically and in the correct order.
<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>multi</groupId>
<artifactId>A</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>B</module>
<module>C</module>
</modules>
</project>
I often use Maven reactor plugin to deal with these type of problems. This plugin even covers tough requirements that a complex project with many sub modules in a complex structure may has.
See link for examples.
For above situations, using
mvn reactor:make -Dmake.folders=B
to build B and C (and all dependencies of B if any).
Hope this helpful.

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.

IntelliJ can't find package imported via New Module feature, then automatically deletes import statement?

I'm new to the IntelliJ project. Here is my IntelliJ version info:
IntelliJ IDEA 2017.2.1
Build #IU-172.3544.35, built on July 31, 2017
Subscription is active until April 3, 2018
JRE: 1.8.0_152-release-915-b6 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 3.13.0-128-generic
I am running on Linux Ubuntu 14.04.
I had two completely separate projects. I wanted to import code that resides in a JAR file from the first project into the second.
I tried using the "New Module -> Import from existing sources" option, and that did add the entire first project to my second project's source structure. But I still can't import classes found in the JAR file that are used successfully by the first project, into the second project. When I try, IntelliJ gives me the "cannot resolve symbol" error. More annoyingly, after a certain length of time or when I try to compile the project it automatically deletes the import statement(s). (Is this the "auto-remove unused imports" feature biting me here?)
For example, the following import statement can be found in the first project and is considered valid by IntelliJ (no warnings or errors):
import com.github.**messenger4j**.MessengerPlatform;
But when I try to use that very same import in a file in my second project I get the "cannot resolve symbol" error for "messenger4j and the import statement gets auto-deleted at some point. Note, the file that I'm trying use the import statement in is a groovy source file that resides in the second project, in case that matters.
Also, if I look at the repo repository that IntelliJ maintains in the "~/.m2" directory, I do see the JAR file. Here you can see that I can successfully navigate the ".m2" repository tree to the messenger4j package:
/home/user9/.m2/repository/com/github/messenger4j$
How can I properly import and use the github.com.messenger4j jar file in my second project?
UPDATE: The following appears to give me access to the desired JAR that resides in the "~/.m2" directory:
File ->
Project Structure ->
Dependencies (tab) ->
"+" sign in right hand border ->
Library
I then see the JAR file in the library list and by adding it, I no longer get the unresolved symbol error. Is this the correct way to use IntelliJ/Maven to use an external JAR file?
Is this the correct way to use IntelliJ/Maven to use an external JAR file?
No, this is not the Maven way.
If both projects use Maven they both have a POM file defining a couple of things. Let's say you have the following setup:
Project A
<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.example</groupId>
<artifactId>projectA</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Project B
<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.example</groupId>
<artifactId>projectB</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
If you want to use classes from project A in Project B, you need to add project A as a dependency of Project B. Change the POM file of project B to:
<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.example</groupId>
<artifactId>projectB</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>projectA</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
But that's not the only thing. For project B to use project A, Maven needs to know where it can find it. This is solved by installing project A in the local Maven repository by running the mvn clean install command in the root directory of project A.
After that, IntelliJ's Maven will be able to find the dependency without you manually importing project A.
File -> Project Structure -> Dependencies -> Choose your dependency and change it from "Runtime" to "Compile"

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.

Categories

Resources