I ve got my maven web project and i want to add my ejb project (the same worksapce) as a liblary. How to make it in the pom.xml?
Do I think in a right way?
Assuming the MyEjbProject is not another Maven Project you own or want to build with maven, you could use system dependencies to link to the existing jar file of the project like so
<project>
...
<dependencies>
<dependency>
<groupId>yourgroup</groupId>
<artifactId>myejbproject</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>path/to/myejbproject.jar</systemPath>
</dependency>
</dependencies>
...
</project>
That said it is usually the better (and preferred way) to install the package to the repository either by making it a maven project and building it or installing it the way you already seem to do.
EDIT: If they are, however, dependent on each other, you can always create a separate parent project (has to be a "pom" project) declaring the two other projects as its "modules". (The child projects would not have to declare the third project as their parent). As a consequence you'd get a new directory for the new parent project, where you'd also quite probably put the two independent projects like this:
parent
|- pom.xml
|- MyEJBProject
| `- pom.xml
`- MyWarProject
`- pom.xml
The parent project would get a "modules" section to name all the child modules. The aggregator would then use the dependencies in the child modules to actually find out the order in which the projects are to be built)
<project>
...
<artifactId>myparentproject</artifactId>
<groupId>...</groupId>
<version>...</version>
<packaging>pom</packaging>
...
<modules>
<module>MyEJBModule</module>
<module>MyWarModule</module>
</modules>
...
</project>
That way the projects can relate to each other but (once they are installed in the local repository) still be used independently as artifacts in other projects
Finally, if your projects are not in related directories, you might try to give them as relative modules:
filesystem
|- mywarproject
| `pom.xml
|- myejbproject
| `pom.xml
`- parent
`pom.xml
now you could just do this (worked in maven 2, just tried it):
<!--parent-->
<project>
<modules>
<module>../mywarproject</module>
<module>../myejbproject</module>
</modules>
</project>
Related
I have the following project package structure.
parent
|- module1
|- module2
|- module3
I am able to include module3 as a dependency in both pom.xml files of module1 & module2. This gives me access to all of the dependencies & code from module3.
<dependency>
<groupId>my.group</groupId>
<artifactId>module3</artifactId>
<version>1.0.0</version>
</dependency>
However, when I run maven commands such as dependency:tree on the pom file of module1 or module2, maven will try to search on certain <servers> that are listed in my settings.xml file in order to try & download module3 as an artifact. module3 is not deployed to any <server> at the moment so, the maven command fails with following error
[ERROR] Failed to execute goal on project module1: Could not resolve dependencies for project my.group:module1:jar:1.0.0-SNAPSHOT: Failed to collect dependencies at my.group:module3:jar:1.0.0-SNAPSHOT: Failed to read artifact descriptor for my.group:module3:jar:1.0.0-SNAPSHOT: Could not transfer artifact my.group:module3:jar:1.0.0-SNAPSHOT from/to MyServer (https://example): Access Denied to: .......etc.
Ideally, module3 would not be deployed at all as an artifact but, instead it might simply have <packaging>pom</packaging>. module3 is just some module that should only be used by module1 & module2. Also, FYI the dependency tree of module3 builds perfectly fine.
Dependencies should never be of packaging pom. The need to be proper JARs and also need to be deployed to the repository.
Usually, you run build commands on the parent of the multi module project. If you want to restrict the build to one module, use -pl.
As far as I understand the question, the following maven module are part of the same reactor:
parent
|- module1
|- module2
|- module3
parent should have:
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
Otherwise, Maven will not look in the file system but in the local repository.
Also, to my understanding, in your example module1 and module2 are two jar packaged module (eg: <packaging>jar</packaging> or omitted) but parent and module3 are two pom packaged module (eg: <packaging>pom</packaging>).
I suppose you want to create some kind of module importing only dependencies without code, in which case you should import it using:
<dependency>
<groupId>my.group</groupId>
<artifactId>module3</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
Strange as it may be, this should work.
You can also use --offline to force maven to not go online.
Make sure the version of the module is correct.
Normally, when you run "mvn clean install" in parent project, all modules will be built, Order builds module3 -> module1 and module2. module3 is always built first.
Thanks
I'm trying to create a maven spring-boot project with multiple modules. I have created a parent module with packaging type pom and many children submodules with packaging type jar.
So my parent's pom.xml looks like:
<groupId>Creator</groupId>
<artifactId>DPAI</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>starter</module>
<module>DatabaseApi</module>
...
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
One of submodules: starter contains only starting class annotated with #SpringBootApplicatoion and in its pom.xml there is a section with other child artifacts like:
<parent>
<artifactId>DPAI</artifactId>
<groupId>Creator</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>starter</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>Creator</groupId>
<artifactId>DatabaseApi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
...
</dependencies>
So I'm trying to do some refactoring and move Main.class and all dependencies to my parent's pom, but it doesn't compile with error with message that my dependencies referencing itself.
In my opinion, the problem is that my parent pom contains section with it's own submodules. Parent of that submoduls is the same pom, where I try to add described dependencies
The parent.pom can't contain any java code, only Maven specifics e.g. See: https://howtodoinjava.com/maven/maven-parent-child-pom-example/#parent-content
Maybe tell us, what you want to achieve.
In a Maven multi module project you usually have a parent Pom (with packaging Pom) and several modules at the same level as you already set your project up.
Build the modules without dependecies on your code first, the the dependent modules: In your parent Pom change the order of the modules to
<modules>
<module>DatabaseApi</module>
<module>starter</module>
...
</modules>
So I'm trying to do some refactoring and move Main.class and all
dependencies to my parent's pom
I dont think this is possible. Your parent pom is actually of type pom, meaning you're not actually supposed to have any java code in it. Its meant to hold the versions of jars used in your child modules. You can relate this to the spring-boot-parent module. When we declare the spring-boot-parent module in a spring boot project, your adding your project as a child of the spring-boot-parent. And the parent will manage the versions of all of your dependencies.
I think the best way forward would be to maintain all your service related code in your spring-boot module. Filters, controllers,etc. The other stuff like your jdbc, integration layers can be maintained in other child modules and then referred to the spring module as jar references similar to your example.
So I'm trying to do some refactoring and move Main.class and all
dependencies to my parent's pom,
I'm not 100% sure if Maven would support something like the following in the parent POM itself:
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>DatabaseApi</artifactId>
<version>${project.version}</version>
</dependency>
...
</dependencies>
But for sure it won't support Java classes in a Module with pom-packaging (such as parent modules or multi-module modules). The compiler:compile goal etc. are not bound to any phase for pom-packaging by default. In other words: Maven does not compile Java classes for pom-modules by default.
My recommendation:
Keep the SpringBootApplication in a Java-based module. For Spring MVC/ WebFlux application I usually create a "-web" module with:
SpringBootApplication
web service controllers
http/ web filters
global configs such as: security, swagger, async
application.yml
...
It's also the module where I configure the Spring Boot Plugin to create an executable JAR.
I have a ModuleA in ProjectA and ModuleB in ProjectB. There is a profile in ProjectB which needs both the modules to be built. How do we achieve this ?
Basically, I want something like this,
<profile>
<id>test</id>
<modules>
<module>ModuleA</module> <!-- this throws error -->
<module>ModuleB</module>
</modules>
</profile>
I have gone through this question(How to include a maven module outside of the project context?), but the requirement here is to have a profile with both the modules and not add ModuleA as dependency in ProjectB
If you need a profile in both, you need a common parent pom. The parent pom is usually the pom where you declare the modules, so define the profile there.
I have created 5 or 6 Java projects in Eclipse by now and will be creating some 20 more projects. I have to add the TestNG library and another library (including some specific jar files) in the project.
Is there any way such that the Eclipse will automatically include both of these libraries at the time of creating every new project?
I don't want to add these libraries on my own by navigating to ADD BUILD PATH -> ADD LIBRARIES.
First, you shouldn't use Eclipse dependency management (add libraries manually to build path using Eclipse) if you're already using Maven.
IMO, you should only rely on Maven dependency mangement.
I suggest you to create a Maven parent project that will handle your dependecies used across multiple projects as follow.
Project structure
- maven-parent-project
|-- project-a
|-- project-b
|-- ...
Define Maven parent project
I suggest you used <dependencyManagement> (documentation) to manage the versions of your dependencies.
pom.xml
<project>
<artifactId>maven-parent-project</artifactId>
<modules>
<module>project-a</module>
<module>project-b</module>
</modules>
<dependencyManagement>
<dependencies>
<!-- Here you can define the versions of your dependencies used accross multiple project -->
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Here you can define the dependencies used accross multiple project -->
</dependencies>
</project>
Define Maven sub projects
Then, you can define in all your project the maven-parent-project
pom.xml
<project>
<parent>
<artifactId>maven-parent-project</artifactId>
<version>0-SNAPSHOT</version>
<groupId>group</groupId>
</parent>
<artifactId>project-a</artifactId>
<dependencies>
<!-- Here you can define the dependencies specific to the project a -->
</dependencies>
</project>
I hope this will help you.
Study about dependency management in Maven. That will solve your requirements. Read this thread to get the jist.
I have a project in Intellij 13 organized with the following structure:
main project
- pom.xml
module 1
- pom.xml
module 2
- pom.xml
I added "module 1" and "module 2" to the "main project" by using "Add maven projects"
In the "main project" pom.xml I added the dependencies to the module 1 and module 2 like:
<dependency>
<groupId>module1</groupId>
<artifactId>module1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>module2</groupId>
<artifactId>module2</artifactId>
<version>1.0</version>
</dependency>
However, "main project" cannot see the beans of module1 and module2. Any idea what could be missing ?
main project must be packaging pom and have modules element with the others.
Or add a main pom
sample here http://svn.apache.org/repos/asf/maven/indexer/trunk/
In your parent project POM...
<modules>
<module>child1</module>
<module>child2</module>
</modules>
And your folder structure should look something like this...
parent/proj/folder
- pom.xml
parent/proj/folder/child1
- pom.xml
parent/proj/folder/child2
- pom.xml
EDIT:
Also, in case you didn't know, in your parent pom you declare all the versions of your dependencies, but when you include them in you child poms you shouldn't include the versions