I have project with several dependencies on other project.
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>group1</groupId>
<artifactId>artifact1<artifactId>
<name>RealtyRegistry</name>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>group1</groupId>
<artifactId>artifact2</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>group1</groupId>
<artifactId>artifact3</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
All of them developed by me simultaniously. I add edition to files of all of project and i need to build main project together with dependent ones. How to do that for projects without tree structure?
There can be 2 or more covering trees for projects hierachy, for example: A depends on B,C; D depends on C,E; A and D are independent.
You can build multiple projects together using "Modules". Normally, you would do this by creating a "mother" project with <packaging>pom</packaging> and adding your real project as modules using the <modules> tag. Then, when you build the "mother" project, all modules are automatically built in the right order.
Here is an example from the Maven by Example book:
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
Note that this requires you to have your modules in subfolders that are named accordingly. For example, you would have the "mother" pom in some folder:
/.../my-project/
and the modules in:
/.../my-project/simple-weather/
/.../my-project/simple-webapp/
For more information, read Chapter 6. A Multi-module Project of the book, it's freely available on the Sonatype website.
Related
My goal is to turn an existing maven project (char-counter) into a module of my multi-module maven project. How can I do this? I use Eclipse 4.16.0.
Here is the structure of my projects.
You can include the sub-modules inside your parent maven project in following way.
Inside your multi-module-project pom.xml
<modules>
<module>char-counter</module>
</modules>
See here: Maven Multiple module
Assuming that you want to convert your char-counter project to multi-module-project.
You need to have following in your multi-module-project pom.
<groupId>com.parent</groupId>
<artifactId>multi-module-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>Anagram<module>
<module>Calculator<module>
</modules
And then in Anagram project's pom :
<parent>
<groupId>com.parent</groupId>
<artifactId>multi-module-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.child</groupId>
<artifactId>Anagram</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
And in Calculator project's pom:
<parent>
<groupId>com.parent</groupId>
<artifactId>multi-module-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.child</groupId>
<artifactId>Calcultor</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
Also you need to add dependency of one module to other,in which you want to use the code from other module and then you need to deploy the artifact(module) which has dependency included in it and has starting point to run (like main).
I have set-up a modular project in Maven with a parent project having more than one child projects.
The pom.xml of parent looks as follows -
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>parent-app</artifactId>
<version>${parent-application.version}</version>
<packaging>pom</packaging>
<dependencies>...</dependencies>
<properties>
<parent-application.version>1.0</parent-application.version>
</properties>
<modules>
<module>parent-model</module>
<module>parent-masters</module>
<module>parent-web</module>
</modules>
</project>
The pom.xml of child projects looks as follows -
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.company</groupId>
<artifactId>parent-app</artifactId>
<version>${parent-application.version}</version>
</parent>
<packaging>jar</packaging>
<artifactId>child-model</artifactId>
<dependencies>...</dependencies>
</project>
Now, I need to use one of the child projects as a lib in a separate unrelated project. The pom of the new project looks like below.
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>unrelated-app</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>child-model</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
I keep getting the below error
Illegal character in path at index 57: http://repo.maven.apache.org/maven2/com/company/parent-app/${parent-application.version}/parent-app-${parent-application.version}.pom
The reason seems that I am inheriting the version attribute in child-model from prent-app.
Is there a way to overcome this issue? OR do I need to provide the version for each child module in respective pom.xml and cannot inherit from common parent.
Thanks in advance.
There is a way to overcome this issue with relativePath but I would not recommend it (take a look at this answer)... You should always provide version for parent module. To update all versions of all modules (parent+children) you can use maven-version-plugin or maven-release-plugin.
I started to learn maven and now I'm trying to make some realworld projects with beginning simple ones. Maybe it looks simple to most of all in here but I really confused about what should I do if I want to package both parent and children projects at the same time.
(PS: I'm using intellij idea btw)
here is my configuration
Project A
assume that it depends on guava and gson
Project B
it will depend on Project A
Project C
it will depend on Project B
when I set packaging attribute of Project A to jar, it gives an error basically saying that "you must set packaging element as pom if it's in a parent pom". But I know that if I set packaging element as pom it wont create the package.
So I decided to create another project as multimodule to manage the packaging issues but dont figure it out how! Shortly how can I generate jar files for each of them at the same time?
EDIT - HERE IS WHAT I DID
MODULE POM
<?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.deneme.module</groupId>
<artifactId>ModuleGroup</artifactId>
<packaging>pom</packaging>
<version>1.0.1</version>
<modules>
<module>A</module>
<module>B</module>
</modules>
</project>
PROJECT A
<?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.mg.A</groupId>
<artifactId>A</artifactId>
<version>1.0.2</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
</project>
PROJECT B
<?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>
<packaging>jar</packaging>
<parent>
<artifactId>A</artifactId>
<groupId>com.mg.A</groupId>
<version>1.0.2</version>
<relativePath>../A/pom.xml</relativePath>
</parent>
<groupId>com.mg.B</groupId>
<artifactId>B</artifactId>
<version>1.0.1</version>
</project>
If your project A declares B and C as modules, it must have the packaging element set to pom. It also means that it shouldn't contains any code and will not generate an artifact by itself. It's merely a way of grouping projects together.
In maven, parent pom are used to define common configuration (properties, etc.) that will be inherited by all children projects.
In your case I would guess that you need a parent project (pom packaging) with 3 childrens : A, B and C :
parentProject
* module A
* module B
* module C
When running mvn install on parentProject, all sub-modules will be built and produce the corresponding jar (say A.jar, B.jar and C.jar ).
Note that you do not need to declare the parent in child pom to achieve this result, you only need to declare them as modules in the parent pom, which is called project aggregation. Declaring the parent in the child is called project inheritance. You can look at the following documentation for more details :
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance_vs_Project_Aggregation
I think you are confusing inheritance and dependency.
Inheritance: A child pom inherits all values, like properties, groupId, version, also dependencies. In general you should not use dependencies in parent poms
Dependency: A pom depends on some other jar/pom and uses it's contents and also uses/depends on transitive dependencies
As far as i understand your problem you have the following:
Project A - a library depending on external packages like gson and guava
Project B - a library depending on Project B, and transitively on gson and guava
Project C - an application depending on Project B, and transitively on Project A, gson and guava
I suggest you use this directory structure:
Workspace
|- ModuleProject
|- ProjectA
|- ProjectB
\- ProjectC
and the poms would look like this (header stripped):
Module pom:
<groupId>com.deneme.projectname</groupId>
<artifactId>ModuleGroup</artifactId>
<packaging>pom</packaging>
<version>1.0.1</version>
<modules>
<module>../ProjectA</module>
<module>../ProjectB</module>
<module>../ProjectC</module>
</modules>
Project A:
<parent>
<groupId>com.deneme.projectname</groupId>
<artifactId>ModuleGroup</artifactId>
<version>1.0.1</version>
<relativePath>../ModuleProject/</relativePath>
</parent>
<artifactId>A</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
Project B:
<parent>
<groupId>com.deneme.projectname</groupId>
<artifactId>ModuleGroup</artifactId>
<version>1.0.1</version>
<relativePath>../ModuleProject/</relativePath>
</parent>
<artifactId>B</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.deneme.projectname</groupId>
<artifactId>A</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
Project C:
<parent>
<groupId>com.deneme.projectname</groupId>
<artifactId>ModuleGroup</artifactId>
<version>1.0.1</version>
<relativePath>../ModuleProject/</relativePath>
</parent>
<artifactId>C</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.deneme.projectname</groupId>
<artifactId>B</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
I have an existing web project which currently uses Ant. I have tasks that:
create the jar based on only certain packages for other applications which use this project as a dependency
create the war file for the app
and a lot of others.
Separately I have a webservice project which uses the jar built by the previous application. This project uses Maven.
Now what I want to achieve is to move the first project to Maven, break it into core and web, and then move the webservice project into the first one.
So the result will be a multi-module project:
core
ui
webservice
Is it possible to move the sources that deal with the web delivery mechanism into the ui module (which will be a war in Maven), or I need to keep the sources in the core?
What would the best approach to do this be?
Should I look into Maven profiles?
I am a novice with Maven, so I am unsure on how to approach this.
Yes you can achieve what you want with Maven. I would break the modules like this:
Core:
<groupId>com.myapp</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
UI:
<groupId>com.myapp</groupId>
<artifactId>ui</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Dependends on core -->
<dependency>
<groupId>com.myapp</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Web service:
<groupId>com.myapp</groupId>
<artifactId>ws</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Also Dependends on core -->
<dependency>
<groupId>com.myapp</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Main Project:
<groupId>com.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>core</module>
<module>ui</module>
<module>ws</module>
</modules>
Then, in Core / UI and Web service you declare myapp as the parent:
<parent>
<groupId>com.myapp</groupId>
<artifactId>myapp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
For further info refer to:
Introduction to the POM: Particularly: Inheritance and Project Aggregation
Maven by Example: Chapter 6. A Multi-module Project: For a sample project
Multi-modules projects: For the folder structure
I've been previously managing a 3-module project as 3 seperate maven projects. As this project has been moving forward, I decided I ought to take advantage of the dependency management of maven2 to streamline integration between these 3 evolving modules.
I defined a super-project that deploys as POM. Some shared dependencies are defined here, and the modules are defined in the POM in the order of dependency from the least dependent module to the most dependent module. Each module has a POM definition back to the parent, and where it applies there are dependencies from one module to the deployed artifact of another module. I'll include possibly worthwhile pom.xml lines at the end.
On to the problem, I setup this project yesterday and was able to get each module building and working on its own. I then come back today to work on one of the modules now that some fresh requirements have come in and all of sudden everything is breaking. I'm editing the projects in Eclipse, and each time I modify a file, it no longer can resolve any of the classes defined within the same project. That is to say if I have a class foo.bar.class1 and it has an object of foo.bar.class2, Eclipse (and the compiler at large) complains that it cannot resolve class foo.bar.class2... Now this is blowing my mind because this other class is in the same project and package. Similar issues are also present for classes not in the same package.
Is there something broken in my maven setup, or does anyone have any idea why these projects can't even resolve classes in the same package??
-::POMs::-
Parent -> /path/to/project/mainApp
<modelVersion>4.0.0</modelVersion>
<groupId>com.moremagic</groupId>
<artifactId>mainApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Main App</name>
<modules>
<module>Broker</module>
<module>Soap</module>
<module>UI</module>
</modules>
Broker -> /path/to/project/mainApp/Broker
<parent>
<artifactId>mainApp</artifactId>
<groupId>com.moremagic</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.moremagic</groupId>
<artifactId>Broker</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
Soap -> /path/to/project/mainApp/Soap
<parent>
<artifactId>mainApp</artifactId>
<groupId>com.moremagic</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.moremagic</groupId>
<artifactId>SOAP</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
...
<dependency>
<groupId>com.moremagic</groupId>
<artifactId>Broker</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
...
UI -> /path/to/project/mainApp/UI
<parent>
<artifactId>mainApp</artifactId>
<groupId>com.moremagic</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.moremagic</groupId>
<artifactId>UI</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
...
<dependency>
<groupId>com.moremagic</groupId>
<artifactId>SOAP</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.moremagic</groupId>
<artifactId>Broker</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
...
It sounds like the problem is with your Eclipse setup and not Maven.
Does mvn compile work from the command-line within your projects? From both the parent project and each individual module (after doing mvn install for the dependencies)?
Are you using a Maven plugin for Eclipse, such as m2eclipse? Check that it is configured to load dependent projects from within Eclipse, rather than looking to the repository ("Enable Workspace Resolution"). What happens if you do Project > Clean to clean out all of the projects?