You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by jerg <je...@protonmail.com.INVALID> on 2021/05/22 00:47:47 UTC

sharing common code in a Client Server Project

I tried to extract common Code of a Server-Client Project by turning my Project into a multi-module Project.

1. I'm unsure if this is the right approach for that Problem. I'd be happy to hear about ways to do this better.
2. I encountered a problem where i couldn't reach the common code that the client and server depend on, when creating java packages inside the client or server modules
3. If multi-module is the right approach for this Problem is there a way to solve Problem 2. or is there another way to structure modules internally?

I created a small demo Project that highlights my Problem: https://github.com/jergk/tcd-maven-package-problem/

If somebody could enlighten me or point me to some resources where I could learn how to do this properly that would make my day.

Re: sharing common code in a Client Server Project

Posted by Maarten Mulders <mt...@apache.org>.
On 22/05/2021 02:47, jerg wrote:
> I tried to extract common Code of a Server-Client Project by turning my Project into a multi-module Project.
> 
> 1. I'm unsure if this is the right approach for that Problem. I'd be happy to hear about ways to do this better.
> 2. I encountered a problem where i couldn't reach the common code that the client and server depend on, when creating java packages inside the client or server modules
> 3. If multi-module is the right approach for this Problem is there a way to solve Problem 2. or is there another way to structure modules internally?
> 
> I created a small demo Project that highlights my Problem: https://github.com/jergk/tcd-maven-package-problem/
> 
> If somebody could enlighten me or point me to some resources where I could learn how to do this properly that would make my day.
> 

I've had a quick look on your sample project. The setup seems good to 
me, except for one thing: the shared class lives in an "unnamed 
package". This is generally discouraged as it can easily lead to 
clashes. The Java Language Specification (JLS) says [1]:

 > Unnamed packages are provided by the Java SE platform principally for 
convenience when developing small or temporary applications or when just 
beginning development.

To solve your problem, the most common thing to do would be to introduce 
a package in the "Shared" module as well. I have created a pull request 
on your project [2] to illustrate the idea.

Hope this helps,


Maarten



[1] https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.4.2
[2] https://github.com/jergk/tcd-maven-package-problem/pull/1

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org