Taking out a part of a maven project as a module - java

I have a maven module A that is dependent on the classes in the module B (both are child modules in a project).
In the A's pom.xml I have the following:
<dependencies>
<dependency>
<groupId>test.pack</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
But when I try to build the A, the B does not appear in the dependencies correctly, I get compilation errors in the class that is in the A's test.pack.packFromA package in the import statement, which looks like import test.pack.packFromB.*.
So, my B dependency doesn't work correctly. But I thought classes from the B would be packed and added in the classpath, so I could use them. I tried to add <type> in that dependency, but that didn't help. What am I doing wrong here? Thanks in advance.
EDIT:
The <modules> part of parent's pom.xml looks like that:
<modules>
<module>B</module>
<module>A</module>
</modules>
Here is the error I get: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project A: Compilation failure: Compilation failure:

Have you done mvn install in module B, before you tried to build module A?
(If you do "mvn package" instead of mvn install, module B won't be placed in the repo, so Maven won't find it when building module A).
Also, have you tried building the multi-module project from the parent module? (When you do this, Maven will build the modules in the correct order)
If that's not it, verify that the classes you are referencing are located under src/main/java/test/pack/packFromB in module B. If they are under src/test/java.. you need a specific type of dependency for that.
Hope that helps.

Related

Maven Reference Pom in Child Module By Name

We are using a parent pom that has a child module where we have 2 pom files - the one named pom.xml and other being images-pom.xml.
This is the situation because we are doing some naming changes and for the time being we want to have them both.
In our parent pom we have the following code
<modules>
<module>child</module>
</modules>
<packaging>pom</packaging>
By default it seems that this is looking and trying to build the pom.xml - but in reality we want to use the images-pom.xml Is there any way to achieve this without creating a new module and using profiles?

Maven compile error - unable to resolve test dependency from another local project

I have two Maven projects A and B, where project B is nested in project A. The structure looks like the following:
Project A:
src/test/java:
DependencyClass.java
Project B:
src/test/java:
MyTest.java
pom.xml
pom.xml
I'm trying to import DependencyClass.java (project A) into MyTest.java (project B), and be able to invoke the methods in DependencyClass.java from MyTest.java. But when I tried to package project B, it failed at the compile phase:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project : Compilation failure:
[ERROR] MyTest.java:[7,41] package XXX does not exist
[ERROR] MyTest.java:[17,9] cannot find symbol
[ERROR] symbol: class DependencyClass.java
I have found some answers such as this and this, and I have done the following:
added the maven-jar-plugin with a goal of "test-jar" in the pom.xml of project A
added the dependency to project A in project B's pom.xml, and declared type as "test-jar" and scope as "test".
Eclipse is able to resolve DependencyClass.java without giving errors, but it just failed when I tried to build with Maven. Any suggestion or idea on what I might be missing? Thanks in advance
Edit: I have also tried adding another dependency in project B's pom.xml, with the following scope and type:
<scope>compile</scope>
<type>jar</type>
But I got more errors when building project B - now it even starts to complain about jUnit and other built-in classes.
I have resolved my error. The fix was to remove the <scope> tag. So now in project B's pom.xml, I declared two dependencies of project A, one with <type>jar</type> and another with <type>test-jar</type>, and both don't have <scope> declared.
In project A's pom.xml, the execution goal of the "maven-jar-plugin" should specify both "jar" and "test-jar".

Depends on other local module

I have two modules and one links the other one. When I install the second one the build failed saying me 'Could not find artifact'. Here's poms:
<groupId>xyz.upperlevel.graphicengine</groupId>
<artifactId>graphicengine</artifactId>
<packaging>pom</packaging>
<version>FINAL</version>
<modules>
<module>graphicengine-api</module>
<module>gamelauncher</module>
<module>testgame</module>
</modules>
This is the pom of the main project.
Then here's the pom of one of its modules (are all the equal):
<parent>
<groupId>xyz.upperlevel.graphicengine</groupId>
<artifactId>graphicengine</artifactId>
<version>FINAL</version>
</parent>
<groupId>xyz.upperlevel.graphicengine.testgame</groupId>
<artifactId>testgame</artifactId>
<version>1.0-SNAPSHOT</version>
I have the module A that links to the module B all inside the MAIN project. When I build the module A all goes fine (since it has no dependency from local modules) but when I build the module B it failed. This is the error:
Failed to execute goal on project gamelauncher: Could not resolve dependencies for project xyz.upperlevel.graphicengine.gamelauncher:gamelauncher:jar:1.0-SNAPSHOT: Failed to collect dependencies at xyz.upperlevel.graphicengine.api:graphicengine-api:jar:1.0-SNAPSHOT: Failed to read artifact descriptor for xyz.upperlevel.graphicengine.api:graphicengine-api:jar:1.0-SNAPSHOT: Failure to find xyz.upperlevel.graphicengine:graphicengine:pom:FINAL in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
How to solve?
Do you use the right coordinates of your artifacts? Maven should have created the repository automatically. See https://maven.apache.org/guides/introduction/introduction-to-repositories.html for more informations on repositories. For a detailed spec for artifacts and dependencies see https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html.
To be able to help you in a better way, please provide some code.
I HAVE SOLVED: I WAS BUILDING FROM MODULES PATH AND INSTEAD OF BUILDING FROM ROOT PROJECT PATH! DON'T FALL IN MY SAME ERROR!

Maven multi module jenkins project

I've a multi-module maven application that we need build through Jenkins.
Project structure is like:
a.xml
<artifactId>parent-1</artifactId>
<modules>
<module>lookup-1</module>
<module>lookup-2</module>
</modules>
lookup-1.xml
<artifactId>lookup-1</artifactId>
<parent>
<groupId>com.lookup</groupId>
<artifactId>parent-1</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<name>lookup-1</name>
lookup-2.xml
<artifactId>lookup-2</artifactId>
<parent>
<groupId>com.lookup</groupId>
<artifactId>parent-1</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<name>lookup-2</name>
now,
mvn clean install -f a.xml works fine. For this I created one Jenkins job.
Now, same as a.xml, I've another project with b.xml. b.xml has the same code as of a.xml except different Ids. So, I've created another jenkins job for b.xml.
Both jobs work fine. But, now I want to build both these project from single Jenkins job based on which project we commit in Git. For, this I want to have a new project(pom.xml) and where I want to put both a and b under modules tag. Like this:
pom.xml
<name>combined_project</name>
<artifactId>combined_project</artifactId>
<modules>
<module>a</module>
<module>b</module>
</modules>
a.xml
<artifactId>parent-1</artifactId>
<name>a</name>
<parent>
<artifactId>combined_project</artifactId>
</parent>
<modules>
<module>lookup-1</module>
<module>lookup-2</module>
</modules>
But, its not working for me. I'm getting following exception in maven:
Child module D:\....\a of D:\....\pom.xml does not exist
Maven not able to find child module.
I've following project structure:
project
|_ lookup-1
|_ lookup-1.xml
|_ lookup-2
|_ lookup-2.xml
|_ a.xml
|_ b.xml
|_ pom.xml
Any hint?
a.xml has 2 sub modules lookup-1 and lookup-2. By default maven is looking for the sub modules in the nested maps. So you need to have the following directory structure:
<parent>
|
+- a/pom.xml
| |
| +- lookup-1/pom.xml
| |
| +- lookup-2/pom.xml
|
+- b/pom.xml
|
+- pom.xml
Alternative is to change the parent into
<name>combined_project</name>
<artifactId>combined_project</artifactId>
<modules>
<module>a</module>
<module>b</module>
<module>lookup-1</module>
<module>lookup-2</module>
</modules>
Maven follows a project structure and each <module> will contain a pom.xml file. As you have explicitly stated the pom file in command line that file will be selected as the pom.xml for that project. However, for every submodule that is listed maven will search for the exact file named "pom.xml" for each submodule.
From the folder structure provided, it can be inferred that a.xml can act as a pom file when explicitly specified in command-line but it cannot act as a module as it does not have a dedicated folder with pom.xml.
A maven project in general has the following structure:
archetype-id(folder)
pom.xml(packaging: pom)(with one module module-artifact-id)
module-artifact-id(folder)
pom.xml
If you are attempting to build only the module that has been updated then the module that is not checked in recently will not be part of the newly created archive. But if you still would want to do it for some "special" reason. The following method might be the one you are looking for:
(I wouldn't recommend it -- as it is messy and goes against conventions)
Have two sub-modules in a single project and also Have two .xml files (pom files to be specified in commandline). In one xml file lets say "a.xml" "module-1" is to be specified as a module and in "b.xml" "module-2" is to be specified as a module.
Using the currently modified module invoke the build on that pom file. Example, if module-1 is modified specify a.xml in the command line as the pom.xml.
Please note that I have not attempted it but if it is possible this is one of the ways I can think of by which we can accomplish what you are looking for.
Reference for passing parameters : Pass a dynamic parameter in Jenkins build
Two possibilities :
Either you need to fix the path of your submodules in the modules section of the parent pom.
While creating a jenkins job you will have to tell jenkins to clone submodules as well. when I started with jenkins, I faced similar problem. The solution was to enable "Recursively update submodules" checkbox under Advanced scm behaviors in Jenkins UI.

Automatically including sub-modules of another maven project

I have my project structure like this.
pro
pro-common
pom.xml
pro-list-history ==> [1] Packaging type pom
pro-list-main
pro-list-entities
pom.xml
pro-list-daos
pom.xml
pro-list-services
pom.xml
pom.xml
pro-search
pom.xml
pro-customers
pom.xml
pom.xml
pro2
pro-list-history
pro-list-main
pro-list-entities
pom.xml
pro-list-daos
pom.xml
pro-list-services
pom.xml ==> Want to use [1]
pom.xml
pom.xml
My question is is okay to use groupId and artifactID from [1] in the second project as shown above?
The referred module packaging type is pom.
Put a dependency section in the project two at the specified module as shown above, and specified as pom. Build is fine, but its not importing the dependencies from that project.
Can anyone help?
First, you can depend on a POM dependency. See: Netbeans: maven dependencies of type pom for an example and discussion.
However, I think you are asking if there is a short-hand way to import all the sub-modules of a "parent" module by specifying its pom.If so, see this question:
Maven - include all submodules of a pom as dependencies in another module
You can just reference the sub-module in another module like you would a file:
<modules>
<module>inside-project-module</module>
<module>inside-project-module</module>
<module>inside-project-module</module>
<module>../OtherProject/outside-project-module</module>
<modules>

Categories

Resources