Best way to share a single class dependency in Java - java

I often make many Java projects to deal with parsing and string manipulation. Since the native String class from Java is a bit limited for these cases, I always end up creating an utility class to manipulate Strings.
However, copying and pasting the same (or almost the same) class in every project is a hassle. So I'm planning to make a single version of this and share as a dependency between projects. The only way to do this that I know of is versioning a maven project in git and importing it as a jar in other maven projects. But is there a simpler solution?
Creating a separate repository and packaging a single class into a jar seems a bit of an overhead. Plus, some simple non-maven projects would have to create a pom.xml just to download this class. So I wonder, is there any other tool to share simple dependencies for small projects? What is the best way to handle this situation?

Related

Use File Containing Variables across different projects

I am trying to make a new file var.java in project A such that it includes some variables . Now I need to use the same variable while passing to an intent from project B .
Till now what I am doing is that we are defining the variables once in project A and then in B and I need to reduce the code redundancy.
I had an idea to configure the whole project B in A settings.gradle file but since I am just needing the particular file var.java in A there is no point of doing that.
Can anyone please suggest some way to fix this.
Basically, what you're trying to achieve is reusable code to be used among several projects. So the logical approach would be to create a shared library to use in all projects that need it. Extract all general purpose code needed for multiple projects into its own project and add it as a dependency with maven or Gradle or add the library in the old fashioned way manually to your projects.
Of course I am assuming you are talking about information that is already existent at compile time, constant values that do not change during runtime. But as you've written it, it doesn't sound as if you were sharing information during runtime, so a small, simple library would do the trick.

Best way to generate maven modules

I have a complex structure of maven module with involves some like 20 modules for a standard application. The standard application is also supposed to contain spring xml files and packages folder setup from the start. Right now we are copying a example application and change the name all over it. But this seems like a lot of unnecessary work. I'm wondering what software would be best to use generate this.
I saw that maven had something for this called archetype but i'm not find much documentation and example on how to use it. Maybe there is better tool for this task.

Maven modular project share classes across modules

I am working on a Java maven built project, which consists of several modules. I (as many before me) face this issue that I have classes that are used in multiple of modules. I wish to find an elegant solution to the issue of sharing classes across all modules.
I am aware that this is possible to be accomplished by making another module called for example common where I would put all shared classes. After this module can be compiled into separate jar and can be used as dependency in other modules.
However I do not find this solution elegant enough and am looking for a more direct sharing. This essentially means that I would like to have those classes as separate module common, but this module would not be compiled as separate jar, instead those classes would be directly included into compilation/packaging of all depending modules.
Is this possible to achieve using maven?
UPD: To add an example why I do not find mentioned above way as acceptable - when writing code and mid way realize that some changes should be done to the common classes, all IDEs would require after those changes to run install goal on common module in order to have it as compiled jar in classpath (so that those changes will be visible in other modules). This is just one of the examples why I find this way inconvenient and am looking for more elegant solution.

Packaging common code for better code reuse in java

I have two java projects that are fairly independent beside the fact that they share a common mysql database.
I wanted to refactor these project and extract everything regarding the common data layer. I am using jOOQ, so most of this layer gets autogenerated in my build. Beside that i then have a few common entity classes that are used in both projects.
what would be the best practice to separate this, so that any change can be done one place and still propagate to both projects? create a third java simple project with the common code? what would you do
I work on a distributed system, and multiple daemons need access to the same Postgres database via jOOQ. Since each daemon is its own Java project, I am in the same boat as you basically.
The solution I've been using is to create a third Java project as a Java Library. If you're using Netbeans you can just include it as a subproject dependency and any changes to the library project can be recompiled into the individual application projects.
One thing of note, you'll need to specify the jOOQ library jars in all 3 projects. In Netbeans its easy to specify a project's library directory, and have multiple projects share these dependencies. Netbeans will copy the dependencies at deployment time.
Edit:
The steps are basically:
create a master layout for system, IE:
/master-project/
/master-project/library
/master-project/software
/master-project/software/daemon1
/master-project/software/daemon2
/master-project/common
/master-project/common/utility1
/master-project/common/utility2
create third-party "library" bundles of {jar,src,docs} under /master-project/library.
create "application" projects under /master-project/software, making sure to tell Netbeans to only use third-party libraries under /master-project/library.
create "library" projects under /master-project/common, making sure to tell NB only to use third-party libraries under /master-project/library.
create a "library" for jOOQ code to be shared, as in step 4.
Each project is responsible for its own compile script (including generating jOOQ code, if desirable), and correctly specifying its dependencies out of /master-project/library, and /master-project/common.

How to move hibernate related code to its own 'project' so I can share it?

If I want to be able to re-use my hibernate related code with multiple IntelliJ solutions, what should I do?
Should I move my models (with annotations) and Dao's and service classes to their own module?
How would I then be able to re-use this module/project with other intellij solutions?
I guess they would have to compile down to a seperate .jar right?
It is possible to configure an IDEA project to point to a module in an external location. So you could configure multiple IDEA projects to point to the same hibernate module. This is a solution for a one-man show, primarily (although see here about using a variable to make this location configurable).
In order to make this distributable and sharable among multiple developers, you are looking at building a jar out of one module, or if it has no particular meaning to any specific project, making a new project that has the code and produces the jar, which other projects then have as a library.
You can use Spring or Guice for dependency injection. Refactor your dao/services to use generic, so if your children modules don't share the same pojo you can still reuse all your hibernate codes (for dao and services) without any duplications (although you might want to make them abstract, in this case)

Categories

Resources