Maven 2 replace class implementation depending on profile?

I also have MailTransport.java

two classes of it: LiveMailTransport.java

and TestMailTransport.java

. 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!

+3


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: com.pany.app.enableTestMails



Boolean.getBoolean (String) is your friend.

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

+3


source


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.



0


source


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.

0


source







All Articles