Maven 2 replace class implementation depending on profile?

I also have

two classes of it:


. LiveMailTransport

will indeed send emails, but TestMailTransprot

will only log them for testing purposes.

Somewhere I am doing new MailTransport();

, and I would like to replace every use MailTransport

in my server-side code with either with Live-

or with TestMailTransport

depending on the profile used to compile (local, production, etc ...).

(Like gwts "replace-with" on the client side ...)

How could I do this using maven? Thank!


source to share

3 answers

What you want is a factory that takes a system property. If the system property is not set, create an instance LiveMailTransport

. If it's a property, create an instance TestMailTransport


Suggested Property Name:

Boolean.getBoolean (String) is your friend.

Now configure the surefire plugin to set the property and you 're done.



This sounds like a misuse of Maven because it looks more like a dependency injection task (like guice), but there is no relationship with Maven.



If you are using Spring or some other dependency injection framework, you can manipulate the dependencies injected based on the inclusion of the appropriate configuration.

But if you want to do it with a simple bare-bones Java application, you can create multiple factories that will create corresponding yoor MailTransport instances and put those factories in different source folders. Then use build-helper-maven-plugin to add source root folder based on active profiles.



All Articles