Best practice for setting up multiple projects with m2eclipse

Example scenario: I have 2 projects, "common-project" and "application-project". The project application depends on the API provided by the overall project. There are also third-party banks (guava example) used by both projects.

I am trying to convert to using maven and m2eclipse, but I am unclear about the best approach. Currently, my setup without maven has third-party banks added as libraries in the common-project, and marked as "exported". Thus, they are inherited by the project application, and I do not need to explicitly add them as libraries in the application project. Both projects are under active development, so I would prefer not to create a jar with a common project first and then β€œinstall” it into my local repository before I can use the new features in the project application.

What is the recommended approach for this type of project layout? I see that the following topic covers roughly the topic: A project in Eclipse that creates a jar used by another project in Eclipse

thanks

+6
java eclipse maven-2 m2eclipse
source share
3 answers

Example scenario: I have 2 projects, "common-project" and "application-project". The project application depends on the API provided by the overall project. There are also third-party banks (guava example) used by both projects.

I would create 3 maven projects: the parent aggregate module, the common-project module and the application-project module depending on the common-project and declare guava as a dependency in the parent module (so that the child project will inherit it). Something like that:

  $ tree Q3337426
 Q3337426
 β”œβ”€β”€ application-project
 β”‚ β”œβ”€β”€ pom.xml
 β”‚ └── src
 β”‚ β”œβ”€β”€ main
 β”‚ └── ...
 β”‚ └── test
 β”‚ └── ...
 β”œβ”€β”€ common-project
 β”‚ β”œβ”€β”€ pom.xml
 β”‚ └── src
 β”‚ β”œβ”€β”€ main
 β”‚ └── ...
 β”‚ └── test
 β”‚ └── ...
 └── pom.xml

Where the parent pom.xml is as follows:

 <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>com.stackoverflow.Q3337426</groupId> <artifactId>Q3337426</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>Q3337426 - Root</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>r05</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <modules> <module>common-project</module> <module>application-project</module> </modules> </project> 

pom.xml for a common project:

 <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> <artifactId>Q3337426</artifactId> <groupId>com.stackoverflow.Q3337426</groupId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>common-project</artifactId> <name>Q3337426 - Common Project</name> <dependencies/> </project> 

pom.xml for the project application:

 <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> <artifactId>Q3337426</artifactId> <groupId>com.stackoverflow.Q3337426</groupId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>application-project</artifactId> <name>Q3337426 - Application Project</name> <dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>common-project</artifactId> <version>${project.version}</version> </dependency> </dependencies> </project> 

This is Maven’s way of organizing such a project and will allow you to start the reactor assembly from the root project to create everything.

(...) Both projects are under active development, so I would prefer not to first create a jar of a common project, and then β€œinstall” it into my local repository before I can use the new features in the -project application.

The m2eclipse plugin can resolve dependencies on Workspace projects (this is actually the default behavior). Therefore, if you import both application-project and common-project , the first one will be configured to depend on the sources of common-project (instead of hanging with a jar). Changes made to the common-project will be immediately visible when using this setting.

This should solve your problem inside the IDE. Outside the IDE, run the reactor on the top project.

+4
source share

Express the dependencies that each project has in the POM <dependencies> element.

The m2eclipse plugin will automatically select them from pom.xml and add them as library links to the build path in Eclipse.

If application-project depends on common-project , then it also inherits its dependencies - there is no need to list common dependencies between them a second time in pom.xml application-project .

0
source share

Suppose you already have maven installed on your computer, then download the m2eclipse plugin, install it and restart eclipse.

Remember that the purpose of the maven assembly is to create and artifact that can be distributed and reused, it is normal if you do not want to create mvn install before you have a somewhat stable version of your API with a common project, although there is nothing wrong with that during development.

In your case, you can do one of the following:

  • If the common-project API is absolutely necessary and will more often be used not only within the framework of the application-project , then create the project as a multi-module project and declare the common-project module of your application project . Here is an example of how to do this.
  • If otherwise the common-project will be more often than not a common artifact, then each project that independently declares the dependencies of both in its pom.xml files should think about the relationship between the two.
  • First you can create a partial implementation of common-project , package it and then declare it as a dependency in the application-project with <scope>system<scope> , indicating where it is located in the file system, this will show maven that the dependency is always present, and it will not search for it in any repository; although mvn install will be more elegant than this, you are trying to avoid this.

Sincerely.

0
source share

All Articles