1

Recently I started to use Maven for managing my project's structure more efficiently. However, since i'm at the same time learning JAVA, i've come to a dead-end, What is the difference between a module in MAVEN and a JAVA package ?

Since packages are used to group classes/interfaces that share common purpose: (source: docs.oracle)

Definition A package is a grouping of related types providing access protection and name space management.

And i couldn't find a clear definition of a module, better stated than the vague: (source: http://docs.jboss.org/tools/latest/en/maven_reference/html/creating_a_maven_application.html)

A Maven module is a sub-project

From what i read, we should create modules in order to separate logic in the project, business, domains, basically anything considered a s a separate entity.

Hence, why can't we do the same with usage of packages? Isn't the purpose the same?

I'm also negelecting all build-configurations here. If it's the only difference, then please provide argumentation when which one should be used.

Mr_Max
  • 47
  • 7
  • 1
    You probably don't want to mix your build of a java app and a webapp even if they are related by some intermediate API. This is best to divide the project into 2 sub projects. And maven does that nicely for you – Adonis Mar 02 '17 at 16:50
  • Then conceptually they do the same thing, only difference or rather purpose of maven lies in physical build configuration properties? – Mr_Max Mar 02 '17 at 16:53

2 Answers2

2

Maven is a bundling tool, it assembles reusable parts of software together to an application, no matter if it is Java code, image resources or HTML templates. It builds your application so it can be run or deployed in a certain environment (local, testing, production etc.) All of this has nothing to do with Java packages.

You should care about Maven Modules if you (or others) want to reuse code that you have written. For instance, you wrote a web application that converts currencies and now you'd like to use the conversion logic (but not the web frontend) in another application. In that case you'd create one module for the web frontend and one module for the business logic.

Jan B.
  • 6,030
  • 5
  • 32
  • 53
  • Module is a collection of classes. Packages are collections of classes. If i can pack all required classes for the business logic in a specific package, what is the difference if i pack them into a module? I see no difference in profits from reusing them. Could you elaborate? – Mr_Max Mar 02 '17 at 18:18
  • Take a large company for example that has an identity management server and an API to access it. Moreover there are several applications that are developed in different teams, e.g. a content management system the editors work with, a customer relation management the salesmen work with etc. All of those applications need to talk to the identity management server to authenticate the users via the API. That API would be a typical Maven Module that all of the applications could have a dependency to. – Jan B. Mar 02 '17 at 20:16
  • In the first scenario with web application and dividing front/back end modules, i suppose that eventually creating a jar with the business logic could be instantly used in any other project that could use it? Hence it wouldn't need to care about recompiling the code? Maybe my doubts come from the fact that in all simple programs i've written so far i could simply copy the code of 1/2 classes. Is my thinking correct? – Mr_Max Mar 02 '17 at 20:51
  • That pretty much hits it. It's all about re-usability which might not make too much sense in small or simple projects. The more it does, though, in framework development when you want to provide multiple artifacts of a large framework in different versions (e.g Spring, Apache Commons...) – Jan B. Mar 02 '17 at 22:43
1

A package is a collection of classes. A module is a collection of packages with build configuration.

If it was your own project you can organize them however you want with packages and/or modules. If you want to create a re-usable component (e.g. library) for multiple project you should use a module.

If you only have packages a project would have to include all of the packages (i.e. source files) directly for every library that it needed. Instead with module (remeber it also has build configuration) you can take the compiled output (e.g. .jar, .aar, etc) and include that in your project.

cyroxis
  • 3,661
  • 22
  • 37