Why importing file from another project module not working? - maven

I have maven project with 2 modules.
root-folder:
- sub-module1:
.... - src
.... - pom.xml
-sub-module2:
.... - src
.... - pom.xml
- pom.xml
For example, i have some Test1 class from sub-module1. And in sub-module2 i have another class Test2. Trying to use Test1 in Test2, but second module cannot resolve import. What is wrong here?

In the main pom.xml, you need to add modules like below
<modules>
<module>artifact-id-of-module-1</module>
<module>artifact-id-of-module-2</module>
</modules>
And in the pom.xml of the second module, you need to add dependency like this
<dependency>
<groupId>parent-group-id</groupId>
<artifactId>artifact-id-of-module-1</artifactId>
<version>${parent.version}</version>
</dependency>
This will enable you to access all the class of module1 in module 2.

Related

spring boot #Autowired a bean from another module

My question is how can i add a package to my list of component to scan #ComponentScan(basePackages = {"io.swagger", "com.company.project", like add it here }), but this package is in another module in my project,
here is the structure of my project :
springbootProject (maven project)/
module1(mavenProject, com.company.module1)
pom1.xml
module2(mavenProject, com.company.module2)
pom2.xml
pom.xml
in module 2 i have my main (#SpringbootAplication) where i want to #Autowired myRepository witch is in module 1
so how can i add the path
Import ModuleB on ModuleA, and you'll be able to use it.
Project
|__ Module A (com.test.a)
|__ Module B (com.test.b)
In pom.xml on ModuleA, add:
<dependency>
<groupId>com.test</groupId>
<artifactId>b</artifactId>
<version>1.0</version>
</dependency>
Then you should be able to add:
#ComponentScan(basePackages = {"com.test.b"})

Maven - import / group all modules of project

I have a parent project with around 20 child modules:
<project>
<modules>
<module>module-1</module>
<module>...</module>
<module>module-20</module>
</modules>
</project>
I would like to use this project as one single entity, with all 20 modules included, in other projects. What is the convenient way to do this in Maven?
Should I make a new child module which imports the other 20 modules and refer to this project? Should this be a JAR or a POM project?
<project>
<packaging>jar</packaging>
<dependencies>
<dependency>... module-1 ...</dependency>
<dependency>...</dependency>
<dependency>... module-20 ...</dependency>
</dependencies>
</project>
I think the way you mentioned in your question is a good idea. It is actually mentioned as a best practice in the Maven book, quoting:
If you have a set of dependencies which are logically grouped together. You can create a project with pom packaging that groups dependencies together.
You can create a new module called module-all, which would be of pom packaging, that simply has a dependency on each of the modules. The packaging should be pom because the primary artifact of this module will only be the pom.xml (there will be no sources to compile, no JAR...). Then, in your external projects, you can simply add a dependency to this new module (as <type>pom</type>) and every module-i dependencies will be included transitively.
There would be a cave-at if all of your modules did not share the same version: there would need to be a reference to a specific version of a specific module and you would have to update the module-all version each time a module's version changes. However, if they all share the same version, module-all release cycle would be in line with module-i's.

Maven: Load dynamically the modules (super pom)

The idea is I have a lot of Maven projects following a naming convention, i.e :
projectA
projectB
projectC
Is it possible to dynamically load all the modules in the super POM, i.e :
<modules>
<module>project*</module>
</modules>
I would like to avoid referencing by hand all the project name in the super POM.
Thank you

Large quantity of profiles in pom.xml

I have the maven project with five separate modules each of them have pom.xml with duplicated profiles. Is there any options to move all profiles from pom to separate file?
You can put the profiles into the parent pom. They get inherited during the build:
parent pom (with all common profiles)
- module1
- module2
- module3
- module4
- module5
You need to reference the modules correctly
from parent pom using the <modules> tag
from each module reference the parent using the <parent> tag

Specify modules in root pom with archetype

I'm generating a custom archetype that has 4 child modules. The problem is, when I define these 4 modules in the root pom.xml, using the following notation:
<modules>
<module>${rootArtifactId}-client</module>
<module>${rootArtifactId}-daemon</module>
<module>${rootArtifactId}-impl</module>
<module>${rootArtifactId}-war</module>
</modules>
When i run a mvn install on the archetype, I get an error saying that the project doesn't contain the modules.
Any ideas?
Thanks.
update your pom as follow:
<modules>
<module>${project.artifactId}-client</module>
<module>${project.artifactId}-daemon</module>
<module>${project.artifactId}-impl</module>
<module>${project.artifactId}-war</module>
</modules>

Resources